{"version":3,"file":"cookie.mjs","names":[],"sources":["../../../../packages/utils/cookie.ts"],"sourcesContent":["const assign = function (target: any, defaultAttributes: any, attributes: any) {\n  for (let i = 1; i < arguments.length; i++) {\n    const source = arguments[i]\n    for (let key in source) {\n      target[key] = source[key]\n    }\n  }\n  return target\n}\n\nconst defaultConverter = {\n  read: function (value: any) {\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: any) {\n    return encodeURIComponent(value).replace(\n      /%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g,\n      decodeURIComponent\n    )\n  }\n}\n\nfunction init(converter: any, defaultAttributes: any) {\n  function set(name: any, value: any, attributes: any) {\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    let stringifiedAttributes = ''\n    for (let 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: any) {\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    const cookies = document.cookie ? document.cookie.split('; ') : []\n    const jar: any = {}\n    for (let i = 0; i < cookies.length; i++) {\n      const parts = cookies[i].split('=')\n      const value = parts.slice(1).join('=')\n\n      try {\n        const found = decodeURIComponent(parts[0])\n        if (!(found in jar)) jar[found] = converter.read(value, found)\n        if (name === found) {\n          break\n        }\n      } catch (e) {\n        // Do nothing...\n      }\n    }\n\n    return name ? jar[name] : jar\n  }\n\n  return Object.create(\n    {\n      set,\n      get,\n      remove: function (name: any, attributes: any) {\n        set(\n          name,\n          '',\n          assign({}, attributes, {\n            expires: -1\n          })\n        )\n      },\n      withAttributes: function (attributes: any) {\n        return init(this.converter, assign({}, this.attributes, attributes))\n      },\n      withConverter: function (converter: any) {\n        return init(assign({}, this.converter, converter), this.attributes)\n      }\n    } as any,\n    {\n      attributes: { value: Object.freeze(defaultAttributes) },\n      converter: { value: Object.freeze(converter) }\n    }\n  )\n}\n\nexport const Cookies = init(defaultConverter, { path: '/' })\n"],"mappings":";AAAA,MAAM,SAAS,SAAU,QAAa,mBAAwB,YAAiB;CAC7E,KAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KAAK;EACzC,MAAM,SAAS,UAAU;EACzB,KAAK,IAAI,OAAO,QACd,OAAO,OAAO,OAAO;CAEzB;CACA,OAAO;AACT;AAEA,MAAM,mBAAmB;CACvB,MAAM,SAAU,OAAY;EAC1B,IAAI,MAAM,OAAO,MACf,QAAQ,MAAM,MAAM,GAAG,EAAE;EAE3B,OAAO,MAAM,QAAQ,oBAAoB,kBAAkB;CAC7D;CACA,OAAO,SAAU,OAAY;EAC3B,OAAO,mBAAmB,KAAK,CAAC,CAAC,QAC/B,4CACA,kBACF;CACF;AACF;AAEA,SAAS,KAAK,WAAgB,mBAAwB;CACpD,SAAS,IAAI,MAAW,OAAY,YAAiB;EACnD,IAAI,OAAO,aAAa,aACtB;EAGF,aAAa,OAAO,CAAC,GAAG,mBAAmB,UAAU;EAErD,IAAI,OAAO,WAAW,YAAY,UAChC,W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