"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("#general");
var import_NodeStore = require("../NodeStore.js");
var import_ClientEndpointStore = require("./ClientEndpointStore.js");
/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class ClientNodeStore extends import_NodeStore.NodeStore {
  #id;
  #storage;
  #stores = /* @__PURE__ */ new Map();
  #write;
  constructor(id, storage) {
    super(storage);
    this.#id = id;
  }
  toString() {
    return `client-node-store#${this.#id}`;
  }
  get id() {
    return this.#id;
  }
  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 endpointStores() {
    return this.#stores.values();
  }
  erase() {
    this.#stores = /* @__PURE__ */ new Map();
    return this.#storage?.clearAll();
  }
  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
