(function(){"use strict";const f=(...e)=>console.log("[ZV:RecurrenceSW]",...e),u="ZV:scheduleStore";function m(e){return e?e.state?.scheduleList||e.scheduleList||[]:[]}function S(e,r){const n=new Date(r),c=e.frequency||"daily";if(c==="daily"){const t=e.timesOfDay?.length?e.timesOfDay:[D(n)],s=t.findIndex(d=>d===D(n));if(s>=0&&s<t.length-1){const[d,p]=t[s+1].split(":").map(Number),g=new Date(n);return g.setHours(d,p||0,0,0),g}const o=new Date(n);o.setDate(o.getDate()+1);const[l,i]=(t[0]||"09:00").split(":").map(Number);return o.setHours(l,i||0,0,0),o}if(c==="weekly"){const t=new Date(n);return t.setDate(t.getDate()+7),t}if(c==="biweekly"){const t=new Date(n);return t.setDate(t.getDate()+14),t}if(c==="monthly"){const t=new Date(n);return t.setMonth(t.getMonth()+1),t}const a=new Date(n);return a.setDate(a.getDate()+1),a}function D(e){const r=String(e.getHours()).padStart(2,"0"),n=String(e.getMinutes()).padStart(2,"0");return`${r}:${n}`}function w(){return"zv-"+Date.now()+"-"+Math.random().toString(36).slice(2,9)}async function h(){const r=(await chrome.storage.local.get(u))[u],n=m(r);let c=!1;const a=[];for(const s of n){if(s.status==="sent"&&s.recurrenceRule?.enabled){const o=s.recurrenceRule,l=o.endDate?new Date(o.endDate+"T23:59:59"):null,i=S(o,s.scheduleDate);(!l||i<=l)&&(a.push({...s,id:w(),status:"scheduled",sentAt:null,scheduleDate:i.toISOString(),createdAt:new Date().toISOString()}),f("pr\xF3xima ocorr\xEAncia",s.id,"->",i.toISOString())),c=!0;continue}s.status!=="sent"&&a.push(s)}if(!c)return;const t=r?.state?{...r,state:{...r.state,scheduleList:a}}:{state:{scheduleList:a},version:0};await chrome.storage.local.set({[u]:t})}chrome.storage.onChanged.addListener((e,r)=>{r==="local"&&e[u]&&setTimeout(h,500)}),setInterval(h,5e3),f("recurrence worker ativo")})();
