{"version":3,"file":"CookieConsentContext.cjs","sources":["../../../../src/components/cookie-consent/CookieConsentContext.tsx"],"sourcesContent":["import React, { useContext, useMemo, useState } from \"react\";\nimport {\n    buildRequirementsObject,\n    getConsentCookie,\n    shouldShowConsentDialog,\n} from \"./cookieConsentUtils.js\";\nimport type {\n    Consent,\n    CookieConsentProviderProps,\n    InternalContext,\n} from \"./types.js\";\n\nexport const DEFAULT_COOKIE_NAME = \"fremtind-cookie-consent\";\n\nconst Context = React.createContext<InternalContext | undefined>(undefined);\n\nexport const CookieConsentProvider: React.FC<CookieConsentProviderProps> = ({\n    children,\n    cookieAdapter,\n    functional,\n    statistics,\n    cookieName = DEFAULT_COOKIE_NAME,\n    cookieDomain,\n    cookiePath,\n}) => {\n    const [timestamp, setTimestamp] = useState(() => Date.now());\n\n    const requirement = useMemo(\n        () => buildRequirementsObject({ functional, statistics }),\n        [functional, statistics],\n    );\n\n    /* Use timestamp as a dependency to be able to force re-reading of cookie */\n    // biome-ignore lint/correctness/useExhaustiveDependencies:\n    const consentCookie = useMemo(() => {\n        return (\n            getConsentCookie({ adapter: cookieAdapter, name: cookieName }) ?? {\n                functional: null,\n                statistics: null,\n            }\n        );\n    }, [cookieAdapter, cookieName, timestamp]);\n\n    const [isOpen, setIsOpen] = useState(() => {\n        return shouldShowConsentDialog(requirement, consentCookie);\n    });\n\n    return (\n        <Context.Provider\n            value={{\n                isOpen,\n                setIsOpen,\n                updateCurrentConsents: () => setTimestamp(Date.now()),\n                requirement,\n                currentConsent: consentCookie,\n                cookieName,\n                cookieDomain,\n                cookiePath,\n            }}\n        >\n            {children}\n        </Context.Provider>\n    );\n};\n\nexport const useInternalState = () => {\n    const context = React.useContext(Context);\n    if (context === undefined) {\n        throw new Error(\n            \"CookieConsent must be used within a CookieConsentProvider\",\n        );\n    }\n\n    return context;\n};\n\ntype UseCookieConsent = {\n    openConsentModal: () => void;\n    consents: Consent;\n};\n\nexport const useCookieConsent = (): UseCookieConsent => {\n    const context = useContext(Context);\n\n    if (context === undefined) {\n        throw new Error(\n            \"useCookieConsent must be used within a CookieConsentProvider\",\n        );\n    }\n\n    const openConsentModal = () => {\n        context.setIsOpen(true);\n    };\n\n    const consents = context.currentConsent;\n\n    return { openConsentModal, consents };\n};\n"],"names":["DEFAULT_COOKIE_NAME","Context","React","createContext","children","cookieAdapter","functional","statistics","cookieName","cookieDomain","cookiePath","timestamp","setTimestamp","useState","Date","now","requirement","useMemo","buildRequirementsObject","consentCookie","getConsentCookie","adapter","name","isOpen","setIsOpen","shouldShowConsentDialog","jsx","Provider","value","updateCurrentConsents","currentConsent","context","useContext","Error","openConsentModal","consents"],"mappings":"8KAYaA,EAAsB,0BAE7BC,EAAUC,EAAMC,mBAA2C,iCAEU,EACvEC,SAAAA,EACAC,cAAAA,EACAC,WAAAA,EACAC,WAAAA,EACAC,WAAAA,EAAaR,EACbS,aAAAA,EACAC,WAAAA,MAEA,MAAOC,EAAWC,GAAgBC,EAAAA,SAAS,IAAMC,KAAKC,OAEhDC,EAAcC,EAAAA,QAChB,IAAMC,0BAAwB,CAAEZ,WAAAA,EAAYC,WAAAA,IAC5C,CAACD,EAAYC,IAKXY,EAAgBF,EAAAA,QAAQ,IAEtBG,EAAAA,iBAAiB,CAAEC,QAAShB,EAAeiB,KAAMd,KAAiB,CAC9DF,WAAY,KACZC,WAAY,MAGrB,CAACF,EAAeG,EAAYG,KAExBY,EAAQC,GAAaX,EAAAA,SAAS,IAC1BY,EAAAA,wBAAwBT,EAAaG,IAGhD,OACIO,EAAAA,IAACzB,EAAQ0B,SAAR,CACGC,MAAO,CACHL,OAAAA,EACAC,UAAAA,EACAK,sBAAuB,IAAMjB,EAAaE,KAAKC,OAC/CC,YAAAA,EACAc,eAAgBX,EAChBX,WAAAA,EACAC,aAAAA,EACAC,WAAAA,GAGHN,SAAAA,4DAqBmB,KAC5B,MAAM2B,EAAUC,EAAAA,WAAW/B,GAE3B,QAAgB,IAAZ8B,EACA,MAAM,IAAIE,MACN,gEAUR,MAAO,CAAEC,iBANgB,KACrBH,EAAQP,WAAU,IAKKW,SAFVJ,EAAQD,0CA7BG,KAC5B,MAAMC,EAAU7B,EAAM8B,WAAW/B,GACjC,QAAgB,IAAZ8B,EACA,MAAM,IAAIE,MACN,6DAIR,OAAOF"}