{"version":3,"file":"getStorageAttributes.cjs","names":[],"sources":["../../../src/utils/getStorageAttributes.ts"],"sourcesContent":["import type {\n  CookiesAttributes,\n  ProcessedStorageAttributes,\n  RoutingStorageInput,\n  StorageAttributes,\n} from '@intlayer/types/config';\nimport {\n  COOKIE_NAME,\n  HEADER_NAME,\n  LOCALE_STORAGE_NAME,\n} from '../defaultValues/routing';\n\n// ============================================================================\n// Types\n// ============================================================================\n\ntype CookieEntry = {\n  name: string;\n  attributes: Omit<CookiesAttributes, 'type' | 'name'>;\n};\n\ntype WebStorageEntry = {\n  name: string;\n};\n\ntype HeaderEntry = {\n  name: string;\n};\n\ntype StorageEntry =\n  | 'cookie'\n  | 'localStorage'\n  | 'sessionStorage'\n  | 'header'\n  | CookiesAttributes\n  | StorageAttributes;\n\n// ============================================================================\n// Helper Functions\n// ============================================================================\n\nconst createCookieEntry = (\n  options?: Partial<CookiesAttributes>\n): CookieEntry => {\n  const { name, path, expires, domain, secure, sameSite, httpOnly } =\n    options ?? {};\n  return {\n    name: name ?? COOKIE_NAME,\n    attributes: { path, expires, domain, secure, sameSite, httpOnly },\n  };\n};\n\nconst createWebStorageEntry = (\n  options?: Partial<StorageAttributes>\n): WebStorageEntry => ({\n  name: options?.name ?? LOCALE_STORAGE_NAME,\n});\n\nconst createHeaderEntry = (\n  options?: Partial<StorageAttributes>\n): HeaderEntry => ({\n  name: options?.name ?? HEADER_NAME,\n});\n\nconst isCookieEntry = (entry: unknown): boolean => {\n  if (typeof entry !== 'object' || entry === null) return false;\n  const e = entry as Record<string, unknown>;\n  return (\n    e['type'] === 'cookie' ||\n    'sameSite' in e ||\n    'httpOnly' in e ||\n    'secure' in e\n  );\n};\n\nconst isStorageType = (\n  value: string\n): value is 'cookie' | 'localStorage' | 'sessionStorage' | 'header' =>\n  value === 'cookie' ||\n  value === 'localStorage' ||\n  value === 'sessionStorage' ||\n  value === 'header';\n\n// ============================================================================\n// Main Function\n// ============================================================================\n\nconst processStorageEntry = (\n  entry: StorageEntry\n): Partial<ProcessedStorageAttributes> => {\n  if (typeof entry === 'string') {\n    if (!isStorageType(entry)) {\n      return { cookies: [], localStorage: [], sessionStorage: [], headers: [] };\n    }\n    if (entry === 'cookie') return { cookies: [createCookieEntry()] };\n    if (entry === 'localStorage')\n      return { localStorage: [createWebStorageEntry()] };\n    if (entry === 'sessionStorage')\n      return { sessionStorage: [createWebStorageEntry()] };\n    if (entry === 'header') return { headers: [createHeaderEntry()] };\n  }\n\n  if (typeof entry === 'object' && entry !== null) {\n    const typedEntry = entry as CookiesAttributes | StorageAttributes;\n\n    if (isCookieEntry(typedEntry)) {\n      return { cookies: [createCookieEntry(typedEntry as CookiesAttributes)] };\n    }\n    if ('type' in typedEntry && typedEntry.type === 'localStorage') {\n      return {\n        localStorage: [createWebStorageEntry(typedEntry as StorageAttributes)],\n      };\n    }\n    if ('type' in typedEntry && typedEntry.type === 'sessionStorage') {\n      return {\n        sessionStorage: [\n          createWebStorageEntry(typedEntry as StorageAttributes),\n        ],\n      };\n    }\n    if ('type' in typedEntry && typedEntry.type === 'header') {\n      return { headers: [createHeaderEntry(typedEntry as StorageAttributes)] };\n    }\n    // Default to localStorage for ambiguous objects\n    return {\n      localStorage: [\n        createWebStorageEntry(typedEntry as Omit<StorageAttributes, 'type'>),\n      ],\n    };\n  }\n\n  return { cookies: [], localStorage: [], sessionStorage: [], headers: [] };\n};\n\nconst mergeStorageAttributes = (\n  accumulated: ProcessedStorageAttributes,\n  partial: Partial<ProcessedStorageAttributes>\n): ProcessedStorageAttributes => ({\n  cookies: [...(accumulated.cookies ?? []), ...(partial.cookies ?? [])],\n  localStorage: [\n    ...(accumulated.localStorage ?? []),\n    ...(partial.localStorage ?? []),\n  ],\n  sessionStorage: [\n    ...(accumulated.sessionStorage ?? []),\n    ...(partial.sessionStorage ?? []),\n  ],\n  headers: [...(accumulated.headers ?? []), ...(partial.headers ?? [])],\n});\n\n/**\n * Extracts and normalizes storage configuration into separate arrays for each storage type.\n * Called at config-build time so the result is pre-computed and stored in the config.\n *\n * @param options - The storage configuration from IntlayerConfig\n * @returns An object containing arrays for cookies, localStorage, sessionStorage and headers\n */\nexport const getStorageAttributes = (\n  options: RoutingStorageInput\n): ProcessedStorageAttributes => {\n  const emptyResult: ProcessedStorageAttributes = {\n    cookies: [],\n    localStorage: [],\n    sessionStorage: [],\n    headers: [],\n  };\n\n  if (options === false || options === undefined) return {};\n\n  const result = Array.isArray(options)\n    ? options.reduce<ProcessedStorageAttributes>((acc, entry) => {\n        const partial = processStorageEntry(entry);\n        return mergeStorageAttributes(acc, partial);\n      }, emptyResult)\n    : mergeStorageAttributes(emptyResult, processStorageEntry(options));\n\n  // Remove empty arrays\n  const cleanedResult: ProcessedStorageAttributes = {};\n\n  if (result.cookies && result.cookies.length > 0) {\n    cleanedResult.cookies = result.cookies;\n  }\n\n  if (result.localStorage && result.localStorage.length > 0) {\n    cleanedResult.localStorage = result.localStorage;\n  }\n\n  if (result.sessionStorage && result.sessionStorage.length > 0) {\n    cleanedResult.sessionStorage = result.sessionStorage;\n  }\n\n  if (result.headers && result.headers.length > 0) {\n    cleanedResult.headers = result.headers;\n  }\n\n  return cleanedResult;\n};\n"],"mappings":";;;;AAyCA,MAAM,qBACJ,YACgB;CAChB,MAAM,EAAE,MAAM,MAAM,SAAS,QAAQ,QAAQ,UAAU,aACrD,WAAW,EAAE;AACf,QAAO;EACL,MAAM;EACN,YAAY;GAAE;GAAM;GAAS;GAAQ;GAAQ;GAAU;GAAU;EAClE;;AAGH,MAAM,yBACJ,aACqB,EACrB,MAAM,SAAS,2BAChB;AAED,MAAM,qBACJ,aACiB,EACjB,MAAM,SAAS,6BAChB;AAED,MAAM,iBAAiB,UAA4B;AACjD,KAAI,OAAO,UAAU,YAAY,UAAU,KAAM,QAAO;CACxD,MAAM,IAAI;AACV,QACE,EAAE,YAAY,YACd,cAAc,KACd,cAAc,KACd,YAAY;;AAIhB,MAAM,iBACJ,UAEA,UAAU,YACV,UAAU,kBACV,UAAU,oBAC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