import{b as l}from"./v_7_4_1_21_73d2acb1-b7f3-47a2-a8fe-3772b225080919.js";import{a as g,W as h,R as S}from"./v_7_4_1_21_73d2acb1-b7f3-47a2-a8fe-3772b225080910.js";import{c as b}from"./v_7_4_1_21_73d2acb1-b7f3-47a2-a8fe-3772b22508097.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":E();break}});const i=b()((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")}})),k=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})}),k(c)},E=async()=>{const d=await S("encomendas")||[];i.setState({encomendas:d})};export{v as M,k as R,E as i,i as u};
