import { useStorage } from './chunk-VVWQTO4Y.mjs';
import { __require } from './chunk-BJTO5JO5.mjs';

// src/storage/adapters/react-native-adapter.ts
var AsyncStorage = null;
try {
  AsyncStorage = __require("@react-native-async-storage/async-storage").default;
} catch (e) {
}
var ReactNativeStorageAdapter = class {
  constructor() {
    this.listeners = /* @__PURE__ */ new Map();
  }
  async getItem(key) {
    if (!AsyncStorage) return null;
    try {
      return await AsyncStorage.getItem(key);
    } catch (error) {
      console.error('[ReactNativeStorage] Error getting item "' + key + '":', error);
      return null;
    }
  }
  async setItem(key, value) {
    if (!AsyncStorage) {
      throw new Error("AsyncStorage is not available");
    }
    try {
      await AsyncStorage.setItem(key, value);
      this.notifyListeners(key, value);
    } catch (error) {
      console.error('[ReactNativeStorage] Error setting item "' + key + '":', error);
      throw error;
    }
  }
  async removeItem(key) {
    if (!AsyncStorage) {
      throw new Error("AsyncStorage is not available");
    }
    try {
      await AsyncStorage.removeItem(key);
      this.notifyListeners(key, null);
    } catch (error) {
      console.error('[ReactNativeStorage] Error removing item "' + key + '":', error);
      throw error;
    }
  }
  async clear() {
    if (!AsyncStorage) {
      throw new Error("AsyncStorage is not available");
    }
    try {
      await AsyncStorage.clear();
    } catch (error) {
      console.error("[ReactNativeStorage] Error clearing storage:", error);
      throw error;
    }
  }
  addChangeListener(callback) {
    if (!this.listeners.has("*")) {
      this.listeners.set("*", /* @__PURE__ */ new Set());
    }
    this.listeners.get("*").add(callback);
    return () => {
      this.listeners.get("*")?.delete(callback);
    };
  }
  notifyListeners(key, value) {
    const globalListeners = this.listeners.get("*");
    if (globalListeners) {
      globalListeners.forEach((callback) => callback(key, value));
    }
  }
};

// src/storage/hooks/useAsyncStorage.ts
var asyncStorage = new ReactNativeStorageAdapter();
function useAsyncStorage(key, defaultValue) {
  return useStorage(asyncStorage, key, defaultValue);
}

// src/storage/adapters/miniapp-adapter.ts
var Taro = null;
var taroLoadPromise = null;
var isMiniAppEnvironment = null;
function checkIsMiniApp() {
  if (isMiniAppEnvironment !== null) return isMiniAppEnvironment;
  if (typeof window === "undefined") {
    isMiniAppEnvironment = false;
    return false;
  }
  const globalObj = window;
  isMiniAppEnvironment = !!(globalObj.wx || // 微信小程序
  globalObj.my || // 支付宝小程序
  globalObj.swan || // 百度小程序
  globalObj.tt || // 抖音小程序
  globalObj.qq || // QQ 小程序
  globalObj.Taro);
  return isMiniAppEnvironment;
}
async function loadTaro() {
  if (!checkIsMiniApp()) {
    return;
  }
  if (Taro) return;
  if (taroLoadPromise) return taroLoadPromise;
  taroLoadPromise = (async () => {
    try {
      const importTaro = new Function('return import("@tarojs/taro")');
      const taroModule = await importTaro();
      Taro = taroModule.default || taroModule;
    } catch (e) {
      console.warn("[MiniAppStorage] Taro is not available. This is expected in non-MiniApp environments.");
    }
  })();
  return taroLoadPromise;
}
var MiniAppStorageAdapter = class {
  constructor() {
    this.listeners = /* @__PURE__ */ new Map();
  }
  async getItem(key) {
    if (!checkIsMiniApp()) {
      return null;
    }
    await loadTaro();
    if (!Taro) return null;
    try {
      const result = await Taro.getStorage({ key });
      return result.data;
    } catch (error) {
      if (error.errMsg?.includes("data not found")) {
        return null;
      }
      console.error('[MiniAppStorage] Error getting item "' + key + '":', error);
      return null;
    }
  }
  async setItem(key, value) {
    if (!checkIsMiniApp()) {
      throw new Error("[MiniAppStorage] This adapter can only be used in MiniApp environment");
    }
    await loadTaro();
    if (!Taro) {
      throw new Error("[MiniAppStorage] Taro is not available");
    }
    try {
      await Taro.setStorage({ key, data: value });
      this.notifyListeners(key, value);
    } catch (error) {
      console.error('[MiniAppStorage] Error setting item "' + key + '":', error);
      throw error;
    }
  }
  async removeItem(key) {
    if (!checkIsMiniApp()) {
      throw new Error("[MiniAppStorage] This adapter can only be used in MiniApp environment");
    }
    await loadTaro();
    if (!Taro) {
      throw new Error("[MiniAppStorage] Taro is not available");
    }
    try {
      await Taro.removeStorage({ key });
      this.notifyListeners(key, null);
    } catch (error) {
      console.error('[MiniAppStorage] Error removing item "' + key + '":', error);
      throw error;
    }
  }
  async clear() {
    if (!checkIsMiniApp()) {
      throw new Error("[MiniAppStorage] This adapter can only be used in MiniApp environment");
    }
    await loadTaro();
    if (!Taro) {
      throw new Error("[MiniAppStorage] Taro is not available");
    }
    try {
      await Taro.clearStorage();
    } catch (error) {
      console.error("[MiniAppStorage] Error clearing storage:", error);
      throw error;
    }
  }
  addChangeListener(callback) {
    if (!this.listeners.has("*")) {
      this.listeners.set("*", /* @__PURE__ */ new Set());
    }
    this.listeners.get("*").add(callback);
    return () => {
      this.listeners.get("*")?.delete(callback);
    };
  }
  notifyListeners(key, value) {
    const globalListeners = this.listeners.get("*");
    if (globalListeners) {
      globalListeners.forEach((callback) => callback(key, value));
    }
  }
};

