// src/storage/adapters/web-adapter.ts
var isBrowser = typeof window !== "undefined" && typeof window.localStorage !== "undefined";
var WebStorageAdapter = class {
  async getItem(key) {
    if (!isBrowser) {
      return null;
    }
    try {
      return localStorage.getItem(key);
    } catch (error) {
      console.error('[WebStorage] Error getting item "' + key + '":', error);
      return null;
    }
  }
  async setItem(key, value) {
    if (!isBrowser) {
      return;
    }
    try {
      localStorage.setItem(key, value);
    } catch (error) {
      console.error('[WebStorage] Error setting item "' + key + '":', error);
      throw error;
    }
  }
  async removeItem(key) {
    if (!isBrowser) {
      return;
    }
    try {
      localStorage.removeItem(key);
    } catch (error) {
      console.error('[WebStorage] Error removing item "' + key + '":', error);
      throw error;
    }
  }
  async clear() {
    if (!isBrowser) {
      return;
    }
    try {
      localStorage.clear();
    } catch (error) {
      console.error("[WebStorage] Error clearing storage:", error);
      throw error;
    }
  }
  addChangeListener(callback) {
    if (!isBrowser) {
      return () => {
      };
    }
    const handleStorageEvent = (e) => {
      if (e.key) {
        callback(e.key, e.newValue);
      }
    };
    const handleCustomEvent = (e) => {
      const customEvent = e;
      callback(customEvent.detail.key, customEvent.detail.value);
    };
    window.addEventListener("storage", handleStorageEvent);
    window.addEventListener("local-storage-change", handleCustomEvent);
    return () => {
      window.removeEventListener("storage", handleStorageEvent);
      window.removeEventListener("local-storage-change", handleCustomEvent);
    };
  }
  /**
   * 触发自定义事件，通知同标签页的其他组件
   */
  dispatchChange(key, value) {
    if (!isBrowser) {
      return;
    }
    window.dispatchEvent(
      new CustomEvent("local-storage-change", {
        detail: { key, value }
      })
    );
  }
};

export { WebStorageAdapter };
//# sourceMappingURL=chunk-WEEXCPSE.mjs.map
//# sourceMappingURL=chunk-WEEXCPSE.mjs.map