import{c as l}from"./v_7_4_0_1_4b8cc392-a410-45f6-b662-85abdad9c03e19.js";import{a as g,W as h,R as S}from"./v_7_4_0_1_4b8cc392-a410-45f6-b662-85abdad9c03e10.js";import{c as k}from"./v_7_4_0_1_4b8cc392-a410-45f6-b662-85abdad9c03e7.js";chrome.runtime.onMessage.addListener(d=>{const{encomendas:c}=i.getState();switch(d.action){case"addProduct":const{userID:e,product:t}=d.dados,a=c.find(s=>s.userID===e);let n=[];a?n=c.map(s=>(s.userID===e&&(s.product=[...s.product,t]),s)):n=[...c,{userID:e,product:[t]}],i.setState({encomendas:n});break;case"editProduct":const{productId:r,updatedProduct:o}=d.dados,u=c.map(s=>{if(s.userID===e){const f=s.product.map(p=>p.id===r?{...p,...o}:p);return{...s,product:f}}return s});i.setState({encomendas:u});break;case"removeProduct":const{productId:m}=d.dados,P=c.map(s=>{if(s.userID===e){const f=s.product.filter(p=>p.id!==m);return{...s,product:f}}return s});i.setState({encomendas:P});break;case"encomendas-backup-update":b();break}});const i=k()((d,c)=>({encomendas:[],getProduct:e=>{const t=i.getState().encomendas.find(a=>a.userID===e);return t?t.product:[]},addProduct:(e,t)=>d(a=>{const n=a.encomendas.find(o=>o.userID===e);let r=[];return n?r=a.encomendas.map(o=>(o.userID===e&&(o.product=[...o.product,t]),o)):r=[...a.encomendas,{userID:e,product:[t]}],g("encomendas",r),l.getState().plugin_page?h("addProduct",{userID:e,product:t},!1):chrome.runtime.sendMessage({action:"addProduct",dados:{userID:e,product:t}}),{encomendas:r}}),editProduct:(e,t,a)=>d(n=>{const r=n.encomendas.map(o=>{if(o.userID===e){const u=o.product.map(m=>m.id===t?{...m,...a}:m);return{...o,product:u}}return o});return g("encomendas",r),l.getState().plugin_page?h("editProduct",{userID:e,productId:t,updatedProduct:a},!1):chrome.runtime.sendMessage({action:"editProduct",dados:{userID:e,productId:t,updatedProduct:a}}),{encomendas:r}}),removeProduct:(e,t)=>d(a=>{const n=a.encomendas.map(r=>{if(r.userID===e){const o=r.product.filter(u=>u.id!==t);return{...r,product:o}}return r});return g("encomendas",n),l.getState().plugin_page?h("removeProduct",{userID:e,productId:t},!1):chrome.runtime.sendMessage({action:"removeProduct",dados:{userID:e,productId:t}}),{encomendas:n}}),openLink:e=>{let t="";switch(e.transportadora){case"Correios":case"Jadlog":t=`https://app.melhorrastreio.com.br/app/${e.codigo}`;break;case"Shopee":case"Aliexpress":case"Gear Best":case"China Post":t=`https://www.muambator.com.br/pacotes/${e.codigo}/detalhes/`;break;case"Loggi":t=`https://www.loggi.com/rastreador/${e.codigo}`;break;default:t=`https://parcelsapp.com/pt/tracking/${e.codigo}`;break}t!==""&&window.open(t,"_blank")}})),E=async d=>{i.setState({encomendas:d}),await g("encomendas",d),chrome.runtime.sendMessage({action:"encomendas-backup-update"})},v=d=>{const c=[...i.getState().encomendas];d.forEach(({userID:e,product:t})=>{const a=c.findIndex(n=>n.userID===e);a!==-1?t.forEach(n=>{c[a].product.findIndex(o=>o.id===n.id)===-1&&c[a].product.push(n)}):c.push({userID:e,product:t})}),E(c)},b=async()=>{const d=await S("encomendas")||[];i.setState({encomendas:d})};export{v as M,E as R,b as i,i as u};
