(function () {
    if (window.MindwecoToast) return;

    class MindwecoToastComponent {
        constructor(options) {
            if (!options) options = {};
            this.options = {
                duration: 3000,
                position: 'end-0',
                topOffset: '80px'
            };
            var keys = Object.keys(options);
            for (var i = 0; i < keys.length; i++) {
                this.options[keys[i]] = options[keys[i]];
            }
            this.container = this.getOrCreateContainer();
        }

        getOrCreateContainer() {
            var container = document.getElementById('mindweco-toast-container');
            if (!container) {
                container = document.createElement('div');
                container.id = 'mindweco-toast-container';
                container.className = 'toast-container position-fixed ' + this.options.position + ' p-3';
                container.style.zIndex = '9999';
                container.style.top = this.options.topOffset;
                document.body.appendChild(container);
            }
            return container;
        }

        createToast(message, type, icon) {
            if (!icon) icon = '';

            var toast = document.createElement('div');
            toast.className = 'toast align-items-center text-bg-' + type + ' border-0';
            toast.setAttribute('role', 'alert');

            var dFlex = document.createElement('div');
            dFlex.className = 'd-flex';

            var body = document.createElement('div');
            body.className = 'toast-body';
            if (icon) {
                var iconSpan = document.createElement('span');
                iconSpan.className = 'toast-icon me-1';
                iconSpan.textContent = icon; 
                body.appendChild(iconSpan);
            }
            
            body.appendChild(document.createTextNode(message));

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

            dFlex.appendChild(body);
            dFlex.appendChild(closeBtn);
            toast.appendChild(dFlex);

            return toast;
        }

        show(message, type, icon) {
            if (!type) type = 'primary';
            if (!icon) icon = '';

            var toastEl = this.createToast(message, type, icon);
            this.container.appendChild(toastEl);

            var bsToast = new bootstrap.Toast(toastEl, {
                autohide: true,
                delay: this.options.duration
            });

            bsToast.show();

            toastEl.addEventListener('hidden.bs.toast', function() {
                toastEl.remove();
            });
        }

        success(msg) { return this.show(msg, 'success'); }
        error(msg)   { return this.show(msg, 'danger');  }
        warning(msg) { return this.show(msg, 'warning'); }
        info(msg)    { return this.show(msg, 'info');    }
    }

    window.MindwecoToast = new MindwecoToastComponent();
})();