import{b as l}from"./v_7_4_2_9_8cb79d4f-81e0-49c1-b597-7f2344477fbf19.js";import{g,W as P,R as k}from"./v_7_4_2_9_8cb79d4f-81e0-49c1-b597-7f2344477fbf10.js";import{c as S}from"./v_7_4_2_9_8cb79d4f-81e0-49c1-b597-7f2344477fbf7.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,h=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:h});break;case"encomendas-backup-update":b();break}});const i=S()((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?P("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?P("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?P("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":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")}})),E=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})}),E(c)},b=async()=>{const n=await k("encomendas")||[];i.setState({encomendas:n})};export{v as M,E as R,b as i,i as u};
