{"version":3,"file":"set-theme-cookie.mjs","sources":["../../../../packages/theme/utils/set-theme-cookie.ts"],"sourcesContent":["import { UAParser } from 'ua-parser-js'\nimport { getTopLevelDomain } from '../../utils/index.js'\nimport { themeCookieMaxAge, themeCookieKey, ThemeName } from '../constants.js'\n\nconst parser = new UAParser()\n\nconst setSecureCookie = (cookie: string) => {\n  // 1. we want this cookie to be available on HTTP websites too.\n  // 2. there is a bug on localhost which causes Chrome to ignore cookies set without Secure,\n  // and Safari when cookies are set with Secure, so we're forcing cookie into both\n  if (parser.getBrowser()?.name?.toLowerCase() === 'safari') {\n    if (window.location.protocol !== 'https:') {\n      document.cookie = cookie\n    } else {\n      document.cookie = `${cookie}Secure;`\n    }\n  } else {\n    document.cookie = `${cookie}Secure;`\n  }\n}\n\nexport const setThemeCookie = (theme: ThemeName) => {\n  const cookie = `${themeCookieKey}=${theme};max-age=${themeCookieMaxAge};path=/;samesite=None;`\n  // For top level domain - *.some-domain.fi\n  setSecureCookie(`${cookie}domain=${getTopLevelDomain()};`)\n\n  if (!document.cookie.includes(`${themeCookieKey}=${theme}`)) {\n    // For specific.domain.fi, if cookie can't be set to top level domain\n    setSecureCookie(cookie)\n  }\n}\n"],"names":["parser","UAParser","setSecureCookie","cookie","getBrowser","name","toLowerCase","window","location","protocol","document","setThemeCookie","theme","themeCookieKey","themeCookieMaxAge","getTopLevelDomain","includes"],"mappings":";;;;;;AAIA,MAAMA,MAAM,GAAG,IAAIC,QAAQ,EAAE,CAAA;AAE7B,MAAMC,eAAe,GAAIC,MAAc,IAAK;AAC1C;AACA;AACA;AACA,EAAA,IAAIH,MAAM,CAACI,UAAU,EAAE,EAAEC,IAAI,EAAEC,WAAW,EAAE,KAAK,QAAQ,EAAE;AACzD,IAAA,IAAIC,MAAM,CAACC,QAAQ,CAACC,QAAQ,KAAK,QAAQ,EAAE;MACzCC,QAAQ,CAACP,MAAM,GAAGA,MAAM,CAAA;AAC1B,KAAC,MAAM;AACLO,MAAAA,QAAQ,CAACP,MAAM,GAAG,CAAA,EAAGA,MAAM,CAAS,OAAA,CAAA,CAAA;AACtC,KAAA;AACF,GAAC,MAAM;AACLO,IAAAA,QAAQ,CAACP,MAAM,GAAG,CAAA,EAAGA,MAAM,CAAS,OAAA,CAAA,CAAA;AACtC,GAAA;AACF,CAAC,CAAA;AAEYQ,MAAAA,cAAc,GAAIC,KAAgB,IAAK;EAClD,MAAMT,MAAM,GAAG,CAAGU,EAAAA,cAAc,IAAID,KAAK,CAAA,SAAA,EAAYE,iBAAiB,CAAwB,sBAAA,CAAA,CAAA;AAC9F;EACAZ,eAAe,CAAC,GAAGC,MAAM,CAAA,OAAA,EAAUY,iBAAiB,EAAE,GAAG,CAAC,CAAA;AAE1D,EAAA,IAAI,CAACL,QAAQ,CAACP,MAAM,CAACa,QAAQ,CAAC,CAAA,EAAGH,cAAc,CAAA,CAAA,EAAID,KAAK,CAAA,CAAE,CAAC,EAAE;AAC3D;IACAV,eAAe,CAACC,MAAM,CAAC,CAAA;AACzB,GAAA;AACF;;;;"}