import{p as f,f as m,C as g}from"./v_7_4_3_7_e8fda66a-fa64-447c-bcdc-42ba13285e7f20.js";import{am as C,at as p}from"./v_7_4_3_7_e8fda66a-fa64-447c-bcdc-42ba13285e7f23.js";import{r as A}from"./v_7_4_3_7_e8fda66a-fa64-447c-bcdc-42ba13285e7f11.js";import{c as l}from"./v_7_4_3_7_e8fda66a-fa64-447c-bcdc-42ba13285e7f8.js";var d=(e=>(e.DRAFT="Rascunho",e.PAUSED="Pausada",e.SENDING="Enviando",e.SENT="Enviada",e.FAILED="Falhou",e))(d||{});const I={interval:{seconds:10},pause:{every:10,durationSeconds:60},signature:{disabled:!1,useCustomName:!1,customName:void 0}};function D(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type==="Date"&&"value"in e&&typeof e.value=="string"}function E(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type==="Map"&&"value"in e&&Array.isArray(e.value)}var w=(e=>(e.IDLE="idle",e.RUNNING="running",e.PAUSED="paused",e.CANCELLED="cancelled",e.COMPLETED="completed",e.FAILED="failed",e))(w||{});const M=l()(f((e,r)=>({executions:new Map,startExecution:(t,a,s,n)=>{e(o=>{const i=new Map(o.executions);return i.set(t,{campaignId:t,status:"running",type:n,currentContactIndex:0,currentActionIndex:0,totalContacts:a,totalActions:s,successContacts:[],failedContacts:[],startedAt:new Date,shouldPause:!1,shouldCancel:!1}),{executions:i}})},updateProgress:(t,a,s,n)=>{e(o=>{const i=new Map(o.executions),c=i.get(t);return c&&i.set(t,{...c,currentContactIndex:a,successContacts:s?[...c.successContacts,n]:c.successContacts,failedContacts:s?c.failedContacts:[...c.failedContacts,n]}),{executions:i}})},requestPause:t=>{e(a=>{const s=new Map(a.executions),n=s.get(t);return n&&n.status==="running"&&s.set(t,{...n,shouldPause:!0}),{executions:s}})},requestCancel:t=>{e(a=>{const s=new Map(a.executions),n=s.get(t);return n&&n.status!=="completed"&&s.set(t,{...n,shouldCancel:!0}),{executions:s}})},forceCancel:t=>{e(a=>{const s=new Map(a.executions);return s.delete(t),{executions:s}})},resume:t=>{e(a=>{const s=new Map(a.executions),n=s.get(t);return n&&n.status==="paused"&&s.set(t,{...n,status:"running",shouldPause:!1}),{executions:s}})},shouldPauseExecution:t=>r().executions.get(t)?.shouldPause||!1,shouldCancelExecution:t=>r().executions.get(t)?.shouldCancel||!1,completeExecution:t=>{e(a=>{const s=new Map(a.executions),n=s.get(t);return n&&s.set(t,{...n,status:"completed",completedAt:new Date}),{executions:s}})},failExecution:(t,a)=>{e(s=>{const n=new Map(s.executions),o=n.get(t);return o&&n.set(t,{...o,status:"failed",completedAt:new Date}),{executions:n}})},clearExecution:t=>{e(a=>{const s=new Map(a.executions);return s.delete(t),{executions:s}})}}),{name:"campaign-execution-store",storage:m(()=>g("CampaignExecutionDB","campaignExecutionStore"),{replacer:(e,r)=>r instanceof Date?{__type:"Date",value:r.toISOString()}:r instanceof Map?{__type:"Map",value:Array.from(r.entries())}:r,reviver:(e,r)=>D(r)?new Date(r.value):E(r)?new Map(r.value):r}),version:2,migrate:(e,r)=>r===1?{executions:new Map}:e,partialize:e=>({executions:e.executions}),onRehydrateStorage:()=>e=>{if(e){const r=new Map(e.executions),t=y.getState();r.forEach((a,s)=>{const n=t.campaigns.find(o=>o.id===s);a.status==="running"&&(r.set(s,{...a,status:"paused",isAutoPause:!1,autoPauseEndsAt:void 0,pausedAt:new Date}),n&&n.status===d.SENDING&&t.updateCampaign(s,{status:d.PAUSED})),a.status==="paused"&&a.isAutoPause===!0&&r.set(s,{...a,isAutoPause:!1,autoPauseEndsAt:void 0})}),e.executions=r}}})),y=l()(f((e,r)=>({currentCampaignId:"",setCurrentCampaignId:t=>e({currentCampaignId:t}),campaigns:[],addCampaign:t=>{if(r().campaigns.some(n=>n.id===t.id)){console.warn(`Campanha com ID ${t.id} já existe`);return}e(n=>({campaigns:[...n.campaigns,{...t,createdAt:t.createdAt||new Date,updatedAt:t.updatedAt||new Date}]}))},updateCampaign:(t,a)=>e(s=>({campaigns:s.campaigns.map(n=>n.id===t?{...n,...a,updatedAt:new Date}:n)})),updateConfig:(t,a)=>e(s=>({campaigns:s.campaigns.map(n=>n.id===t?{...n,config:{...n.config,...a},updatedAt:new Date}:n)})),removeCampaign:t=>{M.getState().clearExecution(t),e(a=>a.campaigns.some(n=>n.id===t)?{campaigns:a.campaigns.filter(n=>n.id!==t),currentCampaignId:a.currentCampaignId===t?"":a.currentCampaignId}:(console.warn(`Campanha com ID ${t} não encontrada`),a))},clearCampaigns:()=>e({campaigns:[]}),setQuickReplyAction:t=>{const{currentCampaignId:a,campaigns:s}=r();if(!a||!s.find(u=>u.id===a))return;const{getAction:o}=C.getState(),{acao:i}=p.getState(),x=(Array.isArray(t.script)&&t.type==="script"?t.script.flatMap(u=>o(u.id)??[]):o(t.id)??[]).map(u=>({...u,id:A()}));p.setState({acao:[...i,...x]})}}),{name:"useSendBulkMessage-store",storage:m(()=>g("SendBulkMessageDB","useSendBulkMessageStore"),{reviver:(e,r)=>{if(typeof r=="string"&&/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/.test(r)){const t=new Date(r);return isNaN(t.getTime())?r:t}return r}}),version:1,partialize:e=>({campaigns:e.campaigns,currentCampaignId:e.currentCampaignId})}));export{d as C,w as E,y as a,I as d,M as u};
