"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 ClientNodeStore_exports = {};
__export(ClientNodeStore_exports, {
  ClientNodeStore: () => ClientNodeStore
});
module.exports = __toCommonJS(ClientNodeStore_exports);
var import_general = require("@matter/general");
var import_NodeStore = require("../NodeStore.js");
var import_ClientEndpointStore = require("./ClientEndpointStore.js");
var import_LocalWriter = require("./LocalWriter.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class ClientNodeStore extends import_NodeStore.NodeStore {
  #id;
  #storage;
  #stores = /* @__PURE__ */ new Map();
  #write;
  #localWriter;
  #buffer;
  #isPreexisting;
  #onErase;
  constructor(id, storage, isPreexisting, onErase) {
    super(storage);
    this.#id = id;
    this.#isPreexisting = isPreexisting;
    this.#onErase = onErase;
  }
  toString() {
    return `client-node-store#${this.#id}`;
  }
  get id() {
    return this.#id;
  }
  get isPreexisting() {
    return this.#isPreexisting;
  }
  get write() {
    if (this.#write === void 0) {
      throw new import_general.InternalError("Write attempt on ClientNodeStore without writer installed");
    }
    return this.#write;
  }
  set write(write) {
    this.#write = write;
  }
  get localWriter() {
    if (this.#localWriter === void 0) {
      this.#localWriter = new import_LocalWriter.LocalWriter(this);
    }
    return this.#localWriter;
  }
  get buffer() {
    return this.#buffer;
  }
  set buffer(buffer) {
    this.#buffer = buffer;
  }
  get endpointStores() {
    return this.#stores.values();
  }
  storeForEndpointNumber(endpointNumber) {
    const store = this.#stores.get(endpointNumber);
    if (store === void 0) {
      throw new import_general.InternalError(`No endpoint store for endpoint ${endpointNumber}`);
    }
    return store;
  }
  async erase() {
    for (const store of this.#stores.values()) {
      await store.erase();
    }
    this.#stores = /* @__PURE__ */ new Map();
    this.#onErase?.();
    await this.#storage?.clearAll();
    await this.construction.close();
  }
  storeForEndpoint(endpoint) {
    const { number } = endpoint;
    if (this.#storage === void 0) {
      throw new import_general.InternalError(`Endpoint storage ${this.toString()}.endpoints.${number} accessed before load`);
    }
    let store = this.#stores.get(number);
    if (store === void 0) {
      store = new import_ClientEndpointStore.ClientEndpointStore(this, number, this.#storage.createContext(number.toString()));
      this.#stores.set(number, store);
    }
    return store;
  }
  async load() {
    this.#storage = this.storageFactory.createContext("endpoints");
    for (const id of await this.#storage.contexts()) {
      const number = Number.parseInt(id);
      if (!Number.isFinite(number)) {
        continue;
      }
      const store = new import_ClientEndpointStore.ClientEndpointStore(this, number, this.#storage.createContext(id));
      await store.load();
      this.#stores.set(number, store);
    }
  }
}
//# sourceMappingURL=ClientNodeStore.js.map
