import{U as N,Q as O,z as R,ah as j,a1 as v,av as q,aw as B,$ as K,a0 as M}from"./main-DHoZdmBw.js";const b="site-login-init",ne="login-dialog-user",J="login-dialog",A="logged-in",z="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.-",C="site-login-debug",P="site-login",_=()=>{const t=new Uint32Array(16);return(window.crypto||window.msCrypto).getRandomValues(t),Array.from(t).map(o=>z.charAt(o%z.length)).join("")},V=t=>t.replace(/-/g,"+").replace(/_/g,"/"),se=(t,n)=>{try{const[,o]=t.split("."),a=atob(V(o)),{nonce:g}=JSON.parse(a);return g!==n}catch{return!0}},L=(t={},n={},o="https://api.fischer.group/authorizationserver/oauth/authorize")=>{const a=new URLSearchParams;[t,n].forEach(d=>Object.entries(d).forEach(([c,p])=>a.set(c,String(p))));const g=Object.keys(t).length?"?":"#";return`${o.split("?")[0]}${g}${a}`},H=(t,n)=>{const o=t.get(b)||{};return n("get cookies",o==null?void 0:o.time),o},Q=async(t,n,o,a,g,d)=>{const c=Date.now();return n("setting cookies",b,c),d.time=c,o.set(b,d,"1h","/",t.hostname(),t.isSecureConnection(),"Lax"),a().time!==c&&(console.info("cookie was not stored, waiting a moment"),await new Promise(p=>setTimeout(p,500)),a().time!==c)?(g("Failed to set cookies"),!1):(n("cookies set"),!0)},ae=t=>{var n;return t.query(C)||new RegExp(`(?:#|&)${C}=true(?:&|$)`).test(window.location.hash)?((n=window.activateLoginDebug)==null||n.call(window,!0),!0):!1},W=async(t,n,o,a)=>{var g;try{t("posting token to sitecore...");const c=(g=(await N.post("/webapi/shop/userapi/Login",{User:{AccessToken:o,IdToken:a,IsIdsSession:O()}})).data)==null?void 0:g.LoginSuccessPage;return t("posted token to sitecore",c),await n("login",{userId:R(a).email}),c}catch{throw new Error}},G=(t,n,o,a,g,d,c,p)=>{t("initializing authorization");try{const f={response_type:p,client_id:n,redirect_uri:a||String(window.location).split("#")[0],scope:c,state:g,nonce:d};o&&new URLSearchParams(o.replace(/^.*\?/,"")).forEach((m,y)=>{f[m]||(f[m]=y)}),window.location.href=L(f)}catch(f){console.error("SSO login failed",f)}},X=(t,n,o,a)=>{},ie=()=>{const t=j(),n=v(!0),o=v(!1),{cookies:a}=q(),{sendEvent:g}=B(),d=K("fields"),c=(...e)=>{console.error(...e),o.value=!0},p=e=>e?`${e.replace(/(\?|&)state=[^&]*/,"").replace(/(\?|&)nonce=[^&]*/,"")}&state=retry`:null,f=M(()=>{const e=t.query("authorize_redirect");return p(e)}),h=(...e)=>{n.value&&console.info(...e)},m=()=>H(a,h),y=async e=>Q(t,h,a,m,c,e),$=async(e,s)=>{try{return await W(h,g,e,s)}catch(i){throw c("Could not post access token",i),i}},D=()=>t.baseSiteId().toLowerCase(),E=(e,s,i,r,l)=>G(h,D(),f.value,e,s,i,r,l),F=e=>{const s=new Date().getTime(),i=Object.keys(e),r=[],l=10;i.forEach(u=>{if(/^(?:state|nonce)-/.test(u)){const{validUntil:w}=e[u];w>s?r.push([w,u]):(console.info("Removing outdated cookie",w),delete e[u])}}),r.length>l&&(console.info("Max login cookies exceeded"),r.sort((u,w)=>w[0]-u[0]),r.slice(l).forEach(u=>delete e[u[1]]))},U=async e=>{const s=_(),i=_(),r=e||t.query("redirect")||t.uri(),l=t.cartId(),u=new Date().getTime()+60*60*5*1e4,w={redirect:r,cartId:l,validUntil:u},k=m();if(F(k),k[`state-${s}`]=w,k[`nonce-${s}`]={value:i,validUntil:u},!await y(k))throw new Error("Could not create token state");return{tokenStateId:s,tokenNonce:i}},S=async e=>{var I;const s=t.pages.login().url,i=s?`${t.hostUrlBase()}${s}`:String(window.location).split("#")[0];h("redirecting to loginFormPage");const r=t.baseSiteId().toLowerCase(),l=e?`dialog:${window.location.pathname}`:r.split("-")[0],{tokenStateId:u,tokenNonce:w}=await U(e?l:void 0),k=t.siteLanguage(),x={brand:l,ui_locales:k,api_key:((I=d==null?void 0:d.apiKey)==null?void 0:I.value)||""},T={response_type:"id_token token",scope:"openid",redirect_uri:i,state:u,nonce:w,brand:l,client_id:r,ui_hint:k};return n.value&&(T[C]=!0),{queryParams:x,hashParams:T,brand:l}};return{isDebug:n,hasCriticalLoginFailure:o,logDebug:h,postAccessTokenInSession:$,initializeAuthorization:async e=>{const{queryParams:s,hashParams:i,brand:r}=await S(e),l=L(s,i);if(h("new location",l),e&&(h("setting login-dialog",r),a.set(J,r)),t.query("debug-no-redirect")){console.info("debug-no-redirect");return}window.location.href=l},authorizeRedirect:f,getAuthorizeRedirect:p,getCookies:m,setCookies:y,setDevToken:(e,s)=>X(),criticalLoginFailure:c,goToLoginPage:E,watchTabLogins:e=>{try{const s=new BroadcastChannel(P);return s.onmessage=i=>{const[r]=(i==null?void 0:i.data)||[];r===A?e():console.error("unknown login bc event")},h("created login bc"),s}catch(s){return console.error("Could not create login bc",s),null}},notifyLogin:()=>{try{const e=new BroadcastChannel(P);e==null||e.postMessage([A]),h("notified login")}catch(e){console.error("Could not notify login",e)}},getAuthParams:S,getAuthorizationUrl:L}};export{L as a,C as b,ie as c,z as d,G as e,H as g,ae as h,se as i,J as l,W as p,Q as s,ne as u};