import{h as o,a as r}from"./p-5d8b1fbd.js";import{f as i,g as e}from"./p-d0bfb1d8.js";import{p as n}from"./p-cad738c9.js";var t=undefined&&undefined.__awaiter||function(o,r,i,e){function n(o){return o instanceof i?o:new i((function(r){r(o)}))}return new(i||(i=Promise))((function(i,t){function c(o){try{f(e.next(o))}catch(o){t(o)}}function d(o){try{f(e["throw"](o))}catch(o){t(o)}}function f(o){o.done?i(o.value):n(o.value).then(c,d)}f((e=e.apply(o,r||[])).next())}))};var c;(function(o){o["Home"]="HOME";o["Category"]="CATEGORY";o["Product"]="PRODUCT";o["Checkout"]="CHECKOUT"})(c||(c={}));var d;(function(o){o["Trending"]="TRENDING";o["TrendingPersonalized"]="TRENDING_PERSONALIZED";o["NewestArrivals"]="NEWEST_ARRIVALS";o["RecentlyViewed"]="RECENTLY_VIEWED";o["HandPicked"]="HAND_PICKED";o["OtherAlsoViewed"]="OTHER_ALSO_VIEWED";o["Similar"]="SIMILAR";o["BoughtTogether"]="BOUGHT_TOGETHER";o["BoughtTogetherPDP"]="BOUGHT_TOGETHER_PDP";o["VisuallySimilar"]="VISUALLY_SIMILAR";o["Custom"]="CUSTOM_LOGIC"})(d||(d={}));function f(f,u,...s){return t(this,void 0,void 0,(function*(){let t;try{let i="";if((u===null||u===void 0?void 0:u.categoryPath)||(u===null||u===void 0?void 0:u.currentProductId)||(u===null||u===void 0?void 0:u.itemGroupId)){i="?";i+=u.categoryPath?`&cp=${u.categoryPath}`:"";i+=u.currentProductId?`&pid=${u.currentProductId}`:"";i+=u.itemGroupId?`&gpid=${u.itemGroupId}`:""}t=yield o(`https://config-cdn.ksearchnet.com/recommendations/${r.getDefault().apiKey}/settings/${f}${i}`)}catch(o){console.warn("Failed to fetch given KMC recommendation");return{klevuFunctionId:"kmcRecommendation"}}if(!t){throw new Error("Couldn't fetch KMC config")}const a=new r({apiKey:r.getDefault().apiKey,url:`https://${t.search.basePath}`});const m=JSON.parse(t.search.payload);const l=m.recordQueries.map((o=>{var r;return Object.assign(Object.assign({},o),{id:(r=u===null||u===void 0?void 0:u.id)!==null&&r!==void 0?r:"kmcrecommendation"})}));if(t.metadata.pageType===c.Home&&t.metadata.logic===d.RecentlyViewed){const o=i.getLastClickedLatestsFirst(10);for(const r of l){if(!r.settings){r.settings={}}r.settings.customANDQuery=`id:(${o.join(" OR ")})`;r.settings.topIds=o.map((o=>({key:"id",value:o})))}}if(t.metadata.pageType===c.Category&&[d.Trending,d.TrendingPersonalized,d.NewestArrivals,d.Custom].includes(t.metadata.logic)){if(!u||!u.categoryPath){throw new Error("'categoryPath' is required for Category recommendation")}for(const o of l){if(!o.settings){o.settings={}}if(!o.settings.query){o.settings.query={}}o.settings.query.categoryPath=u===null||u===void 0?void 0:u.categoryPath}}if(t.metadata.logic===d.TrendingPersonalized){s.push(n())}if(t.metadata.pageType===c.Product&&[d.OtherAlsoViewed,d.Similar].includes(t.metadata.logic)){if(!u||!u.currentProductId){throw new Error("'currentProductId' is required for Product recommendation")}for(const o of l){if(!o.settings){o.settings={}}if(!o.settings.context){o.settings.context={}}if(u.itemGroupId){o.settings.excludeIds=[{key:"itemGroupId",value:u.itemGroupId}]}o.settings.context.recentObjects=[{typeOfRecord:e.Product,records:[{id:u.currentProductId}]}]}}if(t.metadata.pageType===c.Product&&[d.BoughtTogetherPDP,d.Custom].includes(t.metadata.logic)){if(!u||!u.currentProductId){throw new Error("'currentProductId' is required for Product recommendation")}for(const o of l){if(!o.settings){o.settings={}}if(!o.settings.context){o.settings.context={}}if(u.itemGroupId){o.settings.excludeIds=[{key:"itemGroupId",value:u.itemGroupId}]}o.settings.context.recentObjects=[{typeOfRecord:e.Product,records:[{id:u.currentProductId}]}]}}if(t.metadata.pageType===c.Product&&t.metadata.logic===d.VisuallySimilar){if(!u||!u.currentProductId){throw new Error("'currentProductId' is required for Product recommendation")}for(const o of l){if(!o.settings){o.settings={}}if(!o.settings.context){o.settings.context={}}if(u.itemGroupId){o.settings.excludeIds=[{key:"itemGroupId",value:u.itemGroupId}]}o.settings.context.sourceObjects=[{typeOfRecord:e.Product,records:[{id:u.currentProductId}]}];if(u.itemGroupId){o.settings.context.sourceObjects[0].records.push({itemGroupId:u.itemGroupId})}}}if(t.metadata.pageType===c.Checkout&&[d.BoughtTogether,d.Custom].includes(t.metadata.logic)){if(!u||!u.cartProductIds){throw new Error("'cartProductIds' is required for Checkout recommendation")}for(const o of l){if(!o.settings){o.settings={}}if(!o.settings.context){o.settings.context={}}o.settings.context.recentObjects=[{typeOfRecord:e.Product,records:u.cartProductIds.map((o=>({id:o})))}]}}if(t.metadata.productThreshold){s.push({klevuModifierId:"kmcThreshold",onResult:o=>{var r,i;const e=Object.assign({},o);for(const o of(i=(r=e.apiResponse)===null||r===void 0?void 0:r.queryResults)!==null&&i!==void 0?i:[]){if(o.records.length<t.metadata.productThreshold){o.records=[]}}return o}})}return{klevuFunctionId:"kmcRecommendation",queries:l,params:{kmcConfig:t},modifiers:s,configOverride:a}}))}export{f as k};
//# sourceMappingURL=p-6dcbd18c.js.map