'use client'

// src/storage/lib/client.ts
var generateUUID = () => {
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
    const r = Math.random() * 16 | 0, v = c == "x" ? r : r & 3 | 8;
    return v.toString(16);
  });
};
var StorageClient = class {
  constructor(url, opts) {
    opts = opts || {};
    this.id = generateUUID();
    this.frameId = opts.frameId || "CrossStorageClient-" + this.id;
    this.origin = new URL(url).origin;
    this.requests = {};
    this.connected = false;
    this.closed = false;
    this.count = 0;
    this.timeout = opts.timeout || 5e3;
    window.addEventListener("message", this.listener.bind(this), false);
    const frame = this.createFrame(url);
    this.hub = frame.contentWindow;
  }
  onConnect() {
    if (this.connected) {
      return Promise.resolve();
    } else if (this.closed) {
      return Promise.reject(new Error("StorageClient has closed."));
    }
    if (!this.requests.connect) {
      this.requests.connect = [];
    }
    return new Promise((resolve, reject) => {
      const timeout = setTimeout(function() {
        reject(new Error("StorageClient could not connect."));
      }, this.timeout);
      this.requests.connect.push((err) => {
        clearTimeout(timeout);
        if (err)
          return reject(err);
        resolve(void 0);
      });
    });
  }
  set(key, value) {
    return this.request("set", {
      key,
      value
    });
  }
  get(...keys) {
    return this.request("get", { keys });
  }
  del(...keys) {
    return this.request("del", { keys });
  }
  clear() {
    return this.request("clear");
  }
  getKeys() {
    this.request("getKeys");
  }
  close() {
    const frame = document.getElementById(this.frameId);
    if (frame && frame.parentNode) {
      frame.parentNode.removeChild(frame);
    }
    window.removeEventListener("message", this.listener, false);
    this.connected = false;
    this.closed = true;
  }
  listener(message) {
    let i, error, response;
    if (this.closed || !message.data || typeof message.data !== "string") {
      return;
    }
    const origin = message.origin;
    if (origin !== this.origin)
      return;
    if (message.data === "cross-storage:unavailable") {
      if (!this.closed)
        this.close();
      if (!this.requests.connect)
        return;
      error = new Error(
        "Closing StorageClient. Could not access localStorage in StorageHub."
      );
      for (i = 0; i < this.requests.connect.length; i++) {
        this.requests.connect[i](error);
      }
      return;
    }
    if (message.data.indexOf("cross-storage:") !== -1 && !this.connected) {
      this.connected = true;
      if (!this.requests.connect)
        return;
      for (i = 0; i < this.requests.connect.length; i++) {
        this.requests.connect[i](error);
      }
      delete this.requests.connect;
    }
    if (message.data === "cross-storage:ready")
      return;
    try {
      response = JSON.parse(message.data);
    } catch (e) {
      return;
    }
    if (!response.id)
      return;
    if (this.requests[response.id]) {
      this.requests[response.id](response.error, response.result);
    }
  }
  createFrame(url) {
    const frame = window.document.createElement("iframe");
    frame.id = this.frameId;
    frame.style.display = "none";
    frame.style.position = "absolute";
    frame.style.top = "-999px";
    frame.style.left = "-999px";
    window.document.body.appendChild(frame);
    frame.src = url;
    return frame;
  }
  // eslint-disable-next-line @typescript-eslint/ban-types
  request(method, params) {
    if (this.closed) {
      return Promise.reject(new Error("StorageClient has closed."));
    }
    this.count++;
    const req = {
      id: this.id + ":" + this.count,
      method: "cross-storage:" + method,
      params
    };
    return new Promise((resolve, reject) => {
      const timeout = window.setTimeout(() => {
        if (!this.requests[req.id])
          return;
        delete this.requests[req.id];
        reject(
          new Error("Timeout: StorageClient could not perform " + req.method)
        );
      }, this.timeout);
      this.requests[req.id] = (err, result) => {
        clearTimeout(timeout);
        delete this.requests[req.id];
        if (err)
          return reject(new Error(err));
        resolve(result);
      };
      if (this.hub) {
        this.hub.postMessage(JSON.stringify(req), this.origin);
      } else {
        reject(new Error("Not found StorageHub."));
      }
    });
  }
};

export {
  StorageClient
};
//# sourceMappingURL=chunk-UOPEIK22.mjs.map