{"version":3,"file":"cookieConsentUtils.cjs","sources":["../../../../src/components/cookie-consent/cookieConsentUtils.ts"],"sourcesContent":["import type { Consent, ConsentRequirement, ConsentState } from \"./types.js\";\n\nconst getCookie = (name: string) => {\n    if (typeof document === \"undefined\") {\n        return undefined;\n    }\n\n    const cookie = document.cookie\n        // split a string of cookies into array of cookies\n        .split(\";\")\n        // split cookies into [name, value]\n        .map((s) => s.trim().split(\"=\"))\n        // find our golden nugget\n        .find((c) => c[0] === name);\n\n    if (!cookie) {\n        return undefined;\n    }\n\n    return cookie;\n};\n\nexport const getConsentCookie = ({\n    adapter,\n    name,\n}: {\n    adapter?: () => Consent | undefined;\n    name: string;\n}): Consent | undefined => {\n    const cookie = getCookie(name);\n\n    if (cookie) {\n        const consent = JSON.parse(cookie[1]);\n\n        return consent as Consent;\n    }\n\n    if (adapter) {\n        return adapter();\n    }\n\n    return undefined;\n};\n\n// 120 days\nconst DEFAULT_MAX_AGE = 10368000;\n\nexport const setConsentCookie = ({\n    consent,\n    maxAge = DEFAULT_MAX_AGE,\n    name,\n    domain,\n    path,\n}: {\n    consent: Consent;\n    maxAge?: number;\n    name: string;\n    domain?: string;\n    path?: string;\n}): void => {\n    document.cookie = [\n        `${name}=${JSON.stringify(consent)}`,\n        `max-age=${maxAge}`,\n        \"SameSite=Lax\",\n        !!domain && `domain=${domain}`,\n        !!path && `path=${path}`,\n    ]\n        .filter((f) => f)\n        .join(\";\");\n};\n\nexport const shouldShowConsentDialog = (\n    requirement: ConsentRequirement,\n    consent: Consent | undefined,\n): boolean => {\n    if (!consent) {\n        // check if requirement has truthy values. should show consent if it has\n        return Object.values(requirement).some(\n            (requirementValue) => requirementValue,\n        );\n    }\n\n    // convert to a map to ease accessing dynamic keys\n    const consentMap = new Map(Object.entries(consent));\n\n    // pls spare the CPU of converting to an entries array 3 times\n    const requirementEntries = Object.entries(requirement);\n\n    for (const [name, required] of requirementEntries) {\n        // no need to check more if the value isn't required\n        if (!required) {\n            continue;\n        }\n\n        // cancel and show consent if a value isn't decided\n        if (consentMap.get(name) === null) {\n            return true;\n        }\n    }\n\n    return false;\n};\n\nexport const convertBooleanToConsentValue = (\n    formValue: boolean | undefined,\n): ConsentState => {\n    if (typeof formValue === \"undefined\") {\n        return null;\n    }\n\n    if (formValue === false || String(formValue).toLowerCase() === \"false\") {\n        return \"denied\";\n    }\n\n    return \"accepted\";\n};\n\nexport const convertBooleanConsentObjectToConsentObject = (\n    consent: Partial<Record<keyof Consent, boolean | undefined>>,\n    requirement: ConsentRequirement,\n): Consent => {\n    return {\n        functional: requirement.functional\n            ? convertBooleanToConsentValue(consent.functional)\n            : undefined,\n        statistics: requirement.statistics\n            ? convertBooleanToConsentValue(consent.statistics)\n            : undefined,\n    };\n};\n\nexport const buildRequirementsObject = ({\n    functional,\n    statistics,\n}: ConsentRequirement) => {\n    return {\n        ...(functional && { functional }),\n        ...(statistics && { statistics }),\n    };\n};\n"],"names":["convertBooleanToConsentValue","formValue","String","toLowerCase","functional","statistics","consent","requirement","adapter","name","cookie","document","split","map","s","trim","find","c","getCookie","JSON","parse","maxAge","domain","path","stringify","filter","f","join","Object","values","some","requirementValue","consentMap","Map","entries","requirementEntries","required","get"],"mappings":"gFAEA,MAqGaA,EACTC,UAEWA,EAAc,IACd,MAGO,IAAdA,GAA2D,UAApCC,OAAOD,GAAWE,cAClC,SAGJ,2CAiB4B,EACnCC,WAAAA,EACAC,WAAAA,MACJ,IAEYD,GAAc,CAAEA,WAAAA,MAChBC,GAAc,CAAEA,WAAAA,wDApB8B,CACtDC,EACAC,KAAAA,CAGIH,WAAYG,EAAYH,WAClBJ,EAA6BM,EAAQF,iBACrC,EACNC,WAAYE,EAAYF,WAClBL,EAA6BM,EAAQD,iBACrC,oEAzGkB,EAC5BG,QAAAA,EACAC,KAAAA,MAKA,MAAMC,EA3BSD,KACf,UAAWE,SAAa,IACpB,OAGJ,MAAMD,EAASC,SAASD,OAEnBE,MAAM,KAENC,IAAKC,GAAMA,EAAEC,OAAOH,MAAM,MAE1BI,KAAMC,GAAMA,EAAE,KAAOR,GAE1B,OAAKC,QAAL,GAceQ,CAAUT,GAEzB,OAAIC,EACgBS,KAAKC,MAAMV,EAAO,IAKlCF,EACOA,SADX,4BAU4B,EAC5BF,QAAAA,EACAe,OAAAA,EAJoB,QAKpBZ,KAAAA,EACAa,OAAAA,EACAC,KAAAA,MAQAZ,SAASD,OAAS,CACd,GAAGD,KAAQU,KAAKK,UAAUlB,KAC1B,WAAWe,IACX,iBACEC,GAAU,UAAUA,MACpBC,GAAQ,QAAQA,KAEjBE,OAAQC,GAAMA,GACdC,KAAK,sCAGyB,CACnCpB,EACAD,KAEA,IAAKA,EAED,OAAOsB,OAAOC,OAAOtB,GAAauB,KAC7BC,GAAqBA,GAK9B,MAAMC,EAAa,IAAIC,IAAIL,OAAOM,QAAQ5B,IAGpC6B,EAAqBP,OAAOM,QAAQ3B,GAE1C,IAAA,MAAYE,EAAM2B,KAAaD,EAE3B,GAAKC,GAKwB,OAAzBJ,EAAWK,IAAI5B,GACf,OAAO,EAIf,OAAO"}