"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 ClientEndpointStore_exports = {};
__export(ClientEndpointStore_exports, {
  ClientEndpointStore: () => ClientEndpointStore
});
module.exports = __toCommonJS(ClientEndpointStore_exports);
var import_EndpointStore = require("#storage/EndpointStore.js");
var import_DatasourceStore = require("#storage/server/DatasourceStore.js");
var import_protocol = require("@matter/protocol");
var import_DatasourceCache = require("./DatasourceCache.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class ClientEndpointStore extends import_EndpointStore.EndpointStore {
  #owner;
  #number;
  #caches = /* @__PURE__ */ new Set();
  constructor(owner, number, storage) {
    super(storage);
    this.#owner = owner;
    this.#number = number;
    if (!this.#number) {
      const commissioning = this.initialValues.get("commissioning");
      if (commissioning?.peerAddress) {
        commissioning.peerAddress = (0, import_protocol.PeerAddress)(commissioning.peerAddress);
      }
    }
  }
  get number() {
    return this.#number;
  }
  /**
   * Shortcut to persisted peer address so we can use in logging prior to full initialization.
   */
  get peerAddress() {
    return this.initialValues.get("commissioning")?.["peerAddress"];
  }
  /**
   * Create a {@link Datasource.ExternallyMutableStore} for a behavior.
   */
  createStoreForBehavior(behaviorId) {
    const initialValues = this.consumeInitialValues(behaviorId);
    const cache = new import_DatasourceCache.DatasourceCache({
      writer: this.#owner.write,
      endpointNumber: this.#number,
      behaviorId,
      initialValues,
      localWriter: this.#owner.localWriter,
      buffer: this.#owner.buffer
    });
    this.#caches.add(cache);
    return cache;
  }
  async erase() {
    for (const cache of this.#caches) {
      cache.discard();
    }
    this.#caches.clear();
    await super.erase();
  }
  /**
   * Create a {@link Datasource.Store} for a behavior that does not track a remote cluster.
   */
  createStoreForLocalBehavior(behaviorId) {
    const initialValues = this.consumeInitialValues(behaviorId);
    return (0, import_DatasourceStore.DatasourceStore)(this, behaviorId, initialValues);
  }
}
//# sourceMappingURL=ClientEndpointStore.js.map
