/* ─── Demo action feedback ───────────────────────────────────────────────────
   window.rbToast(msg) — slide-in toast. A delegated click handler catches
   buttons that have no real behavior yet and confirms the action instead of
   staying silent. Loads last. */

(function(){
  const el = document.createElement('style');
  el.textContent = `
    #rb-toasts{position:fixed;bottom:24px;left:50%;transform:translateX(-50%);z-index:9999;display:flex;flex-direction:column;gap:8px;align-items:center;pointer-events:none}
    .rb-toast{display:flex;align-items:center;gap:9px;background:#101826;color:#fff;font-size:13px;font-weight:600;padding:11px 18px;border-radius:100px;box-shadow:0 8px 30px rgba(0,0,0,.25);animation:rbtIn .25s cubic-bezier(.2,.9,.3,1.2)}
    .rb-toast svg{flex-shrink:0}
    @keyframes rbtIn{from{opacity:0;transform:translateY(14px) scale(.95)}to{opacity:1;transform:none}}
    .rb-toast.out{transition:opacity .3s, transform .3s;opacity:0;transform:translateY(8px)}
  `;
  document.head.appendChild(el);
  const host = document.createElement('div');
  host.id = 'rb-toasts';
  document.body.appendChild(host);

  window.rbToast = (msg, ok=true) => {
    const t = document.createElement('div');
    t.className = 'rb-toast';
    t.innerHTML = (ok
      ? '<svg width="15" height="15" viewBox="0 0 24 24" fill="none"><circle cx="12" cy="12" r="10" fill="#1FB866"/><path d="M8 12.5l2.8 2.8L16.5 9" stroke="white" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"/></svg>'
      : '<svg width="15" height="15" viewBox="0 0 24 24" fill="none"><circle cx="12" cy="12" r="10" fill="#64748B"/><path d="M12 8v5M12 16.5v.5" stroke="white" stroke-width="2.2" stroke-linecap="round"/></svg>')
      + '<span></span>';
    t.querySelector('span').textContent = msg;
    host.appendChild(t);
    setTimeout(()=>{ t.classList.add('out'); setTimeout(()=>t.remove(), 350); }, 2400);
  };

  /* map of button-label patterns → toast copy for demo-only actions */
  const DEMO_ACTIONS = [
    [/^Export CSV$/i,            'Export started — CSV will download shortly (demo)'],
    [/^Download CSV$/i,          'Invoice history exported (demo)'],
    [/^Download PNG$/i,          'QR code PNG downloaded (demo)'],
    [/^Choose (Starter|Growth|Pro|Enterprise)/i, m => `${m[1]} plan selected — checkout would open here (demo)`],
    [/^Add card$/i,              'Card form would open here (demo)'],
    [/^Duplicate$/i,             'Promotion duplicated (demo)'],
    [/^Delete$/i,                'Deleted (demo)'],
    [/^Delete campaign$/i,       'Campaign deleted (demo)'],
    [/^Publish Product$/i,       'Product published 🎉 (demo)'],
    [/^Create Promotion$/i,      'Promotion created 🎉 (demo)'],
    [/^Add Brand$/i,             'Brand added (demo)'],
    [/^Save changes$/i,          'Changes saved'],
    [/^Update password$/i,       'Password updated'],
    [/^Logout$/i,                'Logged out (demo)'],
    [/^Claim My/i,               'This is the customer-facing claim button (preview)'],
  ];

  document.addEventListener('click', (e) => {
    const btn = e.target.closest('button');
    if (!btn || btn.disabled) return;
    const label = btn.textContent.trim();
    for (const [re, msg] of DEMO_ACTIONS) {
      const m = label.match(re);
      if (m) {
        /* skip buttons that already do something visible (deliver, wizard nav, toggles…) */
        if (btn.closest('[data-rb-live]')) return;
        const out = typeof msg === 'function' ? msg(m) : msg;
        setTimeout(() => window.rbToast(out), 60);
        return;
      }
    }
  }, true);
})();
