import{o as e}from"./chunk-mlkTr37E.js";import{t}from"./query-string-RcCSSKcJ.js";import{t as n}from"./lodash-BtwhQj49.js";import{a as r}from"./logger-BTOOQRog.js";import{n as i}from"./axios-fetch-adapter-C745mZql.js";import{g as a,s as o}from"./cache-BCH2kRNz.js";import{n as s}from"./api-hgs6YYEz.js";import{n as c,t as l}from"./fetch-Cx5zvSbz.js";import{r as u,t as d}from"./getter-CXpjFlcD.js";import{t as f}from"./p-map-BZYlZkh9.js";import{r as p}from"./product-C7mU9Pv6.js";import{n as m}from"./adapter-DIIxC4oG.js";import{r as h}from"./kv-B028JnBK.js";import{n as g}from"./public-B0Q_NJRb.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 h(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(v.default.get(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 g(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 m(c),u=p(l);if(!i)return await a.set(u),s;let d=await C(await w(p(l)));return await a.set(d),d};export{T as t};