import{n as e}from"./types-CFYvnnbw.js";import{a as t,s as n}from"./collectionImages-DG90KIrV.js";import{i as r}from"./collectionSkuImages-BDnlQcTj.js";import{t as i}from"./collectionId-8ALNfTtA.js";var a=`items`;function o(e){return JSON.parse(JSON.stringify(e))}function s(e){return o(e)}function c(){return new Promise((t,n)=>{let r=indexedDB.open(e,1);r.onupgradeneeded=()=>{let e=r.result;if(!e.objectStoreNames.contains(a)){let t=e.createObjectStore(a,{keyPath:`id`});t.createIndex(`platform`,`platform`,{unique:!1}),t.createIndex(`externalId`,`externalId`,{unique:!1}),t.createIndex(`updatedAt`,`updatedAt`,{unique:!1}),t.createIndex(`collectedAt`,`collectedAt`,{unique:!1}),t.createIndex(`platformExternal`,[`platform`,`externalId`],{unique:!0})}},r.onsuccess=()=>t(r.result),r.onerror=()=>n(r.error??Error(`IndexedDB open failed`))})}async function l(e){let t=await c();return new Promise((n,r)=>{let i=t.transaction(a,`readonly`).objectStore(a).get(e);i.onsuccess=()=>{t.close(),n(i.result??null)},i.onerror=()=>{t.close(),r(i.error??Error(`get failed`))}})}async function u(e,t){return await l(i(e,t))!=null}async function d(e){let t=await l(e.id),n=o(r({...e,collectedAt:t?.collectedAt??e.collectedAt,updatedAt:e.updatedAt})),i=await c();await new Promise((e,t)=>{let r=i.transaction(a,`readwrite`);r.oncomplete=()=>{i.close(),e()},r.onerror=()=>t(r.error??Error(`put tx failed`)),r.objectStore(a).put(n)})}async function f(e,t){let n=await l(e);if(!n)throw Error(`素材记录不存在`);let r=new Date().toISOString(),i={...n,title:t.processedTitle,description:t.processedDescription,antiDup:{...t,updatedAt:r},updatedAt:r};return await d(i),i}async function p(e){let t=await l(e);if(!t)throw Error(`素材记录不存在`);if(!t.antiDup)return t;let n=new Date().toISOString(),r={...t,title:t.antiDup.originalTitle||t.title,description:t.antiDup.originalDescription||t.description,antiDup:void 0,updatedAt:n};return await d(r),r}function m(e){return e.processedTitle!==e.originalTitle||e.processedDescription!==e.originalDescription}function h(e){return e.imageVariants.some(e=>!!e.processedUrl?.trim())}async function g(e){let t=await l(e);if(!t)throw Error(`素材记录不存在`);if(!t.antiDup||!m(t.antiDup))return t;if(!h(t.antiDup))return p(e);let n=new Date().toISOString(),r={...t.antiDup,processedTitle:t.antiDup.originalTitle,processedDescription:t.antiDup.originalDescription,updatedAt:n},i={...t,title:r.originalTitle||t.title,description:r.originalDescription||t.description,antiDup:r,updatedAt:n};return await d(i),i}async function _(e,t){let r=await l(e);if(!r)throw Error(`素材记录不存在`);if(!r.antiDup)throw Error(`该图未加工`);let i=n(r,t);if(!r.antiDup.imageVariants.some(e=>e.sourceUrl?.trim()===i&&!!e.processedUrl?.trim()))throw Error(`该图未加工`);let a=r.antiDup.imageVariants.map(e=>{if(e.sourceUrl?.trim()!==i)return e;let t={sourceUrl:e.sourceUrl};return e.sourceHash&&(t.sourceHash=e.sourceHash),t});if(!h({...r.antiDup,imageVariants:a})&&!m(r.antiDup))return p(e);let o=new Date().toISOString(),s={...r,antiDup:{...r.antiDup,imageVariants:a,updatedAt:o},updatedAt:o};return await d(s),s}async function v(e){if(!e.length)return;let t=await c();await new Promise((n,r)=>{let i=t.transaction(a,`readwrite`);i.oncomplete=()=>{t.close(),n()},i.onerror=()=>r(i.error??Error(`delete batch tx failed`));let o=i.objectStore(a);for(let t of e)o.delete(t)})}function y(e){return/^https?:\/\//i.test(e.trim())}function b(e){let n=t(e.images).trim();return y(n)?n:``}function x(e){return{id:e.id,platform:e.platform,title:e.title?.trim()||`（无标题）`,priceText:e.price?.currentText?.trim()||`—`,thumbUrl:b(e),updatedAt:e.updatedAt}}async function S(){let e=await c();return new Promise((t,n)=>{let r=e.transaction(a,`readonly`).objectStore(a),i=[],o=r.openCursor();o.onsuccess=()=>{let n=o.result;if(!n){e.close(),i.sort((e,t)=>t.updatedAt.localeCompare(e.updatedAt)),t(i);return}let r=n.value;r?.id&&i.push(x(r)),n.continue()},o.onerror=()=>{e.close(),n(o.error??Error(`list summaries failed`))}})}async function C(){let e=await c();return new Promise((t,n)=>{let r=e.transaction(a,`readonly`).objectStore(a).getAll();r.onsuccess=()=>{e.close(),t(r.result??[])},r.onerror=()=>{e.close(),n(r.error??Error(`list failed`))}})}async function w(e,t){let n=0,r=0;for(let i of e.items??[]){if(!i?.id||!i.platform||!i.externalId){r+=1;continue}if(await l(i.id)&&t===`skip`){r+=1;continue}await d(i),n+=1}return{imported:n,skipped:r}}export{u as a,C as c,g as d,_ as f,l as i,d as l,s as n,w as o,x as p,v as r,S as s,f as t,p as u};