import{r as i}from"./p-d2af20eb.js";import{i as n}from"./p-7051251d.js";import{s,g as t}from"./p-48d56e80.js";import{g as o}from"./p-d22704c1.js";import{w as e}from"./p-1d921be2.js";import{a}from"./p-25a090c9.js";let c=class{constructor(n){i(this,n),this.endpoint="/api/common/businessrule-promotions/1.0/getPromotionsForCustomer/",this.locale=null,this.env="prod",this.data=null,this.targetSelector="",this.position=0,this.output="teaser",this.maxItems=0,this.pickItems="first",this.timeout=2e3,this.opacityUtilityClass="ux-u-opacity-0",this.inserted=!1,this.timeoutId=null,this.sliderPromotionHasLoaded="slider"!==this.output&&null,this.addLoadingSlide=async()=>{if("slider"!==this.output)return;if(""===this.targetSelector)return;const i=document.querySelector(this.targetSelector);e(i,(()=>{!0!==this.sliderPromotionHasLoaded&&(i.addSlide(`\n \n \n
\n
\n \n \n
\n
\n
\n \n `,0===this.position),window.setTimeout((async()=>{await i.update({autoplay:!1}),i.parentElement.classList.remove(this.opacityUtilityClass)}),100))}))},this.getLoadingSlide=()=>document.querySelector(this.targetSelector).querySelector(".ux-async-promotion__loading-slide"),this.replaceLoadingSlide=i=>{const n=this.getLoadingSlide();null!==n&&(n.innerHTML=i)},this.removeLoadingSlide=()=>{if("slider"!==this.output)return;const i=document.querySelector(this.targetSelector);null!==this.getLoadingSlide()&&(i.removeSlide(this.position),i.update())}}componentWillLoad(){this.addLoadingSlide(),this.timeoutId=window.setTimeout((()=>this.removeLoadingSlide()),this.timeout)}componentDidLoad(){this.init()}init(){if(null!==this.locale){const{env:i,endpoint:n,locale:o}=this;s(i);const{websiteBaseUrl:e}=t();a(`${e}${n}?locale=${o}`).then((i=>this.data=i)).catch((()=>this.data=null))}else this.insertData()}async insertData(){if(this.inserted||null==this.data||void 0===this.data.promotions)return;this.inserted=!0,void 0!==typeof window.__STORYBOOK_ADDONS&&(console.log("simulating slow response"),await new Promise((i=>setTimeout(i,1e3))));const{promotions:i}=this.data;if(0===i.length)return this.removeLoadingSlide(),void window.clearTimeout(this.timeoutId);if(""===this.targetSelector)return;const s=document.querySelector(this.targetSelector);if(null===s)return;if(0===i.filter((i=>i.linkUrl&&i.transformedProperties&&i.transformedProperties.image&&i.headline)).length)return;const t="random"===this.pickItems?function(i){for(let n=i.length-1;n>0;n--){const s=Math.floor(Math.random()*(n+1));[i[n],i[s]]=[i[s],i[n]]}return i}([...i]):i,o=((i,n)=>{const s=-1===n;let t=s?-1:n+1;return i.map(((i,n)=>{var o;return Object.assign(Object.assign({},i),{tracking:{id:null===(o=null==i?void 0:i.campaign)||void 0===o?void 0:o.title,name:null==i?void 0:i.headline,position:`slot${s?t-n:t+n}`}})}))})(this.pickPromotions(t),this.position);var e;this.addContent(s,o),e=o.map((({tracking:i})=>i)),window.ITdataLayer=window.ITdataLayer||[],window.ITdataLayer.push({ecommerce:null}),window.ITdataLayer.push({ecommerce:{promoView:{promotions:e}}}),Array.from(document.querySelectorAll(".js-ux-async-promotion-tracking")).forEach((i=>{try{const s=JSON.parse(i.getAttribute("data-tracking")),t=i.getAttribute("href");i.addEventListener("click",(i=>{i.preventDefault(),n().then((()=>{window.ITdataLayer=window.ITdataLayer||[],window.ITdataLayer.push({ecommerce:null}),window.ITdataLayer.push({event:"promotionClick",ecommerce:{promoClick:{promotions:[s]}},eventCallback:function(){console.log("promotion click tracked"),document.location.href=t}})})).catch((()=>{console.log("promotion tracking failed because cookie was not accepted"),document.location.href=t}))}))}catch(i){console.log("promotion tracking failed")}})),window.clearTimeout(this.timeoutId)}async addContent(i,n){let s=n.map((i=>function(i){const{image:n}=i.transformedProperties;return`\n
\n\n
\n
\n \n \n \n \n \n
`+(""!==i.teaser?`
\n
\n \n ${i.teaser}\n \n
\n
`:"")+`\n
\n
\n ${i.subHeadline}\n \n
\n ${i.headline||""}\n \n
\n
\n
\n ${i.bodytext||""}\n
\n
\n
\n
\n \n\n
\n
\n `}(i)));if(-1!==this.position&&(s=s.reverse()),"slider"===this.output){const n=i;return s.forEach((async(i,s)=>{0===s&&null!==this.getLoadingSlide()?this.replaceLoadingSlide(i):e(n,(async()=>{await n.addSlide(`${i} `,0===this.position)}))})),this.sliderPromotionHasLoaded=!0,void window.setTimeout((()=>n.update()),100)}"teaser"===this.output&&s.forEach((n=>{const s=document.createElement("div");s.innerHTML=n,0!==this.position?i.append(s):i.prepend(s)}))}pickPromotions(i){return 0===this.maxItems||i.length<=this.maxItems?i:i.slice(0,this.maxItems)}static get watchers(){return{env:["init"],locale:["init"],data:["insertData"]}}};export{c as ux_iws_async_promotion}