import{o as e}from"./chunk-mlkTr37E.js";import{t}from"./query-string-RcCSSKcJ.js";import{t as n}from"./lodash-DulIlzb9.js";import{a as r}from"./logger-m2skRwRe.js";import{n as i}from"./axios-fetch-adapter-m8Kx6J-y.js";import{g as a,s as o}from"./cache-hsasTrVL.js";import{n as s}from"./api-DzB1tprs.js";import{n as c,t as l}from"./fetch-BE0PHwMd.js";import{r as u,t as d}from"./getter-D7iIs4HV.js";import{t as f}from"./p-map-DPdBLmfu.js";import{n as p}from"./adapter-DJCtt7vM.js";import{i as m,n as h}from"./public-D6PriEL1.js";import{r as g}from"./kv-CSDV0Kj0.js";var _=e(f()),v=e(n()),y=(e,n)=>t.stringifyUrl({url:`${i}/api/v4/pdp/get_pc`,query:{shop_id:n,item_id:e}}),b=e=>JSON.stringify(Object.fromEntries(e.map((e,t)=>[t,e]))),x={request:async(e,t)=>{let{active:n,listPayload:i}=await u(e,t);if(!(n&&i&&i.length))return r.error({msg:`PRODUCT_GET_HEADER_ERROR`,productId:e,shopId:t}),null;let a=null;return await(0,_.default)(i.reverse(),async({uuid:e,url:t,headers:n})=>{if(!a){let i=await c(t,n);d(i)||(a=i),r.error({msg:`PRODUCT_GET_BY_REQUEST`,isCrawlered:d(i),result:a,uuid:e,get:i})}},{concurrency:1}),a?.data?a.data:null},popup:async(e,t)=>{let n=await s(y(e,t));return n&&n.data?n.data:null},content:async(e,t)=>{let n=await l(y(e,t));return n&&n.data?n.data:null}},S=async(e,t)=>{let n=[e,t].join(`-`),r=new a(n),i=await r.get();if(i)return i||[];let o=await new g(n).get();return o?(r.set(o),o||[]):[]},C=async e=>{try{let{item_id:t,shop_id:n}=e,r=await S(t,n);e.models=e.models.map(e=>{let t=v.default.get(e,`has_stock`,!0),n=e.modelid||e.model_id||e.id,i=b(e.extinfo.tier_index),a=r.find(e=>e.modelId===n||e.modelId===i);return a&&a.stock&&(e.model_stock=a.stock,e.real_stock=a.stock,e.stock=a.stock,e.fetched_stock=!0),t?e:(e.model_stock=0,e.real_stock=0,e.stock=0,e.fetched_stock=!0,e)})}catch(t){r.error({msg:`FETCH_STOCK_PRODUCT_ERROR`,error:t,productData:e})}return e},w=async e=>{try{let{item_id:t,shop_id:n}=e,{listModelSku:r,listModelDimension:i}=await h(n,t),a=i[0]||null;e.models=e.models.map(t=>{let n=t.modelid||t.model_id||t.id,i=v.default.get(t,`has_stock`,!0),a=r.find(e=>Number(e.modelId)===Number(n));return a&&a.stock&&(t.fetched_stock=!0,t.model_stock=a.stock,t.real_stock=a.stock,t.stock=a.stock,t.sku=a.modelSku,e.sku=a.productSku,e.parent_sku=a.productSku),i?t:(t.model_stock=0,t.real_stock=0,t.stock=0,t)}),a&&(e.dimension={weight:a.weight,width:a.width,height:a.height,length:a.length},e.weight=a.weight)}catch(t){r.error({msg:`PULL_PRODUCT_EXTRA_INFO_ERROR`,error:t,productData:e})}return e},T=async(e,t,n=``,i=!1)=>{let a=new o(`${t}-${e}`),s=await a.get();if(s)return r.error({msg:`GET_PRODUCT_DETAIL`,by:`cache`,method:n,product:s}),i?await C(await w(s)):s;let c=null;try{c||(c=await x.request(e,t),c&&r.error({msg:`GET_PRODUCT_DETAIL`,by:`request`,method:n,product:c})),!c&&n===`content`&&(c=await x.content(e,t),c&&r.error({msg:`GET_PRODUCT_DETAIL`,by:`content`,method:n,product:c})),!c&&n===`popup`&&(c=await x.popup(e,t),c&&r.error({msg:`GET_PRODUCT_DETAIL`,by:`popup`,method:n,product:c}))}catch(e){r.error({msg:`GET_PRODUCT_ERROR`,type:n,error:e})}if(!c)return r.error({msg:`ERROR_PRODUCT_DETAIL`,method:n}),null;let l=await p(c),u=m(l);if(!i)return await a.set(u),s;let d=await C(await w(m(l)));return await a.set(d),d};export{T as t};