import{b as l}from"./v_7_4_2_27_fac7f92f-33f6-4d26-8a6f-b87b9638d3cc20.js";import{g,W as h,R as k}from"./v_7_4_2_27_fac7f92f-33f6-4d26-8a6f-b87b9638d3cc11.js";import{c as b}from"./v_7_4_2_27_fac7f92f-33f6-4d26-8a6f-b87b9638d3cc8.js";chrome.runtime.onMessage.addListener(n=>{const{encomendas:c}=i.getState();switch(n.action){case"addProduct":const{userID:e,product:t}=n.dados,a=c.find(o=>o.userID===e);let d=[];a?d=c.map(o=>(o.userID===e&&(o.product=[...o.product,t]),o)):d=[...c,{userID:e,product:[t]}],i.setState({encomendas:d});break;case"editProduct":const{productId:r,updatedProduct:s}=n.dados,u=c.map(o=>{if(o.userID===e){const f=o.product.map(p=>p.id===r?{...p,...s}:p);return{...o,product:f}}return o});i.setState({encomendas:u});break;case"removeProduct":const{productId:m}=n.dados,P=c.map(o=>{if(o.userID===e){const f=o.product.filter(p=>p.id!==m);return{...o,product:f}}return o});i.setState({encomendas:P});break;case"encomendas-backup-update":E();break}});const i=b()((n,c)=>({encomendas:[],getProduct:e=>{const t=i.getState().encomendas.find(a=>a.userID===e);return t?t.product:[]},addProduct:(e,t)=>n(a=>{const d=a.encomendas.find(s=>s.userID===e);let r=[];return d?r=a.encomendas.map(s=>(s.userID===e&&(s.product=[...s.product,t]),s)):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)=>n(d=>{const r=d.encomendas.map(s=>{if(s.userID===e){const u=s.product.map(m=>m.id===t?{...m,...a}:m);return{...s,product:u}}return s});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)=>n(a=>{const d=a.encomendas.map(r=>{if(r.userID===e){const s=r.product.filter(u=>u.id!==t);return{...r,product:s}}return r});return g("encomendas",d),l.getState().plugin_page?h("removeProduct",{userID:e,productId:t},!1):chrome.runtime.sendMessage({action:"removeProduct",dados:{userID:e,productId:t}}),{encomendas:d}}),openLink:e=>{let t="";switch(e.transportadora){case"Correios":t=`https://www.linkcorreios.com.br/?id=${e.codigo}`;break;case"Jadlog":t=`https://t.17track.net/pt#nums=${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")}})),S=async n=>{i.setState({encomendas:n}),await g("encomendas",n),chrome.runtime.sendMessage({action:"encomendas-backup-update"})},v=n=>{const c=[...i.getState().encomendas];n.forEach(({userID:e,product:t})=>{const a=c.findIndex(d=>d.userID===e);a!==-1?t.forEach(d=>{c[a].product.findIndex(s=>s.id===d.id)===-1&&c[a].product.push(d)}):c.push({userID:e,product:t})}),S(c)},E=async()=>{const n=await k("encomendas")||[];i.setState({encomendas:n})};export{v as M,S as R,E as i,i as u};
