import{p as f,j as g,k as l,r as C}from"./v_7_4_3_28_345c6a4a-aab6-4ec9-a0c6-3718e7fdb1164.js";import{ae as A,ak as p}from"./v_7_4_3_28_345c6a4a-aab6-4ec9-a0c6-3718e7fdb11623.js";import{b as m}from"./v_7_4_3_28_345c6a4a-aab6-4ec9-a0c6-3718e7fdb116.js";var d=(e=>(e.DRAFT="Rascunho",e.PAUSED="Pausada",e.SENDING="Enviando",e.SENT="Enviada",e.FAILED="Falhou",e))(d||{});const N={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=m()(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:g(()=>l("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=m()(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}=A.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:C()}));p.setState({acao:[...i,...x]})}}),{name:"useSendBulkMessage-store",storage:g(()=>l("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,N as d,M as u};
