import{o as e}from"./chunk-DpFlLKUR.js";import{o as t}from"./collectionDb-PYbgB2zr.js";import{a as n,d as r,n as i,u as a}from"./collectionZipAssets-CHNc9vS3.js";var o=e(r(),1);function s(e){let t=e.trim();return t.startsWith(`http://`)||t.startsWith(`https://`)||t.startsWith(`//`)}function c(e){let t=e.split(`.`).pop()?.toLowerCase()??`jpg`;return t===`png`?`image/png`:t===`webp`?`image/webp`:t===`gif`?`image/gif`:t===`mp4`?`video/mp4`:t===`webm`?`video/webm`:t===`mov`||t===`m4v`?`video/quicktime`:`image/jpeg`}async function l(e,t,r,a){let o=t.trim();if(!o||o.startsWith(`data:`))return o;if(s(o))return o.startsWith(`//`)?`https:${o}`:o;if(!n(o))return o;let l=e.file(o);if(!l)return r.push(`${a}: zip 内缺少文件 ${o}`),o;let u=await l.async(`arraybuffer`);return i(new Uint8Array(u),c(o))}async function u(e,t,n){let r=t.id,i={...t};if(i.images=await Promise.all(t.images.map(async t=>({...t,url:await l(e,t.url,n,r),posterUrl:t.posterUrl?await l(e,t.posterUrl,n,`${r} 封面`):t.posterUrl}))),i.skus=await Promise.all(t.skus.map(async t=>({...t,imageUrl:t.imageUrl?await l(e,t.imageUrl,n,r):t.imageUrl}))),t.antiDup){let a=[];for(let i of t.antiDup.imageVariants){let t=i.sourceUrl?await l(e,i.sourceUrl,n,r):i.sourceUrl,o={...i,sourceUrl:t};i.processedUrl?.trim()&&(o.processedUrl=await l(e,i.processedUrl,n,`${r} 加工图`)),a.push(o)}i.antiDup={...t.antiDup,imageVariants:a}}return i}async function d(e,n,r){let i=[],s;try{s=await o.default.loadAsync(e)}catch{throw Error(`无法读取 ZIP 文件，请确认格式正确`)}let c=s.file(a);if(!c)throw Error(`ZIP 内缺少 manifest.json`);let l;try{let e=await c.async(`string`);l=JSON.parse(e)}catch{throw Error(`manifest.json 解析失败`)}if(l.schemaVersion!==2)throw Error(`不支持的导出版本 schemaVersion=${l.schemaVersion??`未知`}`);let d=l.items??[];if(!d.length)throw Error(`ZIP 内没有商品数据`);l.warnings?.length&&i.push(...l.warnings);let f=[],p=d.length;for(let e=0;e<d.length;e+=1){let t=d[e];r?.(e,p,`导入商品 ${e+1}/${p}`),f.push(await u(s,t,i))}return r?.(p,p,`写入数据库`),{...await t({schemaVersion:2,exportedAt:l.exportedAt??new Date().toISOString(),items:f},n),warnings:i}}export{d as importCollectionZip};