(function($) {
    $(document).ready(function() {

        // Form confirmation
        $("form.starterpack-form").areYouSure({
            message: "Changes that you made may not be saved"
        });

        // Reset confirmation
        $(".reset-confirm").click(function(event) {
            if (!confirm("This will reset this plugin settings. Are you sure?")) {
                event.preventDefault(); // Prevent the default action (resetting the plugin settings)
                return false; // Stop further execution of the function
            }
        });

        // Tab navigation
        $(".starterpack-navigation ul li a:not(.starterpack-ignore)").click(function() {
            var tab = $(this).attr("data-tab");

            $(".starterpack-navigation ul li a").removeClass("current");
            $(".tab-content").removeClass("current");

            // Fade out the current section
            $(".starterpack-content section.current").fadeOut(175, function() {
                $(this).removeClass("current");

                // Fade in the new section
                $(".starterpack-content section." + tab).fadeIn(175, function() {
                    $(this).addClass("current");
                });
            });

            $(this).addClass("current");
            updateActionURL(tab);
            $("html, body").animate({
                scrollTop: 0
            }, 400);
        });

        // Update action URL with tab
        function updateActionURL(tab) {
            var form = $(".starterpack-form");
            var action = form.attr("action");

            if (action.indexOf("#") !== -1) {
                var hash = action.replace(/.*#/, "#");
                action = action.replace(/#.*/, "");
            }

            form.attr({
                action: action + "#" + tab
            });
        }

        // Set current tab based on hash
        var hash = window.location.hash;
        hash = hash.replace("#", "");

        if (hash !== "") {
            $(".starterpack-navigation ul li a#starterpack_" + hash).addClass("current");
            $(".starterpack-content section." + hash).addClass("current");
            updateActionURL(hash);
        } else if ($(".starterpack-content section.current").length === 0) {
            $(".starterpack-content section").eq(0).addClass("current");
            $(".starterpack-navigation ul li").eq(0).find("a").addClass("current");
        }
    });

    $(document).ready(function() {
        // Main toggle
        $('.main-toggle[type="checkbox"]').change(function() {
            var isChecked = $(this).prop("checked");
            var revised = $(this).attr("data-revised");
            var subFields = $(this).parents(".toggle-group").find(".sub-fields");

            if (subFields.length) {
                if ((isChecked && revised !== "1" && subFields.find('input[type="checkbox"]:checked').length === subFields.find('input[type="checkbox"]').length) ||
                    (!isChecked && revised === "1")) {
                    subFields.fadeOut();
                } else {
                    subFields.fadeIn();
                }
            }
        }).trigger("change");

        // Toggle arrow
        $(".starterpack-toggle-arrow").on("click", function() {
            $(this).toggleClass("active");
            $(this).next("ul").slideToggle();
        });

        // Show/hide admin menu
        $("#enable_starterpack_admin").on("change", function() {
            if ($(this).is(":checked")) {
                $(".menu-admin-wrapper").show();
            } else {
                $(".menu-admin-wrapper").hide();
            }
        }).trigger("change");

        // Show/hide welcome for user roles
        $(".enable_welcome_for_all_roles").on("change", function() {
            var userRoles = $("#select_user_roles" + $(this).data("section"));

            if ($(this).is(":checked")) {
                userRoles.fadeOut();
            } else {
                userRoles.fadeIn();
            }
        });
    });

    // specific for admin menu control
    $(".starterpack-menus-settings > ul > li").each((function() {
        $(this).find('input[type="checkbox"]:checked').length == $(this).find('input[type="checkbox"]').length ? $(this).find("ul.sub_menu_wrapper").hide() : $(this).find("ul.sub_menu_wrapper").hide()
    }));

    // Plugin notice dismiss
    $(document).ready(function() {
        var notice = $(".starterpack-notice");

        if (notice.length && notice.hasClass("is-dismissible")) {
            window.setTimeout(function() {
                notice.fadeOut(800);
            }, 0);
        }
    });

    // Show and hide content
    $(document).ready(function() {
        // Show/hide content based on checkbox
        $(".show-hide").change(function() {
            var isChecked = $(this).prop("checked");
            var showHideAttr = $(this).attr("data-show-hide");
            var showHideContent = $(this).parents(".show-hide-group").find(".show-hide-content");

            if (showHideContent.length) {
                if ((isChecked && showHideAttr !== "1" && showHideContent.find('input[type="checkbox"]:checked').length === showHideContent.find('input[type="checkbox"]').length) ||
                    (!isChecked && showHideAttr === "1")) {
                    showHideContent.fadeOut();
                } else {
                    showHideContent.fadeIn();
                }
            }
        }).trigger("change");
    });

    $(document).ready(function() {
        // Hide/show content based on checkbox
        $(".hide-show").change(function() {
            var isChecked = $(this).prop("checked");
            var hideShowAttr = $(this).attr("data-hide-show");
            var hideShowContent = $(this).parents(".hide-show-group").find(".hide-show-content");

            if (hideShowContent.length) {
                if ((isChecked && hideShowAttr !== "1" && hideShowContent.find('input[type="checkbox"]:checked').length === hideShowContent.find('input[type="checkbox"]').length) ||
                    (!isChecked && hideShowAttr === "1")) {
                    hideShowContent.fadeOut();
                } else {
                    hideShowContent.fadeIn();
                }
            }
        }).trigger("change");
    });

    // Open WordPress Add Media library and add thumbnail image
    $(document).ready(function() {
        $(".starterpack_upload").click(function() {
            var attachment = wp.media.editor.send.attachment;
            var button = $(this);

            wp.media.editor.send.attachment = function(props, attachment) {
                var image = '<img id="' + attachment.id + '" src="' + attachment.url + '" />';
                image += '<span class="dashicons dashicons-dismiss starterpack-remove-img"></span>';
                button.parents(".starterpack-input-group").find(".starterpack-upload-thumbnail").html(image);
                button.parents(".starterpack-input-group").find(".starterpack-upload-input").val(attachment.url);
                //n(); <-- Removed the line causing the error
                wp.media.editor.send.attachment = attachment;
            };

            wp.media.editor.open();
            return false;
        });

        // Remove uploaded image
        $(document).on("click", ".starterpack-remove-img", function() {
            var inputGroup = $(this).closest(".starterpack-input-group");
            inputGroup.find(".starterpack-upload-thumbnail").html("");
            inputGroup.find(".starterpack-upload-input").val("");
        });
    });

    // Initialize the color picker for the .starterpack-color-field elements
    $(document).ready(function() {
        $(".starterpack-color-field").wpColorPicker();
    });

    // Attach click event listener to elements with the class .starterpack-preview-link, indicating that it is related to a preview link
    $(document).ready(function() {
        $(".starterpack-preview-link").on("click", function() {
            $("#starterpack-preview-content").html("...");
            var e = $(".starterpack-form").serialize() + "&action=starterpack_save_login_preview_settings&_ajax_nonce=" + starterpack_settings.starterpack_ajax_nonce;
            jQuery.ajax({
                type: "post",
                url: ajaxurl,
                data: e,
                success: function(e) {
                    //$("#starterpack-preview-content").html('<iframe allowtransparency="true" class="long-animate starterpack-preview-iframe" title="Login Preview" onmousewheel="" src="' + starterpack_settings.loginurl + '?starterpack-action=preview&preview_section=login&reauth=1"></iframe>');
                    $("#starterpack-preview-content").html('<iframe allowtransparency="true" class="long-animate starterpack-preview-iframe" title="Login Preview" onmousewheel="" src="' + starterpack_settings.loginurl + '?starterpack-action=preview&preview_section"></iframe>');
                    $("iframe.starterpack-preview-iframe").load(function() {
                        var e = $(this),
                            t = e.contents();
                        e.slideDown("slow");
                        t.find("body").prepend('<div style="position:absolute;bottom:1.74rem;right:2rem;margin:0;padding:0;background-color:rgba(225,225,225,.75);font-family:verdana;font-size:.94rem;font-weight:bold;line-height:1;text-decoration:underline;text-underline-position:under;-webkit-font-smoothing:antialiased;letter-spacing:-0.005em;word-spacing:-0.05em;width:226px;height:166px;display:flex;justify-content:center;align-items:center;border-radius:8px;text-shadow:0 0 1px rgba(0,0,0,.25);color:#fff;mix-blend-mode:difference;">Login Preview</div>');
                        t.find("form").attr("action", "#");
                        t.find("#wp-submit").attr("type", "button");
                        t.find("a").attr("href", "#");
                    });
                }
            });
        });

        $(".starterpack-dashboard-preview-link").on("click", function() {
            $("#starterpack-dashboard-content").html("...");
            var e = $(".starterpack-form").serialize() + "&action=starterpack_save_dashboard_preview_settings";
            jQuery.ajax({
                type: "post",
                url: ajaxurl,
                data: e,
                success: function(e) {
                    $("#starterpack-preview-content").html('<iframe allowtransparency="true" class="long-animate starterpack-preview-iframe" title="Preview" onmousewheel="" src="' + starterpack_settings.adminurl + '?starterpack-action=preview&preview_section=dashboard"></iframe>');
                    $("iframe.starterpack-preview-iframe").load(function() {
                        var e = $(this),
                            t = e.contents();
                        e.slideDown("slow");
                        t.find("body").prepend('<div style="text-align: center;position: absolute;top: 40px;margin: 0 30%;font-weight: 300;font-size: 24px;background: #dad9d9cf;line-height: 35px;padding: 0 20px;color: #790404;width: 40%;z-index:999999">Preview mode</div>');
                        t.find("a").each(function() {
                            $(this).attr("href", "#");
                        });
                    });
                }
            });
        });
    });

})(jQuery);