/**
 * @preserve
 * Licenses of bundled dependencies can be found in dependencies-licenses.txt
 */
/*! vehicle-context-automation 2.2.0 */
import{initializeLogger as e}from"@seamless/logger";import{getConnectionVehicleDataDispatchers as t,connectionName as n}from"@dh-io-globalelem/vehicle-data-store-connection";import{SeamlessConnection as r,initializeStore as a}from"@seamless/store";import{formatToMarketSpecificCurrency as o}from"@workbench/core";const i="cta-automation-service",s="cta_service.is_initialized";var c,u,d,l,f,g,p;!function(e){e.Configurator="CONFIGURATOR",e.OnlineShop="ONLINE_SHOP",e.ProductPage="PRODUCT_PAGE",e.TestDrive="TEST_DRIVE",e.Enquiry="ENQUIRY"}(c||(c={})),function(e){e.Publish="PUBLISHER",e.Author="AUTHOR"}(u||(u={})),function(e){e.Primary="primary",e.Secondary="secondary",e.Shop="shop",e.Configurator="configurator",e.Info="info",e.CarDrive="cardrive",e.Enquiry="enquiry",e.TestDrive="testDrive"}(d||(d={})),function(e){e.Configurator="bds/car-config/24",e.OnlineShop="bds/shopping-cart/24",e.Enquiry="bds/pencil-document/24"}(l||(l={})),function(e){e.Default="default",e.Alternative="alternative"}(f||(f={})),function(e){e.VehicleId="vehicleId"}(g||(g={})),function(e){e.Default="",e.AMG="MERCEDES_AMG",e.Maybach="MERCEDES_MAYBACH"}(p||(p={}));const m=e(i),h=[c.Configurator,c.OnlineShop],w=[c.Configurator],y=[c.OnlineShop,c.ProductPage],b=[c.ProductPage],v=[c.OnlineShop],$=(e,t)=>t.every((t=>Object.keys(e).includes(t))),S=(e,t=!1,n=f.Default)=>{switch(!0){case $(e,h):return t?C(n):[c.OnlineShop,c.ProductPage];case $(e,w):return t?[c.Configurator,c.Enquiry]:[c.Configurator,c.ProductPage];case $(e,y):return t?[c.OnlineShop]:[c.OnlineShop,c.ProductPage];case $(e,b):return t?[c.Enquiry]:[c.ProductPage];case $(e,v):return[c.OnlineShop,c.Enquiry];default:return m.error("No scenario detected for loaded data:",e),[c.Enquiry]}},C=e=>e===f.Default?[c.OnlineShop,c.Configurator]:[c.OnlineShop,c.Enquiry],E=e(i),N=async e=>{const t=(()=>{try{const e=localStorage.getItem("cta_service.deeplink_response_mock_data");if(e){const t=JSON.parse(e);return E.log("Skipping sending request to the deeplink service and using mock data from the local storage:",t),t}}catch(e){E.error("An error occurred during injecting deep link data from the local storage:",e.message)}})();if(t)return t;try{E.log(`Sending request to ${e}`);const t=await fetch(e),n=await t.json();if(t.ok)return n}catch(e){E.error("Error caused during processing request to deep link service")}},U=(e,{runMode:t,country:n,language:r,modelSeries:a,subBrands:o,engineConcept:i})=>{const s="AUTHOR"===t?u.Author:u.Publish;return`${e}/vehicle-deeplinks-api/v1/deeplinks/${"AQ"===n?"DE":n}/${r}/model-series/${a}?${new URLSearchParams([...(o||[]).map((e=>["subBrands",e])),["cmsInstance",s],..."PLUGIN_HYBRID"===i&&o.includes(p.AMG)?[["engineConcept",i]]:[]])}`},k=(e,t)=>{const n=(e=>{switch(e){case c.OnlineShop:return l.OnlineShop;case c.Configurator:return l.Configurator;case c.Enquiry:return l.Enquiry;default:return""}})(t);return{...e,type:t,baseUrl:e?.url,icon:n}},A=(e,...t)=>{const n=[d.Primary,d.Secondary,d.Shop,d.Configurator,d.Info,d.Enquiry];if(t.length)return t.reduce(((t,r,a)=>{const o=n[a],i=e[r];if(o&&i){const e=k(i,r);t[o]=e}return t}),{})},R=async e=>{const n=(e=>{const t={};for(const n in c){const r=c[n];if(e[r]){const n=k(e[r],r);t[r.toLowerCase()]=n}}return t})(e);(await t()).setCta(n)},B=(e,t)=>{const n={...e};return Object.keys(n).forEach((e=>{const r=n[e];if(r&&r.type===c.Configurator&&t){const a=D(r.baseUrl,t);n[e]={...r,url:a}}})),n},D=(e,t)=>{const n=new URL(e);return n.hash?P(n,t):n.searchParams.set(g.VehicleId,t),n.toString()},P=(e,t)=>{const n=e.hash.indexOf("?")>-1?e.hash.substring(e.hash.lastIndexOf("?")+1):"",r=new URLSearchParams(n);r.set(g.VehicleId,t),e.hash=`${e.hash.replace(`?${n}`,"")}?${r.toString()}`},M="OWC_AEM_ENV";new class extends r{constructor(){super(M)}get initialState(){const e=window.aemNamespace?.environmentVariables||window.top?.aemNamespace?.environmentVariables,t=window.aemNamespace?.pageEnvironmentVariables||window.top?.aemNamespace?.pageEnvironmentVariables;return e&&t?{...e,...t}:{}}getReducer(){return()=>this.initialState}getPublicDispatchers(){return{}}};const _=e=>{const t=a(),r=a=>{e(a,(()=>t.unsubscribe(n,r)))};t.subscribe(n,r)},I=()=>new Promise((e=>{a().once(M,e)})),T=()=>new Promise((e=>{a().once(n,e)})),O=n=>{_((async({cta:r,configurationId:a},o)=>{if(n!==a&&r){o();const n=B(r,a);(await t()).setCta(n);e(i).log(`Updated CTAs with configurationID: ${a} added`),O(a)}}))},q=e=>{sessionStorage.setItem(s,String(e))},L=()=>new Promise((e=>{window.top.seamlessStore.initializeStore().once(M,e)})),z=e("cta-automation-service"),x=async()=>{const e=await(async()=>{let e;try{return e=await fetch(await async function(){const{language:e,country:t,assetUri:n}=await L();return`${n}/globalelem-phrase-translations/latest/translations/${e}-${("AQ"===t?"DE":t).toUpperCase()}.all.json`}()),e.json()}catch(e){z.error("Failed to fetch translations, trying to fetch default.",e)}try{return e=await fetch(await async function(){const{assetUri:e}=await L();return`${e}/globalelem-phrase-translations/latest/translations/en-GB.all.json`}()),e?.json()}catch(e){z.error("Failed to fetch default translation.",e)}return null})();return e&&e.find((e=>"makeAnEnquiry"===e.id))?.translation||""},H=async e=>({url:e?.url||"#",label:await x()}),j="generic-product-template",G=e(i),F="https://api.oneweb.mercedes-benz.com",V=(e,t)=>{e&&G.error(t)},Q=async(e,t=[],n,r,a,o="")=>{const{country:i,language:s,runMode:u}=await I(),d=U(F,{runMode:u,country:i,language:s,modelSeries:e,subBrands:t,engineConcept:o});let l,f;try{l=await N(d),l?G.log(`Data for ${e} has been loaded:`,l):G.error(`No data loaded from deep link service for ${e}.`)}catch(e){G.error(e.message)}finally{l={...l},l[c.Enquiry]=await H(n);const t=S(l,!1,r);G.log("Mapping buttons for the following scenario:",t);const o=A(l,...t);o&&(f=B(o,a),G.log(`Buttons for ${e} have been mapped:`,f))}return f},Y="price-automation-service",J=e(Y);var K;!function(e){e.Price="PRICE"}(K||(K={}));const W=(e,t)=>{const n=`${e.language}-${e.country}`,r=Number("min"===t?e.min:e.max);return o(r,n)},X=e(Y),Z=async(e,t,n=[])=>{const{apiUri:r,country:a,language:o,templateName:i}=await I();if(i===j)if(r&&e)try{const i=((e,{country:t,language:n,modelSeries:r,engineConcept:a,subBrands:o})=>{const i=new URL(`${e}/vmds-highlights-api/v1/highlights/${t}/${n}/${r}`);for(const e of o)i.searchParams.append("subBrands",e);return a&&i.searchParams.append("engineConcept",a),i.toString()})(r,{country:a,language:o,modelSeries:e,engineConcept:t,subBrands:n}),s=await(async e=>{try{J.log(`Sending request to ${e}`);const t=await fetch(e),n=await t.json();if(t.ok)return n}catch(e){throw Error("Error caused during processing request to VMDS service")}})(i);if(!s)return void X.error(`No data loaded from VMDS service for ${e}.`);X.log(`Data for ${e} has been loaded:`,s);const c=((e,t,n)=>{const r=e.i18n,a=e.highlights[K.Price],{unit:o,footnoteI18nKey:i}=a,s={min:String(a.min),max:String(a.max),unit:a.unit,language:t,country:n},c={unit:o,min:W(s,"min"),max:W(s,"max"),pricePrefixLabel:r.PRICE};return i&&r[i]&&(c.priceFootnoteLabel=r[i]),c})(s,o,a);return X.log(`Formatted price for ${e} have been returned:`,c),c}catch(e){X.error(e.message)}else X.error("No apiUri or modelSeries found, skipping price service initialization.");else X.log("Skipping price automation service initialization outside of product page.")};_((async({modelSeries:e,engineConcept:n,subBrands:r},a)=>{a();const o=await Z(e,n,r);o?((await t()).setPrice(o),X.log(`Price data for ${e} have been added to the store:`,o)):X.error(`No price data for ${e} added to the store.`)})),(q(!1),new Promise((e=>{_((async({modelSeries:n,subBrands:r,cta:a={},engineConcept:o},i)=>{try{const{runMode:u,country:d,language:l,templateName:f}=await I(),{enquiry:g}=a;let p;if(f!==j)return void G.log("Skipping CTA service initialization outside of product page.");if("true"===sessionStorage.getItem(s))return void G.log("CTA service is already initialized");V(!n,"No apiUri or modelSeries found, skipping CTA service initialization"),V(!g,`No enquiry configuration loaded for ${n}`),q(!0);const m=U(F,{runMode:u,country:d,language:l,modelSeries:n,subBrands:r,engineConcept:o});p=await N(m),p?G.log(`Data for ${n} has been loaded:`,p):G.error(`No data loaded from deep link service for ${n}.`),p={...p,[c.Enquiry]:await H(g)},R(p);const h=S(p,!0,a.scenario);G.log("Mapping buttons for the following scenario:",h);const w=A(p,...h);w&&(i&&i(),(await t()).setCta(w),G.log(`Buttons for ${n} have been added to the store:`,w),e())}catch(e){G.error(e)}}))}))).then((()=>{O("")}));export{c as CTATypes,l as CtaIcons,j as PRODUCT_PAGE_TEMPLATE,Q as loadCtaConfiguration,Z as loadPriceData,I as provideDataFromAemEnvStoreOnce,_ as provideDataFromVehicleDataStoreByCallback,T as provideDataFromVehicleDataStoreOnce};
//# sourceMappingURL=index.mjs.map
