(function($) {
    /* Handle success request response */
    function handleSuccess(response, redirect = null) {
        if (typeof response.data?.message !== 'undefined') {
            toastrAlert(response.data?.message, response.success ? 'success' : 'error')
        }

        if ((typeof response.data?.redirect !== 'undefined' && response.data?.redirect !== null) || (typeof redirect !== 'undefined' && redirect !== null)) {
            location.replace(redirect || response.data.redirect);
        }
    }

    window.handleSuccess = handleSuccess

    /* Handle error request response */
    function handleError(e, redirect = null) {
        let message = ''
        if (e.status === 0) {
            message = 'Not connected Please verify your network connection.'
        }
        else if (e.status === 200 && typeof redirect !== 'undefined') {
            location.replace(redirect);
        }
        else if (e.status === 404) {
            message = 'The requested data not found.'
        }
        else if (e.status === 403) {
            message = 'You are not allowed this action.'
        }
        else if (e.status === 419) {
            message = 'Nonce verify failed, please try again letter.'
        }
        else if (e.status === 500) {
            message = e.responseJSON?.data?.message || 'Internal Server Error, Please try again letter.'
        }
        else if (e === 'parsererror') {
            message = 'Requested JSON parse failed.'
        }
        else if (e === 'timeout') {
            message = 'Requested Time out.'
        }
        else if (e === 'abort') {
            message = 'Request aborted.'
        }
        else if (e.status === 422) {
            $.each(e.responseJSON.data.errors, function (index, error) {
                $("#invalid_" + index).text(error[0]);
                $("#" + index).addClass("is-invalid");
            });
            message = e.responseJSON?.data?.message
        }
        else if ([300, 301, 302, 401].includes(e.status)) {
            message = e.responseJSON?.data?.message
        }
        else {
            message = e.statusText
        }
        toastrAlert(message, 'error')

        return true;
    }
    window.handleError = handleError

    function toastrAlert(message, type = 'success') {
        if (location.pathname == '/wp-admin/admin.php') {
            const element = $('#ehx-notice');

            const alert = {
                success: 'notice-success',
                error: 'notice-error',
                info: 'notice-success',
            }[type]


            element.css('display', 'block')
            element.addClass(alert).find('p').html(message)

            setTimeout(() => element.css('display', 'none'), 10000)
        } 
        else {
            const element = $('#ehx-alert-message');

            const alert = {
                success: {
                    color: 'ehx-alert-primary',
                    icon: 'fa-regular fa-circle-question',
                },
                error: {
                    color: 'ehx-alert-danger',
                    icon: 'fa-solid fa-triangle-exclamation',
                },
                info: {
                    color: 'ehx-alert-info',
                    icon: 'fa-regular fa-circle-question',
                },
            }[type]

            element.parent().removeClass('d-none')

            element.attr('class', `ehx-alert ${alert.color} text-center rounded-0`)
            element.find('i').attr('class', `${alert.icon}`)
            element.find('span').html(message)

            setTimeout(() => element.parent().addClass('d-none'), 10000)
        }
        
    }
    window.toastrAlert = toastrAlert

})(jQuery);