/**
 * @license
 * Copyright 2022-2024 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import {
  Construction,
  Diagnostic,
  ImplementationError,
  Logger,
  StorageService,
  asyncNew
} from "#general";
import { EventHandler } from "#protocol";
import { EndpointStoreFactory } from "./EndpointStoreService.js";
const logger = 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 ImplementationError("ServerStore must be created with a nodeId");
    }
    const storage = environment.get(StorageService);
    this.#location = storage.location ?? "(unknown location)";
    this.#nodeId = nodeId;
    const initializeStorage = async () => {
      this.#storageManager = await storage.open(nodeId);
      this.#rootStore = await asyncNew(EndpointStoreFactory, {
        storage: this.#storageManager.createContext("root")
      });
      this.#eventStorage = this.#storage.createContext("events");
      this.#eventHandler = await EventHandler.create(this.eventStorage);
      this.#logChange("Opened");
    };
    this.#construction = Construction(this, initializeStorage);
  }
  static async create(environment, nodeId) {
    return await 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 ImplementationError("Event storage accessed prior to initialization");
    }
    return this.#eventStorage;
  }
  get eventHandler() {
    if (!this.#eventHandler) {
      throw new 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 ImplementationError("Endpoint storage accessed prior to initialization");
    }
    return this.#rootStore;
  }
  get #storage() {
    if (this.#storageManager === void 0) {
      throw new ImplementationError("Node storage accessed prior to initialization");
    }
    return this.#storageManager;
  }
  #logChange(what) {
    logger.info(what, Diagnostic.strong(this.#nodeId ?? "node"), "storage at", `${this.#location}/${this.#nodeId}`);
  }
}
export {
  NodeStore
};
//# sourceMappingURL=NodeStore.js.map
