(()=>{ 'use strict'; const version=new URL(document.currentScript.src).searchParams.get('v')||'1'; const url=new URL('../help/knowledge-v1.json',document.currentScript.src);url.searchParams.set('v',version); const esc=s=>String(s??'').replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c])); const norm=s=>String(s).toLowerCase().replace(/ё/g,'е'); const BOT={origin:"https://ai-staff-alpha.vercel.app",agent:"e9cc0f28-aaa5-48d4-a020-ae9633988faf"}; let dialog,data,loading=false,returnFocus; const supportUrl=new URL('support-form.js?v=20260921-support-mail',document.currentScript.src); function loadContactForm(){ if(window.CateriumSupportForm){window.CateriumSupportForm.attach(dialog);return;} if(document.getElementById('ctSupportFormScript'))return; const script=document.createElement('script');script.id='ctSupportFormScript';script.src=supportUrl.href; script.onerror=()=>{script.remove();};document.head.append(script); } function render(){ if(!data||!dialog)return; const query=norm(dialog.querySelector('#ctHelpSearch').value).trim(),category=dialog.querySelector('#ctHelpCategory').value; const terms=query.split(/\s+/).filter(Boolean); const rows=data.articles.filter(a=>!category||a.category===category).map(a=>({a,score:terms.reduce((n,t)=>n+(norm(a.title+' '+a.keywords).includes(t)?3:0)+(norm(a.steps.join(' ')+' '+a.note).includes(t)?1:0),0)})).filter(r=>!terms.length||r.score>0).sort((a,b)=>b.score-a.score); dialog.querySelector('#ctHelpStatus').textContent=rows.length?`Инструкций: ${rows.length}`:'Ничего не найдено. Попробуйте «оплата», «PDF», «склад» или откройте все темы.'; dialog.querySelector('#ctHelpResults').innerHTML=rows.map(({a})=>`
${esc(a.title)}${esc(a.category)}
    ${a.steps.map(s=>`
  1. ${esc(s)}
  2. `).join('')}

${esc(a.note)}

`).join(''); } function showGuide(query=''){ switchMode(false);dialog.querySelector('#ctHelpSearch').value=query;dialog.querySelector('#ctHelpCategory').value='';render();dialog.querySelector('#ctHelpSearch').focus(); } function startBot(){ const box=dialog.querySelector("#ctHelpBot"),start=dialog.querySelector("#ctHelpBotStart"); if(box.querySelector("iframe"))return; // The chat lives in a sandboxed iframe on the assistant's own origin: nothing from the app (orders, clients, session) is reachable from it, and no request leaves the device until the user asks for the assistant. const frame=document.createElement("iframe"); frame.src=`${BOT.origin}/embed/${BOT.agent}`;frame.title="ИИ-помощник Caterium";frame.loading="eager";frame.referrerPolicy="no-referrer"; frame.setAttribute("sandbox","allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox"); box.textContent="";box.hidden=false;box.appendChild(frame);start.hidden=true; } function switchMode(support){ dialog.querySelector('#ctHelpGuide').hidden=support;dialog.querySelector('#ctHelpSupport').hidden=!support; dialog.querySelector('#ctHelpGuideTab').setAttribute('aria-pressed',String(!support));dialog.querySelector('#ctHelpSupportTab').setAttribute('aria-pressed',String(support)); } async function load(){ if(data){render();return;}if(loading)return;loading=true; const status=dialog.querySelector('#ctHelpStatus'),retry=dialog.querySelector('#ctHelpRetry');status.textContent='Загружаю руководство…';retry.hidden=true; const controller=new AbortController(),timer=setTimeout(()=>controller.abort(),8000); try{ const r=await fetch(url,{signal:controller.signal,credentials:'omit'});if(!r.ok)throw new Error('http');const value=await r.json(); if(!Array.isArray(value.articles)||!value.articles.every(a=>typeof a.id==='string'&&typeof a.title==='string'&&typeof a.category==='string'&&Array.isArray(a.steps)&&a.steps.every(s=>typeof s==='string')))throw new Error('format'); data=value;dialog.querySelector('#ctHelpCategory').innerHTML=''+[...new Set(data.articles.map(a=>a.category))].map(c=>``).join(''); dialog.querySelector('#ctHelpEdition').textContent=`Руководство · редакция ${data.version}`;render(); }catch(_){status.textContent='Не удалось загрузить руководство. Проверьте соединение и повторите.';retry.hidden=false;}finally{loading=false;clearTimeout(timer);} } function ensureDialog(){ if(dialog)return; dialog=document.createElement('dialog');dialog.id='ctHelpDialog';dialog.setAttribute('aria-labelledby','ctHelpTitle'); dialog.innerHTML=`

Помощь в Caterium

Руководство пользователя и поддержка

`; document.body.appendChild(dialog); dialog.querySelector('#ctHelpClose').onclick=()=>dialog.close(); dialog.addEventListener('keydown',e=>{if(e.key==='Escape'){e.preventDefault();e.stopPropagation();dialog.close();}}); dialog.addEventListener('close',()=>{dialog.querySelector('#ctHelpSearch').value='';if(returnFocus?.isConnected)returnFocus.focus({preventScroll:true});}); dialog.querySelector('#ctHelpBotStart').onclick=startBot;dialog.querySelector('#ctHelpGuideTab').onclick=()=>switchMode(false);dialog.querySelector('#ctHelpSupportTab').onclick=()=>switchMode(true); dialog.querySelector('#ctHelpSearch').oninput=render;dialog.querySelector('#ctHelpCategory').onchange=render; dialog.querySelector('#ctHelpReset').onclick=()=>showGuide();dialog.querySelector('#ctHelpRetry').onclick=load; dialog.addEventListener('click',e=>{ const q=e.target.closest('[data-help-query]');if(q){showGuide(q.dataset.helpQuery);return;} const a=e.target.closest('[data-help-article]');if(a&&data){showGuide();const target=[...dialog.querySelectorAll('details')].find(x=>x.dataset.article===a.dataset.helpArticle);if(target){target.open=true;target.scrollIntoView({block:'nearest'});target.querySelector('summary').focus();}} }); } function open(trigger){ensureDialog();loadContactForm();dialog.querySelector('#ctContactSection')?.setAttribute('hidden','');dialog.querySelector('#ctContactTab')?.setAttribute('aria-pressed','false');returnFocus=trigger?.currentTarget||trigger||document.activeElement;switchMode(false);if(!dialog.open)dialog.showModal();load();dialog.querySelector('#ctHelpSearch').focus();} function mount(){ const nav=document.querySelector('header nav'); if(nav&&!document.getElementById('ctHelpNav')){const b=document.createElement('button');b.id='ctHelpNav';b.type='button';b.dataset.navLabel='Помощь';b.textContent='Помощь';b.onclick=open;nav.appendChild(b);window.sunRegisterNavButton?.({button:b,group:'management',navLabel:'Помощь'});} const gate=document.getElementById('sunCloudAuthGateV3'); if(gate&&!gate.querySelector('.ct-help-login')){const b=document.createElement('button');b.type='button';b.className='ct-help-login';b.textContent='Помощь со входом';b.onclick=()=>{open(b);showGuide('вход');};(gate.querySelector('.sun-cloud-auth-card')||gate).appendChild(b);} } function boot(){mount();let frame=0;new MutationObserver(()=>{if(frame)return;frame=requestAnimationFrame(()=>{frame=0;mount();});}).observe(document.body,{childList:true,subtree:true}); const context=()=>`${window.SunCloudV2?.getSession?.()?.user?.id||''}:${window.SunCloudV2?.getWorkspace?.()?.id||''}`; let current=context();window.addEventListener('sun:cloud-permissions-changed',()=>{const next=context();if(next!==current&&dialog?.open)dialog.close();current=next;}); } if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',boot,{once:true});else boot(); })();