"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 ServerNodeStore_exports = {};
__export(ServerNodeStore_exports, {
  ServerNodeStore: () => ServerNodeStore
});
module.exports = __toCommonJS(ServerNodeStore_exports);
var import_general = require("@matter/general");
var import_NodeStore = require("../NodeStore.js");
var import_ClientNodeStores = require("../client/ClientNodeStores.js");
var import_ServerEndpointStores = require("./ServerEndpointStores.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const logger = import_general.Logger.get("ServerNodeStore");
class ServerNodeStore extends import_NodeStore.NodeStore {
  #env;
  #nodeId;
  #location;
  #endpointStores;
  #storageManager;
  #clientStores;
  constructor(environment, nodeId) {
    super({
      createContext: (name) => {
        if (!this.#storageManager) {
          throw new import_general.ImplementationError(
            `Cannot create storage context ${name} because store is not initialized`
          );
        }
        return this.#storageManager.createContext(name);
      }
    });
    this.#endpointStores = new import_ServerEndpointStores.ServerEndpointStores();
    this.#env = environment;
    this.#nodeId = nodeId;
    this.#location = this.#env.get(import_general.StorageService).location ?? "(unknown location)";
    this.construction.start();
  }
  static async create(environment, nodeId) {
    return await (0, import_general.asyncNew)(this, environment, nodeId);
  }
  async close() {
    await this.construction.close(async () => {
      await this.#clientStores?.close();
      await this.#storageManager?.close();
      this.#logChange("Closed");
    });
  }
  /**
   * Stores associated with server endpoints supported by this node.
   */
  get endpointStores() {
    return this.construction.assert("endpoint stores", this.#endpointStores);
  }
  /**
   * Stores associated with remote nodes known by this node.
   */
  get clientStores() {
    return this.construction.assert("client stores", this.#clientStores);
  }
  /**
   * The underlying {@link StorageManager} that provides node data.
   */
  get storage() {
    return this.construction.assert("storage manager", this.#storageManager);
  }
  #logChange(what) {
    logger.info(
      what,
      import_general.Diagnostic.strong(this.#nodeId ?? "node"),
      "storage",
      import_general.Diagnostic.dict({
        location: `${this.#location}/${this.#nodeId}`,
        driver: this.#storageManager?.driverId ?? "unknown"
      })
    );
  }
  storeForEndpoint(endpoint) {
    return this.#endpointStores.storeForEndpoint(endpoint);
  }
  erase() {
    return this.#endpointStores.erase();
  }
  async load() {
    this.#storageManager = await this.#env.get(import_general.StorageService).open(this.#nodeId);
    this.#env.set(import_general.StorageManager, this.#storageManager);
    this.#clientStores = await (0, import_general.asyncNew)(import_ClientNodeStores.ClientNodeStores, this.#storageManager.createContext("nodes"));
    const rootContext = this.storageFactory.createContext("root");
    await this.#endpointStores.load(rootContext);
    this.#logChange("Opened");
  }
}
//# sourceMappingURL=ServerNodeStore.js.map