// src/storage/hooks/useTaroStorage.ts
var taroStorage = new MiniAppStorageAdapter();
function useTaroStorage(key, defaultValue) {
  return useStorage(taroStorage, key, defaultValue);
}

// src/storage/adapters/electron-adapter.ts
var hasLocalStorage = typeof window !== "undefined" && typeof window.localStorage !== "undefined";
var ElectronStorageAdapter = class {
  constructor() {
    this.listeners = /* @__PURE__ */ new Map();
  }
  async getItem(key) {
    if (!hasLocalStorage) {
      return null;
    }
    try {
      return localStorage.getItem(key);
    } catch (error) {
      console.error('[ElectronStorage] Error getting item "' + key + '":', error);
      return null;
    }
  }
  async setItem(key, value) {
    if (!hasLocalStorage) {
      return;
    }
    try {
      localStorage.setItem(key, value);
      this.notifyListeners(key, value);
      if (typeof window !== "undefined") {
        window.dispatchEvent(
          new CustomEvent("electron-storage-change", {
            detail: { key, value }
          })
        );
      }
    } catch (error) {
      console.error('[ElectronStorage] Error setting item "' + key + '":', error);
      throw error;
    }
  }
  async removeItem(key) {
    if (!hasLocalStorage) {
      return;
    }
    try {
      localStorage.removeItem(key);
      this.notifyListeners(key, null);
      if (typeof window !== "undefined") {
        window.dispatchEvent(
          new CustomEvent("electron-storage-change", {
            detail: { key, value: null }
          })
        );
      }
    } catch (error) {
      console.error('[ElectronStorage] Error removing item "' + key + '":', error);
      throw error;
    }
  }
  async clear() {
    if (!hasLocalStorage) {
      return;
    }
    try {
      localStorage.clear();
      this.listeners.forEach((listeners, key) => {
        listeners.forEach((listener) => {
          listener(key, null);
        });
      });
      if (typeof window !== "undefined") {
        window.dispatchEvent(
          new CustomEvent("electron-storage-change", {
            detail: { key: "*", value: null }
          })
        );
      }
    } catch (error) {
      console.error("[ElectronStorage] Error clearing storage:", error);
      throw error;
    }
  }
  /**
   * 添加存储变化监听器
   * 支持跨 Electron 窗口同步
   */
  addChangeListener(listener) {
    const allListeners = this.listeners.get("*") || /* @__PURE__ */ new Set();
    allListeners.add(listener);
    this.listeners.set("*", allListeners);
    const storageHandler = (e) => {
      if (e.key) {
        listener(e.key, e.newValue);
      }
    };
    const customHandler = (e) => {
      const customEvent = e;
      const { key, value } = customEvent.detail;
      if (key === "*") {
        this.listeners.forEach((_, k) => listener(k, null));
      } else {
        listener(key, value);
      }
    };
    if (typeof window !== "undefined") {
      window.addEventListener("storage", storageHandler);
      window.addEventListener("electron-storage-change", customHandler);
    }
    return () => {
      allListeners.delete(listener);
      if (allListeners.size === 0) {
        this.listeners.delete("*");
      }
      if (typeof window !== "undefined") {
        window.removeEventListener("storage", storageHandler);
        window.removeEventListener("electron-storage-change", customHandler);
      }
    };
  }
  removeChangeListener(listener) {
    const allListeners = this.listeners.get("*");
    if (allListeners) {
      allListeners.delete(listener);
      if (allListeners.size === 0) {
        this.listeners.delete("*");
      }
    }
  }
  /**
   * 通知监听器
   */
  notifyListeners(key, value) {
    const keyListeners = this.listeners.get(key);
    if (keyListeners) {
      keyListeners.forEach((listener) => {
        listener(key, value);
      });
    }
    const allListeners = this.listeners.get("*");
    if (allListeners) {
      allListeners.forEach((listener) => {
        listener(key, value);
      });
    }
  }
};

// src/storage/hooks/useElectronStorage.ts
var electronStorage = new ElectronStorageAdapter();
function useElectronStorage(key, defaultValue) {
  return useStorage(electronStorage, key, defaultValue);
}

export { ElectronStorageAdapter, MiniAppStorageAdapter, ReactNativeStorageAdapter, useAsyncStorage, useElectronStorage, useTaroStorage };
//# sourceMappingURL=chunk-UKT3PLON.mjs.map
//# sourceMappingURL=chunk-UKT3PLON.mjs.map