"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.isReactNative = exports.getStorageKeys = exports.createPlatformStorage = exports.STORAGE_KEY_PREFIX = void 0;
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
/**
 * Create an in-memory storage implementation used as a safe fallback.
 */
const MEMORY_STORAGE = () => {
  const store = new Map();
  return {
    async getItem(key) {
      return store.has(key) ? store.get(key) : null;
    },
    async setItem(key, value) {
      store.set(key, value);
    },
    async removeItem(key) {
      store.delete(key);
    },
    async clear() {
      store.clear();
    }
  };
};

/**
 * Create a web storage implementation backed by `localStorage`.
 * Falls back to in-memory storage when unavailable.
 */
const createWebStorage = () => {
  if (typeof window === 'undefined' || typeof window.localStorage === 'undefined') {
    return MEMORY_STORAGE();
  }
  return {
    async getItem(key) {
      try {
        return window.localStorage.getItem(key);
      } catch {
        return null;
      }
    },
    async setItem(key, value) {
      try {
        window.localStorage.setItem(key, value);
      } catch {
        // Ignore quota or access issues for now.
      }
    },
    async removeItem(key) {
      try {
        window.localStorage.removeItem(key);
      } catch {
        // Ignore failures.
      }
    },
    async clear() {
      try {
        window.localStorage.clear();
      } catch {
        // Ignore failures.
      }
    }
  };
};
let asyncStorageInstance = null;

/**
 * Lazily import React Native AsyncStorage implementation.
 */
const createNativeStorage = async () => {
  if (asyncStorageInstance) {
    return asyncStorageInstance;
  }
  try {
    const asyncStorageModule = await Promise.resolve().then(() => _interopRequireWildcard(require('@react-native-async-storage/async-storage')));
    asyncStorageInstance = asyncStorageModule.default;
    return asyncStorageInstance;
  } catch (error) {
    if (__DEV__) {
      console.error('Failed to import AsyncStorage:', error);
    }
    throw new Error('AsyncStorage is required in React Native environment');
  }
};

/**
 * Detect whether the current runtime is React Native.
 */
const isReactNative = () => typeof navigator !== 'undefined' && navigator.product === 'ReactNative';

/**
 * Create a platform-appropriate storage implementation.
 * Defaults to in-memory storage when no platform storage is available.
 */
exports.isReactNative = isReactNative;
const createPlatformStorage = async () => {
  if (isReactNative()) {
    return createNativeStorage();
  }
  return createWebStorage();
};
exports.createPlatformStorage = createPlatformStorage;
const STORAGE_KEY_PREFIX = exports.STORAGE_KEY_PREFIX = 'oxy_session';

/**
 * Produce strongly typed storage key names for the supplied prefix.
 *
 * @param prefix - Storage key prefix
 */
const getStorageKeys = (prefix = STORAGE_KEY_PREFIX) => ({
  activeSessionId: `${prefix}_active_session_id`,
  sessionIds: `${prefix}_session_ids`,
  language: `${prefix}_language`
});
exports.getStorageKeys = getStorageKeys;
//# sourceMappingURL=storageHelpers.js.map