import{a as W,r as y,W as M}from"./v_7_4_3_73_d3c9bac7-c74e-47f6-ac42-499f067843a54.js";import"./v_7_4_3_73_d3c9bac7-c74e-47f6-ac42-499f067843a55.js";import{c as S}from"./v_7_4_3_73_d3c9bac7-c74e-47f6-ac42-499f067843a52.js";const v=()=>[{type:"aoReceberMensagem",data:{type:"aoReceberMensagem",active:!0}},{type:"porPalavraChave",data:{type:"porPalavraChave",active:!1,chaves:[]}},{type:"primeiraMensagemDoClienteNoDia",data:{type:"primeiraMensagemDoClienteNoDia",active:!1}},{type:"primeiraMensagemDoCliente",data:{type:"primeiraMensagemDoCliente",active:!1}},{type:"apenasPorAutomacao",data:{type:"apenasPorAutomacao",active:!1}}],k=f=>{const l=new Map((f.acionamentos||[]).map(e=>[e.type,e]));return{...f,acionamentos:v().map(e=>({...e,...l.get(e.type),data:{...e.data,...l.get(e.type)?.data}}))}},p=S((f,l)=>({workflows:[],currentWorkflow:null,saveToStorage:e=>{M("fluxo",e)},getSourceNode:(e,t)=>{const r=l().workflows.find(n=>n.id===t);if(!r)return null;const o=r.edges.find(n=>n.target===e);if(!o)return null;const s=r.nodes.find(n=>n.id===o.source);return s||null},getTargetNode:(e,t)=>{const r=l().workflows.find(n=>n.id===t);if(!r)return null;const o=r.edges.find(n=>n.source===e&&n.sourceHandle!=="timeout");if(!o)return null;const s=r.nodes.find(n=>n.id===o.target);return s||null},getAllTargetNodes:(e,t)=>{const r=l().workflows.find(n=>n.id===t);if(!r)return[];const o=r.edges.filter(n=>n.source===e);return o.length?o.map(n=>r.nodes.find(c=>c.id===n.target)).filter(Boolean):[]},getTargetNodeByBestMatch:(e,t,r)=>{const o=l().workflows.find(a=>a.id===t);if(!o)return null;const s=o.nodes.find(a=>a.id===e);if(!s||s.type!=="send-answers")return null;const n=o.edges.filter(a=>a.source===e);if(!n.length)return null;const c=n.find(a=>parseInt(a.sourceHandle?.split("-")?.pop()||"",10)===r);if(!c)return null;const w=o.nodes.find(a=>a.id===c.target);return w||null},getTargetNodeByRowId:(e,t,r)=>{const o=l().workflows.find(d=>d.id===t);if(!o)return null;const s=o.nodes.find(d=>d.id===e);if(!s||s.type!=="send-list")return null;const n=s?.data;if(!n||!n?.sections)return null;const c=n.sections.flatMap(d=>d.rows).find(d=>d.rowId===r);if(!c)return null;const w=o.edges.find(d=>d.source===e&&d.sourceHandle===`on-response-${c.rowId}`);if(!w)return null;const a=o.nodes.find(d=>d.id===w.target);return a||null},getRetryQuestion:e=>{const{noMatch:t}=e;return t.sendPersonalizedMessage.active&&t.sendPersonalizedMessage.personalizedMessage||t.endPersonalizedMessage.active&&t.endPersonalizedMessage.personalizedMessage?t.sendPersonalizedMessage.active?t.sendPersonalizedMessage.personalizedMessage:t.endPersonalizedMessage.personalizedMessage:e.question},initNodeIsConnected:e=>{const t=e.nodes.find(o=>o.type==="init");return t?e.edges.some(o=>o.source===t.id):!1},getVariableByNodeId:(e,t)=>{const r=l().workflows.find(s=>s.id===t);if(!r)return;const o=r.variaveis.find(s=>s.nodeId===e);if(o)return o},updateVariableAsync:async(e,t,r,o)=>new Promise(s=>{f(n=>{const c=n.workflows.map(a=>{if(a.id!==r)return a;const d=a.variaveis.map(i=>{if(i.nodeId!==t)return i;const m=i.clients.some(g=>g.userId===e)?i.clients.map(g=>g.userId===e?{...g,value:o}:g):[...i.clients,{userId:e,value:o}];return{...i,clients:m}});return{...a,variaveis:d}}),w=n.currentWorkflow&&n.currentWorkflow.id===r&&c.find(a=>a.id===r)||n.currentWorkflow;return l().saveToStorage({workflows:c,currentWorkflow:w}),setTimeout(()=>s(),0),{workflows:c,currentWorkflow:w}})}),delay:e=>new Promise(t=>setTimeout(t,e))}));async function x(){const l=((await W("fluxo")||{}).workflows||[]).map(e=>{const t=k(e);return t.globalSettings?.blockTransitionDelay?t:{...t,globalSettings:{...t.globalSettings,blockTransitionDelay:{value:2,unit:"segundos"}}}});p.setState({workflows:l,currentWorkflow:null})}function z(f){const{workflows:l,currentWorkflow:e}=f,t=p.getState(),r=new Set(t.workflows.map(i=>i.id)),o=new Set(t.workflows.map(i=>i.name)),s=new Map,n=()=>{let i;do i=y();while(r.has(i));return r.add(i),i},c=i=>{let u=i,m=1;for(;o.has(u);)u=`${i} (${m})`,m++;return o.add(u),u},w=[...t.workflows];l.forEach(i=>{let u=k({...i});const m=u.id;if(r.has(u.id)?u.id=n():r.add(u.id),s.set(m,u.id),o.has(u.name)){const g=`${u.name} (backup)`;u.name=c(g)}else o.add(u.name);w.push(u)});let a=t.currentWorkflow;if(e){const i=s.get(e.id)??e.id;a=k({...e,id:i})}const d={workflows:w,currentWorkflow:a};p.setState(d),p.getState().saveToStorage(d)}function I(f){const{workflows:l,currentWorkflow:e}=f,t={workflows:(l||[]).map(k),currentWorkflow:e?k(e):null};p.setState(t),p.getState().saveToStorage(t)}chrome.storage.onChanged.addListener((f,l)=>{if(f.fluxo?.newValue){const e=f.fluxo.newValue;p.setState({workflows:(e.workflows||[]).map(k),currentWorkflow:e.currentWorkflow?k(e.currentWorkflow):null})}});export{z as M,I as R,x as g,p as u};
