"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 ServerEndpointStore_exports = {};
__export(ServerEndpointStore_exports, {
  ServerEndpointStore: () => ServerEndpointStore
});
module.exports = __toCommonJS(ServerEndpointStore_exports);
var import_EndpointStore = require("#storage/EndpointStore.js");
var import_general = require("@matter/general");
var import_DatasourceStore = require("./DatasourceStore.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const NUMBER_KEY = "__number__";
class ServerEndpointStore extends import_EndpointStore.EndpointStore {
  #number;
  #childStorage;
  #childStores = {};
  get number() {
    return this.#number;
  }
  set number(number) {
    if (this.#number !== number) {
      this.#number = number;
    }
  }
  constructor(storage) {
    super(storage);
    this.#childStorage = storage.createContext("parts");
  }
  /**
   * Create a {@link Datasource.Store} for a behavior.
   */
  createStoreForBehavior(behaviorId) {
    const initialValues = this.consumeInitialValues(behaviorId);
    return (0, import_DatasourceStore.DatasourceStore)(this, behaviorId, initialValues);
  }
  /**
   * Invoke a function on this store and the stores of descendant parts.
   */
  visit(fn) {
    fn(this);
    for (const child of Object.values(this.#childStores)) {
      child.visit(fn);
    }
  }
  childStoreFor(endpoint) {
    if (!endpoint.lifecycle.hasId) {
      throw new import_general.ImplementationError("Cannot access endpoint storage because endpoint has no assigned ID");
    }
    return this.storeForPartId(endpoint.id);
  }
  storeForPartId(partId) {
    let store = this.#childStores[partId];
    if (store === void 0) {
      store = this.#childStores[partId] = new ServerEndpointStore(this.#childStorage.createContext(partId));
    }
    return store;
  }
  async saveNumber() {
    await this.storage.set(NUMBER_KEY, this.number);
  }
  async load() {
    await super.load();
    const number = await this.storage.get(NUMBER_KEY, -1);
    if (number !== -1) {
      this.#number = number;
    }
    await this.#loadSubparts();
  }
  /**
   * Erase the child storage for one part
   */
  async eraseChildStoreFor(endpoint) {
    const partId = endpoint.id;
    const store = this.#childStores[partId];
    await store.erase();
    delete this.#childStores[partId];
  }
  async #loadSubparts() {
    const knownParts = await this.#childStorage.contexts();
    for (const partId of knownParts) {
      await this.#loadKnownChildStores(partId);
    }
  }
  async #loadKnownChildStores(partId) {
    const endpointStore = new ServerEndpointStore(this.#childStorage.createContext(partId));
    this.#childStores[partId] = endpointStore;
    await endpointStore.load();
  }
}
//# sourceMappingURL=ServerEndpointStore.js.map
