import{n as e}from"./chunk-B3pr0VN8.js";import{r as t}from"./appConfig-CuVl6yRs.js";import{r as n}from"./norm-CeclA1Tu.js";var r=e({getFavorites:()=>l,isFavorite:()=>d,removeFavorite:()=>p,setFavorites:()=>u,toggleFavorite:()=>f,upsertFavoriteLocal:()=>m}),i=e=>`offer_lens_favorites_v1_${String(e||`public`).trim()||`public`}`,a=()=>globalThis?.chrome?.storage?.local,o=e=>{try{return e?JSON.parse(e):null}catch{return null}},s=async e=>new Promise(t=>{try{let n=a();if(n?.get){n.get(e,n=>t({...e,...n}));return}}catch{}try{let n={...e},r=Object.keys(e||{});for(let e of r){let t=globalThis.localStorage?.getItem?.(e);t!=null&&(n[e]=o(String(t)))}t(n)}catch{t(e)}}),c=async e=>new Promise(t=>{try{let n=a();if(n?.set){n.set(e,()=>t());return}}catch{}try{let n=e&&typeof e==`object`?e:{};for(let[e,t]of Object.entries(n))try{globalThis.localStorage?.setItem?.(e,JSON.stringify(t))}catch{}t()}catch{t()}}),l=async e=>{let t=i(e),r=(await s({[t]:[]}))?.[t];return(Array.isArray(r)?r:[]).map(e=>({jobId:n(e?.jobId),title:n(e?.title),company:n(e?.company),url:n(e?.url),addedAt:Number(e?.addedAt||0)||0})).filter(e=>!!e.jobId).sort((e,t)=>(t.addedAt||0)-(e.addedAt||0))},u=async(e,r)=>{let a=i(e),o=(r||[]).map(e=>({jobId:n(e?.jobId),title:n(e?.title),company:n(e?.company),url:n(e?.url),addedAt:Number(e?.addedAt||0)||Date.now()})).filter(e=>!!e.jobId).slice(0,5e3);await c({[a]:o}),t({type:`FAVORITES_UPDATED`})},d=async(e,t)=>{let r=n(t);return r?(await l(e)).some(e=>e.jobId===r):!1},f=async(e,t)=>{let r=n(t?.id||t?.jobId);if(!r)return{ok:!1,favorited:!1};let i=await l(e),a=i.findIndex(e=>e.jobId===r);return a>=0?(i.splice(a,1),await u(e,i),{ok:!0,favorited:!1}):(i.unshift({jobId:r,title:n(t?.title),company:n(t?.company),url:n(t?.url),addedAt:Date.now()}),await u(e,i),{ok:!0,favorited:!0})},p=async(e,t)=>{let r=n(t);return await u(e,(await l(e)).filter(e=>e.jobId!==r)),!0},m=async(e,t)=>{let r=n(t?.jobId);if(!r)return!1;let i=await l(e),a=i.findIndex(e=>e.jobId===r),o={jobId:r,title:n(t?.title),company:n(t?.company),url:n(t?.url),addedAt:Number(t?.addedAt||0)||Date.now()};return a>=0&&i.splice(a,1),i.unshift(o),await u(e,i),!0};export{l as n,r as t};