import{o as e,s as t}from"./modelConfig-CSZMXwN9.js";import{n,t as r}from"./cloudGateway-faZLvdAI.js";import{n as i,r as a}from"./pagePlainText-BC2HQNNG.js";import{r as o}from"./pageDetailHtml-CA9cVLla.js";import{t as s}from"./adapter1688-CG3T40gu.js";import{t as c}from"./adapterPdd-JWlJKPw0.js";import{t as l}from"./adapterSooxie-LI96NX5W.js";import{t as u}from"./adapterTaobao-ICCVXP9_.js";var d=`[xyxp-cloudModel]`;function f(e){let t=(e??``).trim();return t.startsWith("```")?t.replace(/^```[a-zA-Z]*\s*/,``).replace(/```$/,``).trim():t}function p(e,n){let r=f(e);if(!r)return console.warn(`${d} safeParseCopy: assistant 正文为空`,{content:e}),null;try{let e=JSON.parse(r);if(t(n)){let t=typeof e.formattedCopy==`string`?e.formattedCopy.trim():``;return t?{title:``,sellingPoints:[],description:``,negotiationScript:``,tags:[],formattedCopy:t}:(console.warn(`${d} safeParseCopy: 结构模式缺 formattedCopy`,e),null)}return!e.title||!Array.isArray(e.sellingPoints)?(console.warn(`${d} safeParseCopy: JSON 缺 title 或 sellingPoints`,e),null):e}catch(e){return console.warn(`${d} safeParseCopy: JSON 解析失败`,e),null}}function m(){return new Promise(e=>{requestAnimationFrame(()=>e())})}async function h(t,i,o){if(r(t),!n(t))throw Error(`请先在设置页填写直连所需的 Base URL、API Key 与 Model。`);return new Promise((n,r)=>{let s=chrome.runtime.connect({name:a}),c=e(t),l=!1,u=Promise.resolve(),f=()=>{l||(l=!0,s.onMessage.removeListener(g),r(Error(`流式连接已断开，请重试。`)))},h=e=>{if(!l){l=!0,s.onMessage.removeListener(g),s.onDisconnect.removeListener(f);try{s.disconnect()}catch{}e()}},g=e=>{if(e.type===`chunk`){u=u.then(async()=>{o?.({reasoning:e.reasoning??``,assistant:e.assistant??``}),await m()});return}if(e.type===`done`){u=u.then(()=>{let t=p(e.assistant,c);if(!t){h(()=>{r(Error(`${d} done: 服务返回格式不正确（assistant 为空或非 JSON），请检查提示词是否要求严格输出 JSON。`))});return}h(()=>{n({mode:`cloud`,...t})})});return}e.type===`error`&&(u=u.then(()=>{h(()=>{r(Error(e.message))})}))};s.onMessage.addListener(g),s.onDisconnect.addListener(f),s.postMessage({type:`START`,payload:{config:c,pipeline:i}})})}var g=[s,c,u,l];function _(e){return g.find(t=>t.match(e))??null}function v(e){let t=e.toLowerCase(),n=[];return(t.includes(`detail`)||t.includes(`main`))&&n.push(`主图`),(t.includes(`comment`)||t.includes(`review`))&&n.push(`评论图`),t.includes(`video`)&&n.push(`视频帧`),n.length>0?n:[`商品展示`]}function y(e){let t=e.match(/(\d{2,4})x(\d{2,4})/);return t?{width:Number(t[1]),height:Number(t[2])}:{}}function b(e,t,n){return e.map(e=>({url:e,kind:t,tags:[...n,...v(e)],...y(e)}))}function x(e,t,n,r){return[...b(e,`product`,[`主图`]),...b(t,`detail`,[`详情图`]),...b(n,`sku`,[`SKU图`]),...b(r,`comment`,[`买家实拍`])]}function S(e,t){return Array.from(new Set([...e,...t].filter(Boolean)))}function C(e){let t=e=>typeof e==`string`?e.trim():``,n=new Set(e.commentImages.map(t).filter(Boolean)),r=e=>e.filter(e=>{let r=t(e);return r&&!n.has(r)});e.mainImages=r(e.mainImages),e.detailImages=r(e.detailImages),e.skuImages=r(e.skuImages);let i=new Set,a=e=>{let n=[];for(let r of e){let e=t(r);!e||i.has(e)||(i.add(e),n.push(r))}return n};e.mainImages=a(e.mainImages),e.detailImages=a(e.detailImages),e.skuImages=a(e.skuImages),e.commentImages=a(e.commentImages)}async function w(e,t){let n=new URL(t),r=_(n);if(!r)throw Error(`当前页面暂不支持，请在闲鱼、1688、拼多多、淘宝/天猫或搜鞋商品详情页打开。`);let a=r.extractProduct(e,n),s=r.extractCommentImages(e),c=await r.fetchExtraCommentImagesById(a.id);a.commentImages=S([...a.commentImages,...s],c),C(a);let l=x(a.mainImages,a.detailImages,a.skuImages,a.commentImages),u=r.extractDetailHtml?.(e),d=u?o(u):void 0;return{product:a,imageInsights:l,pagePlainText:i(e),...d?{pageDetailHtml:d}:{}}}async function T(e,t,n){return h(t,e,n)}export{T as n,x as r,w as t};