"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 ControllerStore_exports = {};
__export(ControllerStore_exports, {
  ControllerStore: () => ControllerStore
});
module.exports = __toCommonJS(ControllerStore_exports);
var import_general = require("#general");
/**
 * @license
 * Copyright 2022-2024 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const logger = import_general.Logger.get("ControllerStore");
class ControllerStore {
  #location;
  #nodeId;
  #storageManager;
  #sessionStorage;
  #credentialsStorage;
  // Root certificate and Fabric
  #nodesStorage;
  // Holds list of nodes in root level and then sub levels with data per client node?
  #construction;
  get construction() {
    return this.#construction;
  }
  /**
   * Create a new store.
   *
   * TODO - implement conversion from 0.7 format so people can change API seamlessly
   */
  constructor(environment, nodeId) {
    if (nodeId === void 0) {
      throw new import_general.ImplementationError("ServerStore must be created with a nodeId");
    }
    const storage = environment.get(import_general.StorageService);
    this.#location = storage.location ?? "(unknown location)";
    this.#nodeId = nodeId;
    const initializeStorage = async () => {
      this.#storageManager = await storage.open(nodeId);
      this.#logChange("Opened");
    };
    this.#construction = (0, import_general.Construction)(this, initializeStorage);
  }
  static async create(environment, nodeId) {
    return await (0, import_general.asyncNew)(this, environment, nodeId);
  }
  async erase() {
    await this.#sessionStorage?.clearAll();
    await this.#credentialsStorage?.clearAll();
    await this.#nodesStorage?.clearAll();
  }
  async close() {
    await this.#construction.close(async () => {
      await this.#storageManager?.close();
      this.#logChange("Closed");
    });
  }
  get sessionStorage() {
    if (!this.#sessionStorage) {
      this.#sessionStorage = this.storage.createContext("sessions");
    }
    return this.#sessionStorage;
  }
  get credentialsStorage() {
    if (!this.#credentialsStorage) {
      this.#credentialsStorage = this.storage.createContext("credentials");
    }
    return this.#credentialsStorage;
  }
  get nodesStorage() {
    if (this.#nodesStorage === void 0) {
      this.#nodesStorage = this.storage.createContext("nodes");
    }
    return this.#nodesStorage;
  }
  get storage() {
    if (this.#storageManager === void 0) {
      throw new import_general.ImplementationError("Node storage accessed prior to initialization");
    }
    return this.#storageManager;
  }
  #logChange(what) {
    logger.info(what, import_general.Diagnostic.strong(this.#nodeId ?? "node"), "storage at", `${this.#location}/${this.#nodeId}`);
  }
}
//# sourceMappingURL=ControllerStore.js.map
