"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 NodeStore_exports = {};
__export(NodeStore_exports, {
  NodeStore: () => NodeStore
});
module.exports = __toCommonJS(NodeStore_exports);
var import_general = require("#general");
var import_protocol = require("#protocol");
var import_EndpointStoreService = require("./EndpointStoreService.js");
/**
 * @license
 * Copyright 2022-2024 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const logger = import_general.Logger.get("ServerStore");
class NodeStore {
  #location;
  #nodeId;
  #storageManager;
  #eventHandler;
  #sessionStorage;
  #fabricStorage;
  #eventStorage;
  #rootStore;
  #construction;
  get construction() {
    return this.#construction;
  }
  /**
   * Create a new store.
   */
  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.#rootStore = await (0, import_general.asyncNew)(import_EndpointStoreService.EndpointStoreFactory, {
        storage: this.#storageManager.createContext("root")
      });
      this.#eventStorage = this.#storage.createContext("events");
      this.#eventHandler = await import_protocol.EventHandler.create(this.eventStorage);
      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.#fabricStorage?.clearAll();
    await this.#eventStorage?.clearAll();
    await this.#rootStore?.erase();
  }
  async close() {
    await this.#construction.close(async () => {
      await this.#storageManager?.close();
      this.#logChange("Closed");
    });
  }
  get eventStorage() {
    if (!this.#eventStorage) {
      throw new import_general.ImplementationError("Event storage accessed prior to initialization");
    }
    return this.#eventStorage;
  }
  get eventHandler() {
    if (!this.#eventHandler) {
      throw new import_general.ImplementationError("Event handler accessed prior to initialization");
    }
    return this.#eventHandler;
  }
  get sessionStorage() {
    if (!this.#sessionStorage) {
      this.#sessionStorage = this.#storage.createContext("sessions");
    }
    return this.#sessionStorage;
  }
  get fabricStorage() {
    if (!this.#fabricStorage) {
      this.#fabricStorage = this.#storage.createContext("fabrics");
    }
    return this.#fabricStorage;
  }
  get endpointStores() {
    if (this.#rootStore === void 0) {
      throw new import_general.ImplementationError("Endpoint storage accessed prior to initialization");
    }
    return this.#rootStore;
  }
  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=NodeStore.js.map
