(function() {
  'use strict';

  function ensureRegion() {
    var region = document.querySelector('.yatoon-toast-region');
    if (!region) {
      region = document.createElement('div');
      region.className = 'yatoon-toast-region';
      region.setAttribute('aria-live', 'polite');
      region.setAttribute('aria-atomic', 'true');
      region.setAttribute('role', 'status');
      document.body.appendChild(region);
    }
    return region;
  }

  function normalizeType(type) {
    return ['error', 'warning', 'success', 'info'].indexOf(type) !== -1 ? type : 'info';
  }

  function titleFor(type) {
    if (type === 'success') { return 'Saved'; }
    if (type === 'error') { return 'Needs attention'; }
    if (type === 'warning') { return 'Check this'; }
    return 'Yatoon';
  }

  function installToast() {
    window.YatoonToast = function(type, message, title) {
    type = normalizeType(type);
    message = String(message || '');

    var toast = document.createElement('div');
    toast.className = 'yatoon-toast is-' + type;

    var copy = document.createElement('div');
    var heading = document.createElement('div');
    heading.className = 'yatoon-toast-title';
    heading.textContent = title || titleFor(type);
    var body = document.createElement('div');
    body.className = 'yatoon-toast-message';
    body.textContent = message;
    copy.appendChild(heading);
    copy.appendChild(body);

    var close = document.createElement('button');
    close.type = 'button';
    close.className = 'yatoon-toast-close';
    close.setAttribute('aria-label', 'Dismiss notice');
    close.innerHTML = '&times;';

    toast.appendChild(copy);
    toast.appendChild(close);
    ensureRegion().appendChild(toast);

    var timer = window.setTimeout(function() {
      toast.style.opacity = '0';
      window.setTimeout(function() { toast.remove(); }, 180);
    }, type === 'error' ? 7000 : 4200);

    close.addEventListener('click', function() {
      window.clearTimeout(timer);
      toast.remove();
    });
    };

    if (document.body && !document.body.dataset.yatoonAlertEnhanced) {
      document.body.dataset.yatoonAlertEnhanced = '1';
      var nativeAlert = window.alert;
      window.alert = function(message) {
        var search = window.location.search || '';
        var isYatoonPage = document.body.classList.contains('wp-admin') &&
          search.indexOf('page=') !== -1 &&
          (search.indexOf('yatoon') !== -1 || search.indexOf('sbs-') !== -1);
        if (isYatoonPage) {
          window.YatoonToast('error', message);
          return;
        }
        nativeAlert(message);
      };
    }

    document.addEventListener('click', function(event) {
      if (!event.target || !event.target.classList || !event.target.classList.contains('notice-dismiss')) {
        return;
      }
      var notice = event.target.closest('.notice');
      if (notice && notice.textContent.trim()) {
        window.YatoonToast('info', notice.textContent.trim(), 'Notice dismissed');
      }
    });

    document.addEventListener('keydown', function(event) {
      if (event.key !== 'Escape') {
        return;
      }
      var modal = document.querySelector('#sbs-category-modal, #sbs-copy-addon-modal, #sbs-service-modal');
      if (modal && window.getComputedStyle(modal).display !== 'none') {
        var close = modal.querySelector('button[id$="-close"], button[id$="-cancel"], .sbs-text-modal-close');
        if (close) {
          close.click();
        }
      }
    });

    document.querySelectorAll('#sbs-category-modal, #sbs-copy-addon-modal, #sbs-service-modal').forEach(function(modal) {
      modal.setAttribute('role', 'dialog');
      modal.setAttribute('aria-modal', 'true');
    });

    document.documentElement.setAttribute('data-yatoon-toast-ready', '1');
  }

  if (document.body) {
    installToast();
  } else {
    document.addEventListener('DOMContentLoaded', installToast, { once: true });
  }
})();
