(function () {
    var backdrop = document.querySelector('[data-modal-backdrop]');
    if (!backdrop) return;

    var title = backdrop.querySelector('[data-modal-title]');
    var body = backdrop.querySelector('[data-modal-body]');
    var close = backdrop.querySelector('[data-modal-close]');
    var actions = backdrop.querySelector('[data-modal-actions]');
    var lastFocused = null;

    function clearActions() {
        actions.innerHTML = '';
        actions.hidden = true;
    }

    function closeModal() {
        backdrop.hidden = true;
        clearActions();
        if (lastFocused && typeof lastFocused.focus === 'function') lastFocused.focus();
    }

    window.ZaloroxModal = {
        open: function (modalTitle, modalBody, modalActions) {
            lastFocused = document.activeElement;
            title.textContent = modalTitle || '';
            body.textContent = modalBody || '';
            clearActions();

            if (Array.isArray(modalActions) && modalActions.length) {
                modalActions.forEach(function (item) {
                    var button = document.createElement('button');
                    button.type = 'button';
                    button.textContent = item.label || 'Tamam';
                    if (item.primary) button.classList.add('primary');
                    if (item.danger) button.classList.add('danger');
                    button.addEventListener('click', function () {
                        if (typeof item.onClick === 'function') item.onClick();
                    });
                    actions.appendChild(button);
                });
                actions.hidden = false;
            }

            backdrop.hidden = false;
            var firstButton = actions.querySelector('button') || close;
            window.setTimeout(function () { firstButton.focus(); }, 0);
        },
        close: closeModal,
        alert: function (message, options) {
            options = options || {};

            return new Promise(function (resolve) {
                window.ZaloroxModal.open(options.title || 'Bilgi', message || '', [
                    {
                        label: options.okLabel || 'Devam et',
                        primary: true,
                        onClick: function () {
                            closeModal();
                            resolve(true);
                        }
                    }
                ]);
            });
        },
        confirm: function (message, options) {
            options = options || {};

            return new Promise(function (resolve) {
                window.ZaloroxModal.open(options.title || 'Onay', message || '', [
                    {
                        label: options.cancelLabel || 'Vazgec',
                        onClick: function () {
                            closeModal();
                            resolve(false);
                        }
                    },
                    {
                        label: options.confirmLabel || 'Onayla',
                        primary: true,
                        danger: options.danger === true,
                        onClick: function () {
                            closeModal();
                            resolve(true);
                        }
                    }
                ]);
            });
        },
        choice: function (message, options) {
            options = options || {};

            return new Promise(function (resolve) {
                window.ZaloroxModal.open(options.title || 'Tercih', message || '', [
                    {
                        label: options.rejectLabel || 'Reddet',
                        danger: true,
                        onClick: function () {
                            closeModal();
                            resolve('reject');
                        }
                    },
                    {
                        label: options.cancelLabel || 'Vazgec',
                        onClick: function () {
                            closeModal();
                            resolve('cancel');
                        }
                    },
                    {
                        label: options.acceptLabel || 'Kabul et',
                        primary: true,
                        onClick: function () {
                            closeModal();
                            resolve('accept');
                        }
                    }
                ]);
            });
        }
    };

    close.addEventListener('click', closeModal);
    backdrop.addEventListener('click', function (event) {
        if (event.target === backdrop) closeModal();
    });
    document.addEventListener('keydown', function (event) {
        if (event.key === 'Escape' && !backdrop.hidden) closeModal();
    });
})();
