if (typeof window.MindwecoModal === 'undefined') {

  function mindwecoEscapeHtml(str) {
    if (!str) return '';
    return String(str)
      .replace(/&/g, '&amp;')
      .replace(/</g, '&lt;')
      .replace(/>/g, '&gt;')
      .replace(/"/g, '&quot;')
      .replace(/'/g, '&#039;');
  }

  class MindwecoModal {
    constructor(id, options = {}) {
      this.id = id;
      this.element = document.getElementById(id);
      this.modal = null;
      this.options = {
        backdrop: 'static',
        keyboard: true,
        focus: true,
        ...options
      };
    }

    show() {
      if (!this.element) return this;
      if (!this.modal) {
        this.modal = new bootstrap.Modal(this.element, this.options);
      }
      this.modal.show();
      return this;
    }

    hide() {
      if (this.modal) {
        this.modal.hide();
      }
      return this;
    }

    toggle() {
      if (this.modal) {
        this.modal.toggle();
      } else {
        this.show();
      }
      return this;
    }

    setTitle(title) {
      var titleEl = this.element ? this.element.querySelector('.modal-title') : null;
      if (titleEl) titleEl.textContent = title; 
      return this;
    }

    setContent(content) {
      var body = this.element ? this.element.querySelector('.modal-body') : null;
      if (body) body.textContent = content; 
      return this;
    }

    setFooter(footer) {
      var footerEl = this.element ? this.element.querySelector('.modal-footer') : null;
      if (!footerEl && footer) {
        footerEl = document.createElement('div');
        footerEl.className = 'modal-footer';
        this.element.querySelector('.modal-content').appendChild(footerEl);
      }
      if (footerEl) footerEl.textContent = footer; 
      return this;
    }

    setLoading(isLoading, message) {
      if (isLoading === undefined) isLoading = true;
      if (!message) message = 'Loading...';
      var body = this.element ? this.element.querySelector('.modal-body') : null;
      if (!body) return this;
      if (isLoading) {
        body.innerHTML = '';
        var wrapper = document.createElement('div');
        wrapper.className = 'text-center py-5';

        var spinner = document.createElement('div');
        spinner.className = 'spinner-border text-primary';
        spinner.setAttribute('role', 'status');
        var srSpan = document.createElement('span');
        srSpan.className = 'visually-hidden';
        srSpan.textContent = 'Loading...';
        spinner.appendChild(srSpan);

        var p = document.createElement('p');
        p.className = 'mt-3 mb-0';
        p.textContent = message; 
        wrapper.appendChild(spinner);
        wrapper.appendChild(p);
        body.appendChild(wrapper);
      }
      return this;
    }

    onConfirm(callback) {
      var confirmBtn = this.element ? this.element.querySelector('[data-confirm]') : null;
      if (confirmBtn) {
        confirmBtn.onclick = function() {
          callback();
          this.hide();
        }.bind(this);
      }
      return this;
    }

    on(event, callback) {
      if (this.element) {
        this.element.addEventListener(event, callback);
      }
      return this;
    }

    dispose() {
      if (this.modal) {
        this.modal.dispose();
        this.modal = null;
      }
    }

    static confirm(title, message, onConfirm, confirmText, confirmClass) {
      if (!confirmText) confirmText = 'Unindex';
      if (!confirmClass) confirmClass = 'btn-danger';

      var existing = document.getElementById('mindweco-confirm-modal');
      if (existing) existing.remove();

      var modalDiv = document.createElement('div');
      modalDiv.className = 'modal fade';
      modalDiv.id = 'mindweco-confirm-modal';
      modalDiv.setAttribute('tabindex', '-1');

      var dialog = document.createElement('div');
      dialog.className = 'modal-dialog modal-dialog-centered';
      dialog.style.maxWidth = '500px';

      var content = document.createElement('div');
      content.className = 'modal-content rounded-3 shadow';

      var header = document.createElement('div');
      header.className = 'modal-header text-white';
      header.style.background = 'linear-gradient(135deg, #1a365d 0%, #0f172a 100%)';
      var titleEl = document.createElement('h5');
      titleEl.className = 'modal-title-header';
      titleEl.textContent = title; 
      var closeBtn = document.createElement('button');
      closeBtn.type = 'button';
      closeBtn.className = 'btn-close btn-close-white';
      closeBtn.setAttribute('data-bs-dismiss', 'modal');
      closeBtn.setAttribute('aria-label', 'Close');
      header.appendChild(titleEl);
      header.appendChild(closeBtn);

      var body = document.createElement('div');
      body.className = 'modal-body py-4';
      var msgP = document.createElement('p');
      msgP.className = 'mb-0';
      msgP.textContent = message; 
      body.appendChild(msgP);

      var footer = document.createElement('div');
      footer.className = 'modal-footer';
      var cancelBtn = document.createElement('button');
      cancelBtn.type = 'button';
      cancelBtn.className = 'btn btn-secondary';
      cancelBtn.setAttribute('data-bs-dismiss', 'modal');
      cancelBtn.textContent = 'Close';
      var confirmBtn = document.createElement('button');
      confirmBtn.type = 'button';
      confirmBtn.className = 'btn ' + confirmClass;
      confirmBtn.setAttribute('data-confirm', '');
      confirmBtn.textContent = confirmText; 
      footer.appendChild(cancelBtn);
      footer.appendChild(confirmBtn);

      content.appendChild(header);
      content.appendChild(body);
      content.appendChild(footer);
      dialog.appendChild(content);
      modalDiv.appendChild(dialog);
      document.body.appendChild(modalDiv);

      var modal = new MindwecoModal('mindweco-confirm-modal');
      modal.show().onConfirm(onConfirm);
      modal.on('hidden.bs.modal', function() {
        if (modal.element) modal.element.remove();
      });
      return modal;
    }

    static loading(title, message) {
      if (!message) message = 'Loading...';

      var existing = document.getElementById('mindweco-loading-modal');
      if (existing) existing.remove();

      var modalDiv = document.createElement('div');
      modalDiv.className = 'modal fade';
      modalDiv.id = 'mindweco-loading-modal';
      modalDiv.setAttribute('tabindex', '-1');
      modalDiv.setAttribute('aria-hidden', 'true');
      modalDiv.setAttribute('data-bs-backdrop', 'static');
      modalDiv.setAttribute('data-bs-keyboard', 'false');

      var dialog = document.createElement('div');
      dialog.className = 'modal-dialog modal-dialog-centered modal-sm';

      var content = document.createElement('div');
      content.className = 'modal-content';

      var header = document.createElement('div');
      header.className = 'modal-header';
      var titleEl = document.createElement('h5');
      titleEl.className = 'modal-title';
      titleEl.textContent = title; 
      header.appendChild(titleEl);

      var body = document.createElement('div');
      body.className = 'modal-body text-center py-4';
      var spinner = document.createElement('div');
      spinner.className = 'spinner-border text-primary';
      spinner.setAttribute('role', 'status');
      var srSpan = document.createElement('span');
      srSpan.className = 'visually-hidden';
      srSpan.textContent = 'Loading...';
      spinner.appendChild(srSpan);
      var p = document.createElement('p');
      p.className = 'mt-3 mb-0';
      p.textContent = message; 
      body.appendChild(spinner);
      body.appendChild(p);

      content.appendChild(header);
      content.appendChild(body);
      dialog.appendChild(content);
      modalDiv.appendChild(dialog);
      document.body.appendChild(modalDiv);

      var modal = new MindwecoModal('mindweco-loading-modal', {
        backdrop: 'static',
        keyboard: false
      });
      modal.show();
      return modal;
    }
  }

  window.MindwecoModal = MindwecoModal;
}