'use client'
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/storage/lib/hub.ts
var hub_exports = {};
__export(hub_exports, {
  init: () => init
});
module.exports = __toCommonJS(hub_exports);
var context = {};
var init = (permissions) => {
  context.permissions = permissions;
  window.addEventListener("message", listener, false);
  window.parent.postMessage("cross-storage:ready", "*");
};
var permitted = (origin, method) => {
  const available = ["get", "set", "del", "clear", "getKeys"];
  if (!available.includes(method)) {
    return false;
  }
  if (context.permissions === void 0) {
    return false;
  }
  for (let i = 0; i < context.permissions.length; i++) {
    const entry = context.permissions[i];
    const match = entry.origin.test(origin);
    if (match && entry.allow.includes(method)) {
      return true;
    }
  }
  return false;
};
var methods = {
  get: (params) => {
    const storage = window.localStorage;
    const result = [];
    for (let i = 0; i < params.keys.length; i++) {
      const value = storage.getItem(params.keys[i]) || null;
      result.push(value);
    }
    return result.length > 1 ? result : result[0];
  },
  set: (params) => {
    window.localStorage.setItem(params.key, params.value);
  },
  del: (params) => {
    for (var i = 0; i < params.keys.length; i++) {
      window.localStorage.removeItem(params.keys[i]);
    }
  },
  getKeys: () => {
    const keys = [];
    const length = window.localStorage.length;
    for (let i = 0; i < length; i++) {
      keys.push(window.localStorage.key(i));
    }
    return keys;
  },
  clear: () => {
    window.localStorage.clear();
  }
};
var listener = (message) => {
  const origin = message.origin === "null" ? "file://" : message.origin;
  if (message.data === "cross-storage:poll") {
    return window.parent.postMessage("cross-storage:ready", message.origin);
  }
  if (message.data === "cross-storage:ready")
    return;
  const request = JSON.parse(message.data);
  if (!request || typeof request.method !== "string") {
    return;
  }
  const method = request.method.split("cross-storage:")[1];
  let error = "";
  let result = void 0;
  if (!method) {
    return;
  } else if (!permitted(origin, method)) {
    error = "Invalid permissions for " + method;
  } else {
    try {
      result = methods[method](request.params);
    } catch (err) {
      error = err.message;
    }
  }
  const response = JSON.stringify({
    id: request.id,
    error,
    result
  });
  const targetOrigin = origin === "file://" ? "*" : origin;
  window.parent.postMessage(response, targetOrigin);
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  init
});
//# sourceMappingURL=hub.js.map