/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { InternalError } from "@matter/general";
import { NodeStore } from "../NodeStore.js";
import { ClientEndpointStore } from "./ClientEndpointStore.js";
class ClientNodeStore extends NodeStore {
  #id;
  #storage;
  #stores = /* @__PURE__ */ new Map();
  #write;
  #isPreexisting;
  constructor(id, storage, isPreexisting) {
    super(storage);
    this.#id = id;
    this.#isPreexisting = isPreexisting;
  }
  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 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 InternalError(`Endpoint storage ${this.toString()}.endpoints.${number} accessed before load`);
    }
    let store = this.#stores.get(number);
    if (store === void 0) {
      store = new 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 ClientEndpointStore(this, number, this.#storage.createContext(id));
      await store.load();
      this.#stores.set(number, store);
    }
  }
}
export {
  ClientNodeStore
};
//# sourceMappingURL=ClientNodeStore.js.map
