'use strict';
// Shared feedback for Home, Inbox and Super Admin.
window.HubToast=(()=>{
  const active=new Map();let host, root;
  const toneIcons={error:'alert-circle',success:'circle-check',warning:'alert-triangle',info:'info-circle'};
  const buttonClasses='shrink-0 cursor-pointer rounded-md border-0 bg-transparent p-1 hover:bg-surface';
  const iconColors={error:'text-danger',success:'text-good',warning:'text-accent',info:'text-accent'};
  function container(){
    if(!host){
      host=document.createElement('div');
      host.className='hub-toasts pointer-events-none fixed z-[10020] grid w-[min(380px,calc(100vw-32px))] gap-2.5 '+
        'font-hub text-[13px] font-normal leading-[1.45]';
      root=ReactDOM.createRoot(host);
      host.setAttribute('role','region');
      host.setAttribute('aria-label','Notifications');
    }
    const parent=document.querySelector('dialog[open]')||document.body;
    if(host.parentNode!==parent)parent.appendChild(host);
    return host;
  }
  function paint() {
    root.render([...active.values()].map(entry => {
      const {id, tone, options} = entry;
      return (
        <div key={id} role={tone === 'error' ? 'alert' : 'status'}
          className="pointer-events-auto flex min-w-0 items-start gap-2.5 rounded-xl border border-solid border-edge bg-canvas p-3 text-ink shadow-hub-popup"
          onPointerEnter={() => { entry.hovered = true; entry.pause(); }}
          onPointerLeave={() => { entry.hovered = false; entry.resume(); }}
          onFocus={() => { entry.focused = true; entry.pause(); }}
          onBlur={event => {
            if (!event.currentTarget.contains(event.relatedTarget)) { entry.focused = false; entry.resume(); }
          }}>
          <i aria-hidden="true" className={HubIcons.className(toneIcons[tone]) + ' ' + iconColors[tone] +
            ' grid size-6 shrink-0 place-items-center rounded-full bg-[color:color-mix(in_srgb,currentColor_12%,var(--bg))] [font-weight:var(--toast-icon-weight,400)]'} />
          <span className="min-w-0 flex-1 pt-0.5 [overflow-wrap:anywhere]">{entry.message}</span>
          {options.action && <button type="button" className={buttonClasses+' text-accent [font:inherit] phone:min-h-9'}
            onClick={() => { remove(id); options.action.onClick(); }}>{options.action.label}</button>}
          <button type="button" aria-label="Dismiss notification" onClick={() => remove(id)}
            className={buttonClasses+' -mt-0.5 grid size-7 place-items-center text-muted [font-family:inherit] text-xl font-normal leading-none phone:size-9'}>
            <i className={HubIcons.className('x')} aria-hidden="true" />
          </button>
        </div>
      );
    }));
  }
  function remove(id,notify=true){
    const entry=active.get(id);if(!entry)return;
    clearTimeout(entry.timer);active.delete(id);paint();
    if(notify)entry.onDismiss?.();
  }
  function show(message,options={}){
    if(!String(message||'').trim())return null;
    const tone=['error','success','warning','info'].includes(options.tone)?options.tone:'info';
    const id=options.id||tone+':'+String(message);
    remove(id,false);
    while(active.size>=3)remove(active.keys().next().value);
    const entry={id,tone,message:String(message),options,onDismiss:options.onDismiss,remaining:options.duration??6000,timer:null,start:0,hovered:false,focused:false};
    const pause=()=>{if(entry.timer){clearTimeout(entry.timer);entry.timer=null;entry.remaining-=Date.now()-entry.start;}};
    const resume=()=>{if(entry.remaining>0&&!entry.timer&&!entry.hovered&&!entry.focused){entry.start=Date.now();entry.timer=setTimeout(()=>remove(id),entry.remaining);}};
    entry.pause=pause;entry.resume=resume;
    active.set(id,entry);container();paint();resume();
    return id;
  }
  function clear(){for(const id of [...active.keys()])remove(id);}
  document.addEventListener('close',()=>{if(host&&document.body)container();},true);
  return {show,error:(message,options)=>show(message,{...options,tone:'error'}),
    success:(message,options)=>show(message,{...options,tone:'success'}),
    info:(message,options)=>show(message,{...options,tone:'info'}),dismiss:remove,clear};
})();
