(function ($, window) {
    var my_account = {
        open_class: 'prodigy-navbar-account__wrap--open',
        my_account_element: '#my-account',
        toggle_button_selector: 'button[aria-controls="prodigy-account-menu"]',
        main_widget_container: '#my-account-widget-js',

        init: function () {
            this.load_widget();
            this.toggle_widget();
            this.logout();
            this.closeOnOutsideClick();
            this.user_login();
        },

        load_widget: function () {
            let $main = $(this.main_widget_container);
            if (!$main.length) {
                return;
            }

            // Skip AJAX if widget content was pre-rendered server-side
            if ($main.children().length > 0) {
                return;
            }

            let data = JSON.parse($('#my-account-widget-options-js').val());

            let post_data = {
                action: "prodigy-render-my-account-widget",
                parameters: data,
                nonce: options.nonce
            };

            $.ajax({
                type: "post",
                data: post_data,
                dataType: "json",
                url: ajax_url,
                success: function (response) {
                    if (response.success && response.data && response.data.html) {
                        $main.html(response.data.html);
                    }
                },
                error: function () {
                    console.error('Failed to load my account widget');
                }
            });
        },

        get_toggle_button: function ($parent) {
            return $parent.find(this.toggle_button_selector);
        },

        get_aria_block: function () {
            return $('.prodigy-account__block');
        },

        set_aria: function ($element, ariaAttr, value) {
            if ($element.length) {
                $element.attr(ariaAttr, value);
            }
        },

        user_login: function () {
            $(document).on('click', '.user-login-js', this.handle_login.bind(this));
        },

        handle_login: function () {
            let post_data = {
                action: "prodigy-user-login",
                current_url: window.location.href,
                nonce: options.nonce
            };

            $.ajax({
                type: "post",
                data: post_data,
                dataType: "json",
                url: ajax_url,
                success: function (data) {
                    window.location.href = data.data.login_url;
                }
            });
        },

        toggle_widget: function () {
            $(document).off('click', this.my_account_element).on('click', this.my_account_element, this.toggle_account.bind(this));
        },

        toggle_account: function (e) {
            e.stopPropagation();
            let $this = $(e.currentTarget);
            let is_open = $this.hasClass(this.open_class);
            let $toggle_btn = this.get_toggle_button($this);
            let $ariaBlock = this.get_aria_block();

            $this.toggleClass(this.open_class);
            this.set_aria($toggle_btn, 'aria-expanded', !is_open);
            this.set_aria($ariaBlock, 'aria-hidden', is_open);
        },

        get_account_element: function () {
            return $(this.my_account_element);
        },

        closeOnOutsideClick: function () {
            $(document).on('click', this.handle_outside_click.bind(this));
        },

        handle_outside_click: function (e) {
            if (!$(e.target).closest(this.my_account_element).length) {
                let $account = this.get_account_element();
                let $toggle_button = this.get_toggle_button($account);
                let $ariaBlock = this.get_aria_block();

                $account.removeClass(this.open_class);
                this.set_aria($toggle_button, 'aria-expanded', 'false');
                this.set_aria($ariaBlock, 'aria-hidden', 'true');
            }
        },

        logout: function () {
            $(document).on('click', '.user-logout-js, .impersonation-frame__button', this.handle_logout.bind(this));
        },

        handle_logout: function () {
            let post_data = {
                action: "prodigy-user-logout",
                nonce: options.nonce
            };

            $.ajax({
                type: "post",
                data: post_data,
                dataType: "json",
                url: ajax_url,
                success: function () {
                    location.reload();
                }
            });
        }
    };

    window.my_account = my_account;
})(jQuery, window);

jQuery(function ($) {
    window.my_account.init();
});
