{"version":3,"file":"storage.mjs","sources":["../../../../../../../web/src/utils/storage.ts"],"sourcesContent":["const PERSISTENT_STORAGE_KEY_PREFIX = 'stytch_sdk_state_';\n\n// Declare all keys to use with storage here. These storage are global so re-using the same key will cause collision\n// Do not remove values from here when the caller is removed since these will still be persisted on user's browsers.\nexport type StorageKey =\n  // SubscriptionService\n  | ''\n  | 'seen_domains'\n  // Bootstrap\n  | 'bootstrap'\n  // PKCE manager\n  | `PKCE_VERIFIER:${string}`\n  // Admin portal\n  | `${string}_itemsPerPage`\n  | `${string}_filter_${string}`\n  // B2B\n  | 'reset-email-value'\n  | `b2b_last_used_method`\n  // B2C\n  | `b2c_last_used_oauth`;\n\nexport const getPersistentStorageKey = (publicToken: string, key: StorageKey): string => {\n  return `${PERSISTENT_STORAGE_KEY_PREFIX}${publicToken}${key ? `::${key}` : ''}`;\n};\n\nexport interface IStorage {\n  getItem(publicKey: string, key: StorageKey): string | null;\n  removeItem(publicKey: string, key: StorageKey): void;\n  setItem(publicKey: string, key: StorageKey, value: string): void;\n}\n\nexport interface IKeyBoundStorage {\n  getItem(key: StorageKey): string | null;\n  setItem(key: StorageKey, value: string): void;\n  removeItem(key: StorageKey): void;\n}\n\nfunction makeSafeStorage(storage: Storage | undefined): IStorage {\n  if (storage == null) {\n    return {\n      getItem() {\n        return null;\n      },\n      removeItem() {\n        // Noop\n      },\n      setItem() {\n        // Noop\n      },\n    };\n  }\n\n  return {\n    getItem(publicKey: string, key: StorageKey): string | null {\n      const persistentStorageKey = getPersistentStorageKey(publicKey, key);\n      try {\n        return storage.getItem(persistentStorageKey);\n      } catch {\n        // Swallow error\n        return null;\n      }\n    },\n    setItem(publicKey: string, key: StorageKey, value: string): void {\n      const persistentStorageKey = getPersistentStorageKey(publicKey, key);\n      try {\n        if (value) storage.setItem(persistentStorageKey, value);\n      } catch {\n        // Swallow error\n      }\n    },\n    removeItem(publicKey: string, key: StorageKey): void {\n      const persistentStorageKey = getPersistentStorageKey(publicKey, key);\n      try {\n        storage.removeItem(persistentStorageKey);\n      } catch {\n        // Swallow error\n      }\n    },\n  };\n}\n\nexport const safeLocalStorage = makeSafeStorage(globalThis.localStorage);\nexport const safeSessionStorage = makeSafeStorage(globalThis.sessionStorage);\n\nexport function getKeyBoundStorage(storage: IStorage, publicToken: string): IKeyBoundStorage {\n  return {\n    getItem(key: StorageKey) {\n      return storage.getItem(publicToken, key);\n    },\n    setItem(key: StorageKey, value: string): void {\n      storage.setItem(publicToken, key, value);\n    },\n    removeItem(key: StorageKey) {\n      storage.removeItem(publicToken, key);\n    },\n  };\n}\n"],"names":["PERSISTENT_STORAGE_KEY_PREFIX","getPersistentStorageKey","publicToken","key","makeSafeStorage","storage","getItem","removeItem","setItem","publicKey","persistentStorageKey","value","safeLocalStorage","globalThis","localStorage","safeSessionStorage","sessionStorage","getKeyBoundStorage"],"mappings":"AAAA,MAAMA,6BAAAA,GAAgC,mBAAA;AAqB/B,MAAMC,uBAAAA,GAA0B,CAACC,WAAAA,EAAqBC,GAAAA,GAAAA;IAC3D,OAAO,CAAA,EAAGH,gCAAgCE,WAAAA,CAAAA,EAAcC,GAAAA,GAAM,CAAC,EAAE,EAAEA,GAAAA,CAAAA,CAAK,GAAG,EAAA,CAAA,CAAI;AACjF;AAcA,SAASC,gBAAgBC,OAA4B,EAAA;AACnD,IAAA,IAAIA,WAAW,IAAA,EAAM;QACnB,OAAO;AACLC,YAAAA,OAAAA,CAAAA,GAAAA;gBACE,OAAO,IAAA;AACT,YAAA,CAAA;AACAC,YAAAA,UAAAA,CAAAA,GAAAA;;AAEA,YAAA,CAAA;AACAC,YAAAA,OAAAA,CAAAA,GAAAA;;AAEA,YAAA;AACF,SAAA;AACF,IAAA;IAEA,OAAO;QACLF,OAAAA,CAAAA,CAAQG,SAAiB,EAAEN,GAAe,EAAA;YACxC,MAAMO,oBAAAA,GAAuBT,wBAAwBQ,SAAAA,EAAWN,GAAAA,CAAAA;YAChE,IAAI;gBACF,OAAOE,OAAAA,CAAQC,O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