{"version":3,"file":"context-cookie-qk-RTVhn.cjs","names":[],"sources":["../node_modules/js-cookie/dist/js.cookie.mjs","../src/contexts/context-cookie/context.tsx","../src/contexts/context-cookie/context.hooks.ts"],"sourcesContent":["/*! js-cookie v3.0.5 | MIT */\n/* eslint-disable no-var */\nfunction assign (target) {\n  for (var i = 1; i < arguments.length; i++) {\n    var source = arguments[i];\n    for (var key in source) {\n      target[key] = source[key];\n    }\n  }\n  return target\n}\n/* eslint-enable no-var */\n\n/* eslint-disable no-var */\nvar defaultConverter = {\n  read: function (value) {\n    if (value[0] === '\"') {\n      value = value.slice(1, -1);\n    }\n    return value.replace(/(%[\\dA-F]{2})+/gi, decodeURIComponent)\n  },\n  write: function (value) {\n    return encodeURIComponent(value).replace(\n      /%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g,\n      decodeURIComponent\n    )\n  }\n};\n/* eslint-enable no-var */\n\n/* eslint-disable no-var */\n\nfunction init (converter, defaultAttributes) {\n  function set (name, value, attributes) {\n    if (typeof document === 'undefined') {\n      return\n    }\n\n    attributes = assign({}, defaultAttributes, attributes);\n\n    if (typeof attributes.expires === 'number') {\n      attributes.expires = new Date(Date.now() + attributes.expires * 864e5);\n    }\n    if (attributes.expires) {\n      attributes.expires = attributes.expires.toUTCString();\n    }\n\n    name = encodeURIComponent(name)\n      .replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent)\n      .replace(/[()]/g, escape);\n\n    var stringifiedAttributes = '';\n    for (var attributeName in attributes) {\n      if (!attributes[attributeName]) {\n        continue\n      }\n\n      stringifiedAttributes += '; ' + attributeName;\n\n      if (attributes[attributeName] === true) {\n        continue\n      }\n\n      // Considers RFC 6265 section 5.2:\n      // ...\n      // 3.  If the remaining unparsed-attributes contains a %x3B (\";\")\n      //     character:\n      // Consume the characters of the unparsed-attributes up to,\n      // not including, the first %x3B (\";\") character.\n      // ...\n      stringifiedAttributes += '=' + attributes[attributeName].split(';')[0];\n    }\n\n    return (document.cookie =\n      name + '=' + converter.write(value, name) + stringifiedAttributes)\n  }\n\n  function get (name) {\n    if (typeof document === 'undefined' || (arguments.length && !name)) {\n      return\n    }\n\n    // To prevent the for loop in the first place assign an empty array\n    // in case there are no cookies at all.\n    var cookies = document.cookie ? document.cookie.split('; ') : [];\n    var jar = {};\n    for (var i = 0; i < cookies.length; i++) {\n      var parts = cookies[i].split('=');\n      var value = parts.slice(1).join('=');\n\n      try {\n        var found = decodeURIComponent(parts[0]);\n        jar[found] = converter.read(value, found);\n\n        if (name === found) {\n          break\n        }\n      } catch (e) {}\n    }\n\n    return name ? jar[name] : jar\n  }\n\n  return Object.create(\n    {\n      set,\n      get,\n      remove: function (name, attributes) {\n        set(\n          name,\n          '',\n          assign({}, attributes, {\n            expires: -1\n          })\n        );\n      },\n      withAttributes: function (attributes) {\n        return init(this.converter, assign({}, this.attributes, attributes))\n      },\n      withConverter: function (converter) {\n        return init(assign({}, this.converter, converter), this.attributes)\n      }\n    },\n    {\n      attributes: { value: Object.freeze(defaultAttributes) },\n      converter: { value: Object.freeze(converter) }\n    }\n  )\n}\n\nvar api = init(defaultConverter, { path: '/' });\n/* eslint-enable no-var */\n\nexport { api as default };\n","import { logger } from '@local/cores/logger';\n\nimport Cookies from 'js-cookie';\nimport { createContext, FC, useCallback, useEffect, useState } from 'react';\n\nimport { ICookieAttributes, ICookieContext, ICookieProvider, IValidCookieObject } from './context.types';\n\n/**\n * Context for managing cookies.\n */\nexport const CookieContext = createContext<ICookieContext | null>(null);\n\n/**\n * Provider component for managing cookies.\n *\n * @remarks\n * You should understand which cookies you are changing and whether you have access to them.\n *\n * @remarks\n * The local storage is typed using the `jenesei-kit-react.d.ts` file:\n *\n * ```typescript\n * import '@jenesei-software/jenesei-kit-react'\n *\n * declare module '@jenesei-software/jenesei-kit-react' {\n *   export interface IValidCookieObject {\n *    access_token: string\n *    refresh_token: string\n *   }\n * }\n * ```\n */\nexport const ProviderCookie: FC<ICookieProvider> = (props) => {\n  const { getCookie, setCookie, removeCookieValue, removeCookieValues, checkCookie, cookieValues } =\n    useProviderCookie(props);\n\n  return (\n    <CookieContext.Provider\n      value={{\n        getCookie,\n        setCookie,\n        removeCookieValue,\n        removeCookieValues,\n        checkCookie,\n        cookieValues,\n      }}\n    >\n      {props.children}\n    </CookieContext.Provider>\n  );\n};\n\nconst useProviderCookie = (props: ICookieProvider) => {\n  const [cookieValues, setCookieValues] = useState<IValidCookieObject>();\n\n  const getCookie = useCallback(<K extends keyof IValidCookieObject>(name: K): IValidCookieObject[K] | undefined => {\n    const cookie = Cookies.get(String(name));\n    setCookieValues((prevState) => ({\n      ...prevState,\n      [name]: cookie ? JSON.parse(cookie) : undefined,\n    }));\n    return cookie ? JSON.parse(cookie) : undefined;\n  }, []);\n\n  const changeCookie = useCallback(\n    <K extends keyof IValidCookieObject>(name: K, value: IValidCookieObject[K], options?: ICookieAttributes) => {\n      try {\n        Cookies.set(String(name), JSON.stringify(value), options);\n        setCookieValues((prevState) => ({ ...prevState, [name]: value }));\n      } catch {\n        logger.info(`Provider Cookie. ChangeCookie error - key:${name}, value:${value}.`);\n      }\n    },\n    [],\n  );\n\n  const removeCookieValue = useCallback(<K extends keyof IValidCookieObject>(name: K, options?: ICookieAttributes) => {\n    try {\n      Cookies.remove(String(name), options);\n      setCookieValues((prevState) => ({ ...prevState, [name]: undefined }));\n    } catch {\n      logger.info(`Provider Cookie. RemoveCookieValue error - key:${name}.`);\n    }\n  }, []);\n\n  const removeCookieValues = useCallback(() => {\n    if (props.validate?.validateKeys) {\n      props.validate?.validateKeys.forEach((key) => {\n        removeCookieValue(String(key) as never);\n      });\n    } else {\n      logger.info('Provider Cookie. Validate is not defined.');\n    }\n  }, [props.validate, removeCookieValue]);\n\n  const checkCookie = useCallback(() => {\n    if (props.validate?.validateKeys && props.validate.getValidateCookieValue) {\n      props.validate?.validateKeys.forEach((key) => {\n        const cookieValue = Cookies.get(String(key));\n        if (cookieValue) {\n          try {\n            const parsedValue = JSON.parse(cookieValue);\n            if (!props.validate?.getValidateCookieValue(String(key) as never, parsedValue as never)) {\n              removeCookieValue(String(key) as never);\n            } else {\n              setCookieValues((prevState) => ({\n                ...prevState,\n                [key]: parsedValue,\n              }));\n            }\n          } catch {\n            removeCookieValue(String(key) as never);\n          }\n        } else {\n          removeCookieValue(String(key) as never);\n        }\n      });\n    } else {\n      logger.info('Provider Cookie. Validate is not defined.');\n    }\n  }, [props.validate, removeCookieValue]);\n\n  useEffect(() => {\n    checkCookie();\n  }, [checkCookie]);\n\n  useEffect(() => {\n    return () => {\n      setCookieValues(undefined);\n    };\n  }, []);\n  return { getCookie, setCookie: changeCookie, removeCookieValue, removeCookieValues, checkCookie, cookieValues };\n};\n","import { useContext } from 'react';\n\nimport { CookieContext } from './context';\nimport { ICookieContext } from './context.types';\n\n/**\n * Custom hook to access the CookieContext.\n *\n * @remarks\n * You should understand which cookies you are changing and whether you have access to them.\n *\n */\nexport const useCookie = (): ICookieContext => {\n  const context = useContext(CookieContext);\n  if (!context) {\n    throw new Error('useCookie must be used within an ProviderCookie');\n  }\n  return context;\n};\n"],"x_google_ignoreList":[0],"mappings":"iIAEA,SAAS,EAAQ,EAAQ,CACvB,QAAS,EAAI,EAAG,EAAI,UAAU,OAAQ,IAAK,CACzC,IAAI,EAAS,UAAU,CAAA,EACvB,QAAS,KAAO,EACd,EAAO,CAAA,EAAO,EAAO,CAAA,EAGzB,OAAO,EAKT,IAAI,EAAmB,CACrB,KAAM,SAAU,EAAO,CACrB,OAAI,EAAM,CAAA,IAAO,MACf,EAAQ,EAAM,MAAM,EAAG,EAAA,GAElB,EAAM,QAAQ,mBAAoB,kBAAA,GAE3C,MAAO,SAAU,EAAO,CACtB,OAAO,mBAAmB,CAAA,EAAO,QAC/B,2CACA,kBAAA,IAQN,SAAS,EAAM,EAAW,EAAmB,CAC3C,SAAS,EAAK,EAAM,EAAO,EAAY,CACrC,GAAI,SAAO,SAAa,KAIxB,CAAA,EAAa,EAAO,CAAA,EAAI,EAAmB,CAAA,EAEvC,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