import{a as e,p as t}from"./types-CFYvnnbw.js";import{i as n}from"./collectionSkuImages-BDnlQcTj.js";import{t as r}from"./collectionId-8ALNfTtA.js";import{i,n as a,r as o,t as s}from"./adapterTaobao-ICCVXP9_.js";import{a as c}from"./imagePickBroadcast-ZJib9Pjm.js";import{t as l}from"./mountPageCollectActions-Di2YARbR.js";import{c as u,d,f,i as p,l as m,o as h,r as g,u as _}from"./bridgePayload-0nV9-YOo.js";function v(e){return e==null?``:String(e).trim()}function y(e){let t=e.trim();return t?t.startsWith(`//`)?`https:${t}`:t:``}function b(e,t,n,r){let i=y(n);!i||t.has(i)||(t.add(i),e.push({url:i,role:r}))}function x(){return o(window.location.href)||null}async function S(){try{let e=new URL(window.location.href);return a(e)?s.extractProduct(document,e):null}catch{return null}}function C(e,t){return v(e.title)||t?.title?.trim()||`淘宝商品`}function w(e,t,n){if(n?.priceText?.trim())return n.priceText.trim();let r=t.find(e=>e.priceText?.trim());if(r?.priceText)return`¥${r.priceText}`;let i=v(e.vagueSellCount);return i?`已售 ${i}`:``}function T(e){let t=v(e.vagueSellCount),n=t.match(/(\d+(?:\.\d+)?)\s*[万+]*/);if(!n)return;let r=Number(n[1]);if(Number.isFinite(r))return t.includes(`万`)&&(r*=1e4),Math.round(r)}function E(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function D(e){let t=e.videos;if(!Array.isArray(t)||!t.length)return null;let n=t[0];if(!E(n))return null;let r=y(v(n.url));return r?{videoUrl:r,posterUrl:y(v(n.videoThumbnailURL))||void 0}:null}function O(e,n,r){let i=[],a=new Set,o=D(e);o&&t(i,a,{videoUrl:o.videoUrl,posterUrl:o.posterUrl,role:`main`});let s=e.images;if(Array.isArray(s))for(let e of s)b(i,a,v(e),`main`);for(let e of n)e.imageUrl&&b(i,a,e.imageUrl,`sku`);if(r){for(let e of r.mainImages)b(i,a,e,`main`);for(let e of r.detailImages)b(i,a,e,`detail`)}return i}function k(e,t,n){let i=e.id.trim()||t?.trim()||``;if(!i)return{ok:!1,errorMessage:`无法解析商品 ID`};let a=new Date().toISOString(),o=[],s=new Set;for(let t of[...e.mainImages,...e.detailImages,...e.skuImages])b(o,s,t,`main`);return{ok:!0,item:{id:r(n,i),platform:n,externalId:i,url:e.url,title:e.title,description:e.description,price:{currentText:e.priceText?.trim()??``,currency:`CNY`},images:o,tags:e.specs.map(e=>({kind:`property`,text:e})),skus:[],collectedAt:a,updatedAt:a}}}function A(t,a,o,s){let c=i(s),l=t&&d(t)?_(t,`collectedItemFromTaobaoDetail`):[];if(!l.length)return a?k(a,o,c):{ok:!1,errorMessage:`详情数据未就绪（ICE SKU 缺失）`};let u=t?f(t):null,p=v(u?.itemId)||o?.trim()||a?.id?.trim()||``;if(!p)return{ok:!1,errorMessage:`无法解析商品 ID`};let m=u?C(u,a):a?.title??`淘宝商品`,h=new Date().toISOString(),g=u?O(u,l,a):[],y=a?.specs?.map(e=>({kind:`property`,text:e}))??[],b={id:r(c,p),platform:c,externalId:p,url:s,title:m,description:a?.description??m,price:{currentText:u?w(u,l,a):a?.priceText??``,currency:`CNY`},images:g,tags:y,skus:l,stats:u?{soldCount:T(u)}:void 0,collectedAt:h,updatedAt:h,sourceApi:e,rawPayloadJson:t?JSON.stringify(t):void 0};return b=n(b),{ok:!0,item:b}}if(console.log(`[xyxp-taobao] isolated script loaded`,{href:location.href,top:window.self===window.top}),window.self===window.top){let e=null,t=null;function n(t,n){if(!t||!d(t))return;e=t;let r=_(e).length;console.log(`[xyxp-taobao-sku] isolated: received ICE data`,{via:n,rowCount:r}),p()}async function r(){return t||=S(),t}function i(){try{window.postMessage({source:m,type:u},`*`)}catch{}}async function a(t=8e3){if(e&&d(e))return;i();let n=Date.now()+t;for(;Date.now()<n;){if(await new Promise(e=>{window.setTimeout(e,120)}),e&&d(e))return;i()}}async function o(){let t=x();await a();let n=await r(),i=A(e,n,t,window.location.href);return console.log(`[xyxp-taobao-sku] buildCollectedItem`,{ok:i.ok,skuCount:i.item?.skus.length??0,imageCount:i.item?.images.length??0,hasIceData:!!e}),i}function s(){l({hostId:`xyxp-taobao-manual-add-sidebar`,storageKey:c,platform:`taobao`,resolvePlatform:()=>/tmall\.com/i.test(location.hostname)?`tmall`:`taobao`,theme:`goofish`,buildCollectedItem:()=>o(),resolveExternalId:()=>x()||null})}window.addEventListener(`message`,e=>{if(e.source!==window)return;let t=e.data;!g(t)||t.type!==`TAOBAO_ICE_DATA`||n(t.payload,`message`)}),document.addEventListener(h,e=>{let t=e.detail;n(t,`dom-event`)}),chrome.runtime.onMessage.addListener((e,t,n)=>e?.type===`GET_TAOBAO_COLLECTED_ITEM`?(o().then(e=>{n(e.ok&&e.item?{ok:!0,item:e.item}:{ok:!1,error:e.errorMessage??`淘宝/天猫商品数据未就绪`})}).catch(e=>{n({ok:!1,error:e instanceof Error?e.message:`淘宝/天猫整理失败`})}),!0):!1),i(),document.readyState===`loading`?document.addEventListener(`DOMContentLoaded`,s,{once:!0}):s()}