"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);
var MemoryStorageDriver_exports = {};
__export(MemoryStorageDriver_exports, {
  MemoryStorageDriver: () => MemoryStorageDriver
});
module.exports = __toCommonJS(MemoryStorageDriver_exports);
var import_DeepCopy = require("#util/DeepCopy.js");
var import_StorageDriver = require("./StorageDriver.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class MemoryStorageDriver extends import_StorageDriver.StorageDriver {
  constructor(store = {}) {
    super();
    this.store = store;
  }
  store;
  static id = "memory";
  isInitialized = false;
  get initialized() {
    return this.isInitialized;
  }
  static create(_namespace, _descriptor) {
    const storage = new MemoryStorageDriver();
    storage.initialize();
    return storage;
  }
  createContextKey(contexts) {
    for (const ctx of contexts) {
      if (!ctx.length || ctx.includes(".")) {
        throw new import_StorageDriver.StorageError("Context must not contain empty segments or leading or trailing dots.");
      }
    }
    return contexts.join(".");
  }
  initialize() {
    if (this.initialized) throw new import_StorageDriver.StorageError("Storage already initialized!");
    this.isInitialized = true;
  }
  clone() {
    const clone = new MemoryStorageDriver((0, import_DeepCopy.deepCopy)(this.store));
    clone.initialize();
    return clone;
  }
  async close() {
    this.isInitialized = false;
  }
  get data() {
    return this.store;
  }
  get(contexts, key) {
    this.#assertInitialized();
    if (!key.length) throw new import_StorageDriver.StorageError("Key must not be empty.");
    return this.store[this.createContextKey(contexts)]?.[key];
  }
  #setKey(contexts, key, value) {
    if (!key.length) throw new import_StorageDriver.StorageError("Key must not be empty.");
    const contextKey = this.createContextKey(contexts);
    if (this.store[contextKey] === void 0) {
      this.store[contextKey] = {};
    }
    this.store[contextKey][key] = value;
  }
  set(contexts, keyOrValues, value) {
    this.#assertInitialized();
    if (typeof keyOrValues === "string") {
      this.#setKey(contexts, keyOrValues, value);
    } else {
      Object.entries(keyOrValues).forEach(([key, value2]) => {
        this.#setKey(contexts, key, value2);
      });
    }
  }
  delete(contexts, key) {
    this.#assertInitialized();
    if (!key.length) throw new import_StorageDriver.StorageError("Key must not be empty.");
    delete this.store[this.createContextKey(contexts)]?.[key];
  }
  keys(contexts) {
    this.#assertInitialized();
    return Object.keys(this.store[this.createContextKey(contexts)] ?? {});
  }
  values(contexts) {
    const values = {};
    for (const key of this.keys(contexts)) {
      values[key] = this.get(contexts, key);
    }
    return values;
  }
  contexts(contexts) {
    this.#assertInitialized();
    const contextKey = contexts.length ? this.createContextKey(contexts) : "";
    const startContextKey = contextKey.length ? `${contextKey}.` : "";
    const foundContexts = new Array();
    Object.keys(this.store).forEach((key) => {
      if (key.startsWith(startContextKey)) {
        const subKeys = key.substring(startContextKey.length).split(".");
        if (subKeys.length < 1) return;
        const context = subKeys[0];
        if (context.length && !foundContexts.includes(context)) {
          foundContexts.push(context);
        }
      }
    });
    return foundContexts;
  }
  clearAll(contexts) {
    this.#assertInitialized();
    const contextKey = this.createContextKey(contexts);
    if (contextKey.length) {
      delete this.store[contextKey];
    }
    const startContextKey = contextKey.length ? `${contextKey}.` : "";
    Object.keys(this.store).forEach((key) => {
      if (key.startsWith(startContextKey)) {
        delete this.store[key];
      }
    });
  }
  #assertInitialized() {
    if (this.initialized) {
      return;
    }
    throw new import_StorageDriver.StorageError("Storage is not initialized");
  }
}
//# sourceMappingURL=MemoryStorageDriver.js.map
