{"version":3,"file":"index.mjs","sources":[""],"sourcesContent":["import { getDocument } from \"ssr-window\";\r\n\r\ntype TSameSite = \"lax\" | \"strict\" | \"none\" | string;\r\n\r\ntype TCookieKV = Record<string, unknown>;\r\n\r\n/**\r\n * Cookie options supported by setCookie.\r\n */\r\ntype TCookieOptions = {\r\n  /**\r\n   * Expiration setting:\r\n   * number: seconds from now\r\n   * Date: specific expiry date\r\n   * string: already formatted date string\r\n   */\r\n  expires?: number | Date | string;\r\n  path?: string;\r\n  domain?: string;\r\n  secure?: boolean;\r\n  \"max-age\"?: number | string;\r\n  samesite?: TSameSite;\r\n} & TCookieKV;\r\n\r\nexport type { TCookieOptions };\r\n\r\nexport type TSetCookieArgs = Parameters<typeof setCookie>;\r\n\r\nexport type TSetCookieReturn = ReturnType<typeof setCookie>;\r\n\r\n/**\r\n * Sets a cookie.\r\n *\r\n * If `options.expires` is a number, it's treated as seconds from now.\r\n * If `options.expires` is a Date, it's converted via toUTCString.\r\n * Other options are appended as cookie attributes.\r\n *\r\n * @param {string} name Cookie name\r\n * @param {string} value Cookie value\r\n * @param {TCookieOptions} [options={}] Cookie attributes\r\n * @returns {void}\r\n * @throws {TypeError} If inputs are invalid\r\n * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie\r\n * @example\r\n * // How to set Cookie for one day or other time?\r\n * setCookie(\"myCookie\", \"value\", { expires: 86400 }); // expires in 1 day (seconds)\r\n */\r\nexport const setCookie = (name: string, value: string, options: TCookieOptions = {}): void => {\r\n  if (typeof name !== \"string\" || name.length === 0) {\r\n    throw new TypeError(\"setCookie: name must be a non-empty string\");\r\n  }\r\n  if (typeof value !== \"string\") {\r\n    throw new TypeError(\"setCookie: value must be a string\");\r\n  }\r\n  if (options === null || typeof options !== \"object\" || Array.isArray(options)) {\r\n    throw new TypeError(\"setCookie: options must be an object\");\r\n  }\r\n\r\n  let expires = options.expires;\r\n  if (typeof expires === \"number\" && expires > 0) {\r\n    const d = new Date();\r\n    d.setTime(d.getTime() + expires * 1000);\r\n    expires = d;\r\n    options.expires = d;\r\n  }\r\n  if (expires && typeof (expires as Date).toUTCString === \"function\") {\r\n    options.expires = (expires as Date).toUTCString();\r\n  }\r\n\r\n  const encoded = encodeURIComponent(value);\r\n  let cookie = `${name}=${encoded}`;\r\n\r\n  for (const key in options) {\r\n    if (!Object.prototype.hasOwnProperty.call(options, key)) {\r\n      continue;\r\n    }\r\n    cookie += `; ${key}`;\r\n    const optVal = (options as Record<string, unknown>)[key];\r\n    if (optVal !== true && typeof optVal !== \"undefined\") {\r\n      cookie += `=${String(optVal)}`;\r\n    }\r\n  }\r\n\r\n  getDocument().cookie = cookie;\r\n};\r\n"],"names":["setCookie","name","value","options","length","TypeError","Array","isArray","expires","d","Date","setTime","getTime","toUTCString","encoded","encodeURIComponent","cookie","key","Object","prototype","hasOwnProperty","call","optVal","String","getDocument"],"mappings":";;;;;;;;;;;;;;;;;;;;GA+CO,MAAMA,UAAYA,CAACC,KAAcC,MAAeC,QAA0B,CAAA,KAC/E,UAAWF,OAAS,UAAYA,KAAKG,SAAW,EAC9C,MAAM,IAAIC,UAAU,8CAEtB,UAAWH,QAAU,SACnB,MAAM,IAAIG,UAAU,qCAEtB,GAAIF,UAAY,aAAeA,UAAY,UAAYG,MAAMC,QAAQJ,SACnE,MAAM,IAAIE,UAAU,wCAGtB,IAAIG,QAAUL,QAAQK,QACtB,UAAWA,UAAY,UAAYA,QAAU,EAAG,CAC9C,MAAMC,EAAI,IAAIC,KACdD,EAAEE,QAAQF,EAAEG,UAAYJ,QAAU,MAClCA,QAAUC,EACVN,QAAQK,QAAUC,CACpB,CACA,GAAID,gBAAmBA,QAAiBK,cAAgB,WACtDV,QAAQK,QAAWA,QAAiBK,cAGtC,MAAMC,QAAUC,mBAAmBb,OACnC,IAAIc,OAAS,GAAGf,QAAQa,UAExB,IAAK,MAAMG,OAAOd,QAAS,CACzB,IAAKe,OAAOC,UAAUC,eAAeC,KAAKlB,QAASc,KACjD,SAEFD,QAAU,KAAKC,MACf,MAAMK,OAAUnB,QAAoCc,KACpD,GAAIK,SAAW,aAAeA,SAAW,YACvCN,QAAU,IAAIO,OAAOD,SAEzB,CAEAE,cAAcR,OAASA"}