(function ($) {
    function author_kit_ensure_toast_container() {
        if ($('#author-kit-toast-container').length === 0) {
            $('body').append('<div id="author-kit-toast-container"></div>');
        }
    }

    function author_kit_create_toast(message, type, timeout) {
        author_kit_ensure_toast_container();

        let existing_performing_toast = $('#author-kit-toast-container .author-kit-toast.author-kit-performing');

        // Replace performing toast if active
        if (type === 'success' || type === 'error') {
            if (existing_performing_toast.length) {
                existing_performing_toast.find('i').attr('class', author_kit_get_icon(type));
                existing_performing_toast
                    .removeClass('author-kit-performing')
                    .addClass(get_type_class(type));
                existing_performing_toast.find('.author-kit-toast-message').text(message);

                setTimeout(() => {
                    author_kit_close_toast(existing_performing_toast);
                }, timeout);
                return existing_performing_toast;
            }
        }

        // Create new toast
        const toast = $('<div class="author-kit-toast ' + get_type_class(type) + '" role="status" aria-live="polite"></div>');
        const close_button = $('<span class="author-kit-toast-close-btn" aria-label="Close notification">&times;</span>');

        toast.append('<i class="' + author_kit_get_icon(type) + '" aria-hidden="true"></i><span class="author-kit-toast-message">' + message + '</span>');
        toast.append(close_button);

        $('#author-kit-toast-container').append(toast);

        setTimeout(() => {
            toast.addClass('show');
        }, 10);

        if (type !== 'performing') {
            const auto_close_timer = setTimeout(() => {
                author_kit_close_toast(toast);
            }, timeout);

            close_button.on('click', function () {
                clearTimeout(auto_close_timer);
                author_kit_close_toast(toast);
            });
        } else {
            close_button.on('click', function () {
                author_kit_close_toast(toast);
            });
        }

        return toast;
    }

    function author_kit_close_toast(toast) {
        toast.addClass('hide');
        setTimeout(() => {
            toast.remove();
        }, 400);
    }

    function author_kit_get_icon(type) {
        switch (type) {
            case 'success':
                return 'fa-solid fa-circle-check';
            case 'error':
                return 'fa-solid fa-circle-xmark';
            case 'performing':
                return 'fa-solid fa-spinner fa-spin';
            default:
                return '';
        }
    }

    function get_type_class(type) {
        switch (type) {
            case 'success':
                return 'author-kit-success';
            case 'error':
                return 'author-kit-error';
            case 'performing':
                return 'author-kit-performing';
            default:
                return '';
        }
    }

    window.show_author_kit_toast = function (message, type, timeout = 3000) {
        author_kit_create_toast(message, type, timeout);
    };

    window.show_author_kit_progress_toast = function (performing_message, performing_duration, final_state, final_message) {
        const performing_toast = author_kit_create_toast(performing_message, 'performing', performing_duration + 1000);

        setTimeout(() => {
            performing_toast.find('i').attr('class', author_kit_get_icon(final_state));
            performing_toast
                .removeClass('author-kit-performing')
                .addClass(get_type_class(final_state));
            performing_toast.find('.author-kit-toast-message').text(final_message);

            setTimeout(() => {
                author_kit_close_toast(performing_toast);
            }, 3000);
        }, performing_duration);
    };
})(jQuery);
