import{W as g,d as f,e as h,a as k}from"./v_7_4_3_48_026858c9-0735-4edd-ae06-f5f59030b0285.js";import{d as S}from"./v_7_4_3_48_026858c9-0735-4edd-ae06-f5f59030b0282.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(o=>o.userID===e);let n=[];a?n=c.map(o=>(o.userID===e&&(o.product=[...o.product,t]),o)):n=[...c,{userID:e,product:[t]}],i.setState({encomendas:n});break;case"editProduct":const{productId:r,updatedProduct:s}=d.dados,u=c.map(o=>{if(o.userID===e){const l=o.product.map(p=>p.id===r?{...p,...s}:p);return{...o,product:l}}return o});i.setState({encomendas:u});break;case"removeProduct":const{productId:m}=d.dados,P=c.map(o=>{if(o.userID===e){const l=o.product.filter(p=>p.id!==m);return{...o,product:l}}return o});i.setState({encomendas:P});break;case"encomendas-backup-update":E();break}});const i=S()((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(s=>s.userID===e);let r=[];return n?r=a.encomendas.map(s=>(s.userID===e&&(s.product=[...s.product,t]),s)):r=[...a.encomendas,{userID:e,product:[t]}],g("encomendas",r),f.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(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),f.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 s=r.product.filter(u=>u.id!==t);return{...r,product:s}}return r});return g("encomendas",n),f.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":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")}})),b=async d=>{i.setState({encomendas:d}),await g("encomendas",d),chrome.runtime.sendMessage({action:"encomendas-backup-update"})},M=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(s=>s.id===n.id)===-1&&c[a].product.push(n)}):c.push({userID:e,product:t})}),b(c)},E=async()=>{const d=await k("encomendas")||[];i.setState({encomendas:d})};export{M,b as R,E as i,i as u};
