(function () {
    'use strict';

    function readConfirmI18n() {
        return typeof window.zertiConfirmAlertI18n === 'object' && window.zertiConfirmAlertI18n !== null
            ? window.zertiConfirmAlertI18n
            : {};
    }

    var activeResolve = null;
    var keydownHandler = null;

    function getMount() {
        return document.body || document.getElementById('zertiAdmin');
    }

    function finish(confirmed) {
        var ov = getOverlay();
        if (!ov) {
            if (activeResolve) {
                var resolve = activeResolve;
                activeResolve = null;
                resolve(!!confirmed);
            }
            return;
        }
        ov.hidden = true;
        if (keydownHandler) {
            document.removeEventListener('keydown', keydownHandler);
            keydownHandler = null;
        }
        var confirmBtn = ov.querySelector('.zerti-confirm-alert-confirm');
        if (confirmBtn) {
            confirmBtn.disabled = false;
        }
        if (activeResolve) {
            var done = activeResolve;
            activeResolve = null;
            done(!!confirmed);
        }
    }

    function adminDir() {
        return window.zertiAdminTextDirection ? window.zertiAdminTextDirection() : 'rtl';
    }

    function getOverlay() {
        var mount = getMount();
        if (!mount) {
            return null;
        }
        var ov = document.querySelector('.zerti-confirm-alert-overlay');
        if (ov) {
            if (mount !== ov.parentNode) {
                mount.appendChild(ov);
            }
            ov.setAttribute('dir', adminDir());
            var dialogEl = ov.querySelector('.zerti-confirm-alert-dialog');
            if (dialogEl) {
                dialogEl.setAttribute('dir', adminDir());
            }
            return ov;
        }

        ov = document.createElement('div');
        ov.className = 'zerti-products-grid-alert-overlay zerti-confirm-alert-overlay';
        ov.setAttribute('role', 'alertdialog');
        ov.setAttribute('aria-modal', 'true');
        ov.setAttribute('aria-labelledby', 'zertiConfirmAlertMessage');
        ov.setAttribute('dir', adminDir());
        ov.hidden = true;
        ov.innerHTML =
            '<div class="zerti-products-grid-alert-dialog zerti-confirm-alert-dialog alert alert-danger" dir="' +
            adminDir() +
            '">' +
            '<p id="zertiConfirmAlertMessage" class="zerti-products-grid-alert-message zerti-confirm-alert-message mb-0" dir="' +
            adminDir() +
            '"></p>' +
            '<div class="zerti-confirm-alert-actions">' +
            '<button type="button" class="btn btn-light zerti-confirm-alert-cancel"></button>' +
            '<button type="button" class="btn btn-danger zerti-confirm-alert-confirm"></button>' +
            '</div>' +
            '</div>';

        mount.appendChild(ov);

        var cancelBtn = ov.querySelector('.zerti-confirm-alert-cancel');
        var confirmBtn = ov.querySelector('.zerti-confirm-alert-confirm');

        if (cancelBtn) {
            cancelBtn.addEventListener('click', function () {
                finish(false);
            });
        }
        if (confirmBtn) {
            confirmBtn.addEventListener('click', function () {
                finish(true);
            });
        }
        ov.addEventListener('click', function (e) {
            if (e.target === ov) {
                finish(false);
            }
        });

        return ov;
    }

    function hide() {
        finish(false);
    }

    function setConfirmDisabled(disabled) {
        var ov = getOverlay();
        if (!ov) {
            return;
        }
        var confirmBtn = ov.querySelector('.zerti-confirm-alert-confirm');
        if (confirmBtn) {
            confirmBtn.disabled = !!disabled;
        }
    }

    /**
     * @param {{ message?: string, cancelText?: string, confirmText?: string }} opts
     * @returns {Promise<boolean>}
     */
    function show(opts) {
        opts = opts || {};
        var message = opts.message == null ? '' : String(opts.message);
        if (!message) {
            return Promise.resolve(false);
        }

        var ov = getOverlay();
        if (!ov) {
            return Promise.resolve(window.confirm(message));
        }

        if (activeResolve) {
            finish(false);
        }

        var msgEl = ov.querySelector('.zerti-confirm-alert-message');
        var cancelBtn = ov.querySelector('.zerti-confirm-alert-cancel');
        var confirmBtn = ov.querySelector('.zerti-confirm-alert-confirm');

        if (msgEl) {
            msgEl.setAttribute('dir', adminDir());
            msgEl.textContent = message;
        }
        var i18n = readConfirmI18n();
        if (cancelBtn) {
            cancelBtn.textContent = opts.cancelText || i18n.cancel || '';
        }
        if (confirmBtn) {
            confirmBtn.textContent = opts.confirmText || i18n.delete || '';
            confirmBtn.disabled = false;
        }

        return new Promise(function (resolve) {
            activeResolve = resolve;
            ov.hidden = false;

            keydownHandler = function (e) {
                if (e.key === 'Escape') {
                    finish(false);
                }
            };
            document.addEventListener('keydown', keydownHandler);

            if (cancelBtn && typeof cancelBtn.focus === 'function') {
                cancelBtn.focus();
            }
        });
    }

    /**
     * @param {string} message
     * @param {{ cancel?: string, delete?: string }} [i18n]
     * @returns {Promise<boolean>}
     */
    function showDeleteConfirm(message, i18n) {
        var defaults = readConfirmI18n();
        i18n = i18n || {};
        return show({
            message: message,
            cancelText: i18n.cancel || defaults.cancel || '',
            confirmText: i18n.delete || defaults.delete || '',
        });
    }

    window.zertiConfirmAlert = {
        show: show,
        showDeleteConfirm: showDeleteConfirm,
        hide: hide,
        setConfirmDisabled: setConfirmDisabled,
    };
})();
