;(function (global) {
    const toastContainer = document.createElement('div');
    toastContainer.className = 'toast-container position-fixed top-0 p-3';
    toastContainer.style.zIndex = '11000';
    toastContainer.style.left = '50%';
    toastContainer.style.top = '-29px';
    toastContainer.style.transform = 'translateX(-50%)';
    document.body.appendChild(toastContainer);

    function showToast(message, variant = '#0d9488') {
        const toast = document.createElement('div');
        toast.className = 'toast align-items-center text-white d-flex border-0';
        toast.setAttribute('role', 'alert');
        toast.setAttribute('aria-live', 'assertive');
        toast.setAttribute('aria-atomic', 'true');
        toast.style.background = variant;

        const body = document.createElement('div');
        body.className = 'toast-body flex-grow-1';
        body.textContent = message;

        const closeBtn = document.createElement('button');
        closeBtn.type = 'button';
        closeBtn.className = 'btn-close btn-close-white me-2 m-auto';
        closeBtn.setAttribute('data-bs-dismiss', 'toast');
        closeBtn.setAttribute('aria-label', 'Close');

        toast.append(body, closeBtn);
        toastContainer.append(toast);

        const bsToast = new bootstrap.Toast(toast, { delay: 3500 });
        bsToast.show();
        toast.addEventListener('hidden.bs.toast', () => toast.remove());
    }

    function openConfirmationModal(options = {}) {
        const defaults = {
            title: 'Confirmar ação',
            message: 'Deseja prosseguir?',
            confirmText: 'Confirmar',
            cancelText: 'Cancelar',
            onConfirm() {},
            onCancel() {}
        };

        const config = { ...defaults, ...options };

        new DeleteModal({
            title: config.title,
            message: config.message,
            confirmText: config.confirmText,
            cancelText: config.cancelText,
            onConfirm() {
                config.onConfirm();
            },
            onCancel() {
                config.onCancel();
            }
        }).open();
    }

    function attachConfirmation(selector, options = {}) {
        const element = document.querySelector(selector);
        if (!element) return;
        element.addEventListener('click', () => openConfirmationModal(options));
    }

    global.ModalHelper = {
        showToast,
        openConfirmationModal,
        attachConfirmation
    };
})(window);
