import{r as c}from"./solana-DqgBswAh.js";function C(t){const[a,u]=c.useState(void 0),[E,m]=c.useState(!0),[n,i]=c.useState(null),h=c.useCallback(async()=>{try{m(!0),i(null);const s=(await chrome.storage.local.get(t))[t];u(s)}catch(e){i(e instanceof Error?e:new Error(`Failed to load ${t}`)),u(void 0)}finally{m(!1)}},[t]),I=c.useCallback(async e=>{try{await chrome.storage.local.set({[t]:e}),u(e),i(null)}catch(s){const d=s instanceof Error?s:new Error(`Failed to save ${t}`);throw i(d),d}},[t]);return c.useEffect(()=>{h();const e=(s,d)=>{if(d==="local"&&s[t]){const o=s[t].newValue;u(o)}};return chrome.storage.onChanged.addListener(e),()=>{chrome.storage.onChanged.removeListener(e)}},[h]),{data:a,isLoading:E,error:n,setData:I,reloadData:h}}function b(t,a){const{data:u,isLoading:E,error:m,setData:n,reloadData:i}=C(t),h=u??[],I=c.useCallback(async o=>{try{const l=(await chrome.storage.local.get(t))[t]??[];if(l.some(w=>w[a]===o[a]))return;await n([...l,o])}catch(r){throw r instanceof Error?r:new Error(`Failed to add item to ${t}`)}},[t,n,a]),e=c.useCallback(async o=>{try{const w=((await chrome.storage.local.get(t))[t]??[]).map(f=>f[a]===o[a]?{...o}:f);await n(w)}catch(r){throw r instanceof Error?r:new Error(`Failed to update item in ${t}`)}},[t,n,a]),s=c.useCallback(async o=>{try{const w=((await chrome.storage.local.get(t))[t]??[]).filter(f=>f[a]!==o);await n(w)}catch(r){throw r instanceof Error?r:new Error(`Failed to delete item from ${t}`)}},[t,n,a]),d=c.useCallback(async o=>{try{await n(o)}catch(r){throw r instanceof Error?r:new Error(`Failed to set items in ${t}`)}},[t,n]);return{items:h,isLoading:E,error:m,addItem:I,updateItem:e,deleteItem:s,setItems:d,reloadItems:i}}export{b as a,C as u};
