/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import {
  asyncNew,
  Diagnostic,
  ImplementationError,
  Logger,
  StorageManager,
  StorageService
} from "@matter/general";
import { NodeStore } from "../NodeStore.js";
import { ClientNodeStores } from "../client/ClientNodeStores.js";
import { ServerEndpointStores } from "./ServerEndpointStores.js";
const logger = Logger.get("ServerNodeStore");
class ServerNodeStore extends NodeStore {
  #env;
  #nodeId;
  #location;
  #endpointStores;
  #storageManager;
  #clientStores;
  constructor(environment, nodeId) {
    super({
      createContext: (name) => {
        if (!this.#storageManager) {
          throw new ImplementationError(
            `Cannot create storage context ${name} because store is not initialized`
          );
        }
        return this.#storageManager.createContext(name);
      }
    });
    this.#endpointStores = new ServerEndpointStores();
    this.#env = environment;
    this.#nodeId = nodeId;
    this.#location = this.#env.get(StorageService).location ?? "(unknown location)";
    this.construction.start();
  }
  static async create(environment, nodeId) {
    return await 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, Diagnostic.strong(this.#nodeId ?? "node"), "storage at", `${this.#location}/${this.#nodeId}`);
  }
  storeForEndpoint(endpoint) {
    return this.#endpointStores.storeForEndpoint(endpoint);
  }
  erase() {
    return this.#endpointStores.erase();
  }
  async load() {
    this.#storageManager = await this.#env.get(StorageService).open(this.#nodeId);
    this.#env.set(StorageManager, this.#storageManager);
    this.#clientStores = await asyncNew(ClientNodeStores, this.#storageManager.createContext("nodes"));
    const rootContext = this.storageFactory.createContext("root");
    await this.#endpointStores.load(rootContext);
    this.#logChange("Opened");
  }
}
export {
  ServerNodeStore
};
//# sourceMappingURL=ServerNodeStore.js.map
