var h = Object.defineProperty, f = (s, e, t) => e in s ? h(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t, a = (s, e, t) => f(s, typeof e != "symbol" ? e + "" : e, t);
class l {
  constructor() {
    a(this, "data", /* @__PURE__ */ new Map());
  }
  get length() {
    return this.data.size;
  }
  key(e) {
    return Array.from(this.data.keys())[e] || null;
  }
  getItem(e) {
    return this.data.get(e) || null;
  }
  setItem(e, t) {
    this.data.set(e, t);
  }
  removeItem(e) {
    this.data.delete(e);
  }
  clear() {
    this.data.clear();
  }
}
const m = (s) => {
  switch (s) {
    case "localStorage":
      return typeof window < "u" && window.localStorage ? window.localStorage : new l();
    case "sessionStorage":
      return typeof window < "u" && window.sessionStorage ? window.sessionStorage : new l();
    case "memoryStorage":
    default:
      return new l();
  }
};
class g {
  constructor(e = {}) {
    a(this, "storage"), a(this, "config"), a(this, "eventListeners", /* @__PURE__ */ new Map()), this.config = {
      type: "localStorage",
      prefix: "frank_auth_",
      encryption: !1,
      compression: !1,
      ...e
    }, this.storage = m(this.config.type), typeof window < "u" && this.config.type !== "memoryStorage" && window.addEventListener("storage", this.handleStorageChange.bind(this));
  }
  createKey(e) {
    return `${this.config.prefix}${e}`;
  }
  handleStorageChange(e) {
    const t = e;
    if (!t.key || !t.key.startsWith(this.config.prefix))
      return;
    const r = t.key.substring(this.config.prefix.length), n = this.deserialize(t.oldValue), i = this.deserialize(t.newValue), c = {
      key: r,
      oldValue: n,
      newValue: i,
      timestamp: Date.now()
    };
    this.emitEvent(r, c);
  }
  serialize(e, t = {}) {
    const r = {
      value: e,
      timestamp: Date.now(),
      version: "1.0"
    };
    (t.ttl || this.config.ttl) && (r.expires = Date.now() + (t.ttl || this.config.ttl));
    let n = JSON.stringify(r);
    return this.config.compression && (n = this.compress(n)), this.config.encryption && (n = this.encrypt(n)), n;
  }
  deserialize(e) {
    if (!e) return null;
    try {
      let t = e;
      this.config.encryption && (t = this.decrypt(t)), this.config.compression && (t = this.decompress(t));
      const r = JSON.parse(t);
      return r.expires && Date.now() > r.expires ? null : r.value;
    } catch {
      return null;
    }
  }
  compress(e) {
    return e;
  }
  decompress(e) {
    return e;
  }
  encrypt(e) {
    return e;
  }
  decrypt(e) {
    return e;
  }
  emitEvent(e, t) {
    const r = this.eventListeners.get(e);
    if (r)
      for (const i of r)
        i(t);
    const n = this.eventListeners.get("*");
    if (n)
      for (const i of n)
        i(t);
  }
  // Public API
  get(e) {
    try {
      const t = this.createKey(e), r = this.storage.getItem(t);
      return this.deserialize(r);
    } catch {
      return null;
    }
  }
  set(e, t, r = {}) {
    try {
      const n = this.createKey(e), i = this.get(e), c = this.serialize(t, r);
      this.storage.setItem(n, c);
      const u = {
        key: e,
        oldValue: i,
        newValue: t,
        timestamp: Date.now()
      };
      this.emitEvent(e, u);
    } catch (n) {
      console.error("Failed to set storage item:", n);
    }
  }
  remove(e) {
    try {
      const t = this.createKey(e), r = this.get(e);
      this.storage.removeItem(t);
      const n = {
        key: e,
        oldValue: r,
        newValue: null,
        timestamp: Date.now()
      };
      this.emitEvent(e, n);
    } catch (t) {
      console.error("Failed to remove storage item:", t);
    }
  }
  clear() {
    try {
      const e = this.keys();
      for (const t of e) {
        const r = this.createKey(t);
        this.storage.removeItem(r);
      }
      for (const t of e) {
        const r = {
          key: t,
          oldValue: null,
          newValue: null,
          timestamp: Date.now()
        };
        this.emitEvent(t, r);
      }
    } catch (e) {
      console.error("Failed to clear storage:", e);
    }
  }
  keys() {
    try {
      const e = [], t = this.config.prefix.length;
      for (let r = 0; r < this.storage.length; r++) {
        const n = this.storage.key(r);
        n && n.startsWith(this.config.prefix) && e.push(n.substring(t));
      }
      return e;
    } catch {
      return [];
    }
  }
  size() {
    return this.keys().length;
  }
  exists(e) {
    const t = this.createKey(e);
    return this.storage.getItem(t) !== null;
  }
  // Event handling
  on(e, t) {
    return this.eventListeners.has(e) || this.eventListeners.set(e, /* @__PURE__ */ new Set()), this.eventListeners.get(e).add(t), () => {
      this.eventListeners.get(e)?.delete(t);
    };
  }
  off(e, t) {
    this.eventListeners.get(e)?.delete(t);
  }
  // Utility methods
  getObject(e) {
    return this.get(e);
  }
  setObject(e, t, r) {
    this.set(e, t, r);
  }
  getString(e) {
    return this.get(e);
  }
  setString(e, t, r) {
    this.set(e, t, r);
  }
  getNumber(e) {
    return this.get(e);
  }
  setNumber(e, t, r) {
    this.set(e, t, r);
  }
  getBoolean(e) {
    return this.get(e);
  }
  setBoolean(e, t, r) {
    this.set(e, t, r);
  }
  // Batch operations
  setMultiple(e, t) {
    for (const [r, n] of Object.entries(e))
      this.set(r, n, t);
  }
  getMultiple(e) {
    const t = {};
    for (const r of e)
      t[r] = this.get(r);
    return t;
  }
  removeMultiple(e) {
    for (const t of e)
      this.remove(t);
  }
  // Cleanup expired items
  cleanup() {
    const e = this.keys();
    let t = 0;
    for (const r of e)
      this.get(r) === null && (this.remove(r), t++);
    return t;
  }
  // Export/import functionality
  export() {
    const e = {}, t = this.keys();
    for (const r of t) {
      const n = this.get(r);
      n !== null && (e[r] = n);
    }
    return e;
  }
  import(e, t) {
    t?.merge || this.clear();
    for (const [r, n] of Object.entries(e))
      this.set(r, n, t);
  }
}
const o = new g({
  type: "localStorage",
  prefix: "frank_auth_"
}), d = new g({
  type: "sessionStorage",
  prefix: "frank_auth_session_"
}), p = new g({
  type: "memoryStorage",
  prefix: "frank_auth_memory_"
}), y = (s, e) => e ? `${e}_${s}` : s, S = (s) => {
  const e = s.split("_");
  return e.length >= 2 && e[0].match(/^[a-zA-Z0-9]+$/) ? {
    organizationId: e[0],
    key: e.slice(1).join("_")
  } : { key: s };
}, v = (s) => {
  if (typeof window > "u") return !1;
  if (s === "memoryStorage") return !0;
  try {
    const e = s === "localStorage" ? window.localStorage : window.sessionStorage, t = "__frank_auth_test__";
    return e.setItem(t, "test"), e.removeItem(t), !0;
  } catch {
    return !1;
  }
}, w = (s) => {
  if (typeof window > "u" || s === "memoryStorage") return 0;
  try {
    const e = s === "localStorage" ? window.localStorage : window.sessionStorage;
    let t = 0;
    for (let r = 0; r < e.length; r++) {
      const n = e.key(r);
      if (n) {
        const i = e.getItem(n);
        t += n.length + (i ? i.length : 0);
      }
    }
    return t;
  } catch {
    return 0;
  }
}, _ = (s) => s.cleanup(), k = (s) => (e, t) => {
  const r = () => s.get(e) ?? t ?? null, n = (i) => {
    i === null ? s.remove(e) : s.set(e, i);
  };
  return [r(), n];
}, b = {
  // Token management
  getAccessToken() {
    return o.getString("access_token");
  },
  setAccessToken(s, e) {
    o.setString("access_token", s, { ttl: e });
  },
  removeAccessToken() {
    o.remove("access_token");
  },
  getRefreshToken() {
    return o.getString("refresh_token");
  },
  setRefreshToken(s) {
    o.setString("refresh_token", s);
  },
  removeRefreshToken() {
    o.remove("refresh_token");
  },
  // Session management
  getSessionId() {
    return o.getString("session_id");
  },
  setSessionId(s) {
    o.setString("session_id", s);
  },
  removeSessionId() {
    o.remove("session_id");
  },
  // User data
  getUserData() {
    return o.getObject("user_data");
  },
  setUserData(s) {
    o.setObject("user_data", s);
  },
  removeUserData() {
    o.remove("user_data");
  },
  // Organization context
  getOrganizationId() {
    return o.getString("organization_id");
  },
  setOrganizationId(s) {
    o.setString("organization_id", s);
  },
  removeOrganizationId() {
    o.remove("organization_id");
  },
  // Clear all auth data
  clearAll() {
    const s = [
      "access_token",
      "refresh_token",
      "session_id",
      "user_data",
      "organization_id"
    ];
    o.removeMultiple(s);
  },
  // Device management
  getDeviceFingerprint() {
    return o.getString("device_fingerprint");
  },
  setDeviceFingerprint(s) {
    o.setString("device_fingerprint", s);
  },
  // Remember me functionality
  getRememberMe() {
    return o.getBoolean("remember_me") ?? !1;
  },
  setRememberMe(s) {
    o.setBoolean("remember_me", s);
  }
};
export {
  b as AuthStorage,
  g as StorageManager,
  _ as clearExpiredItems,
  k as createStorageHook,
  y as createStorageKey,
  o as defaultStorage,
  w as getStorageSize,
  v as isStorageAvailable,
  p as memoryStorage,
  S as parseStorageKey,
  d as sessionStorage
};
//# sourceMappingURL=storage.js.map
