"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 MutableEndpoint_exports = {};
__export(MutableEndpoint_exports, {
  MutableEndpoint: () => MutableEndpoint
});
module.exports = __toCommonJS(MutableEndpoint_exports);
var import_cluster_behavior_utils = require("#behavior/cluster/cluster-behavior-utils.js");
var import_SupportedBehaviors = require("../properties/SupportedBehaviors.js");
var import_SupportedClientClusters = require("../properties/SupportedClientClusters.js");
var import_EndpointType = require("./EndpointType.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
function MutableEndpoint(options) {
  const type = (0, import_EndpointType.EndpointType)(options);
  let clientClusters = type.clientClusters;
  const mandatoryClients = options.requirements?.client?.mandatory ?? {};
  if (Object.keys(mandatoryClients).length > 0) {
    const merged = import_SupportedClientClusters.SupportedClientClusters.extend(clientClusters, Object.values(mandatoryClients));
    if (Object.keys(merged).some((k) => !(k in clientClusters))) {
      clientClusters = merged;
    }
  }
  let defaults;
  return {
    ...type,
    clientClusters,
    get defaults() {
      if (!defaults) {
        defaults = {};
        for (const name in type.behaviors) {
          defaults[name] = type.behaviors[name].defaults;
        }
      }
      return defaults;
    },
    set(defaults2) {
      const newBehaviors = Array();
      for (const name in this.behaviors) {
        const updates = defaults2[name];
        const behavior = this.behaviors[name];
        if (updates) {
          newBehaviors.push(behavior.set(updates));
        }
      }
      return this.with(...newBehaviors);
    },
    with(...behaviors) {
      const serverArgs = [];
      const clientArgs = [];
      for (const b of behaviors) {
        if ((0, import_cluster_behavior_utils.isClientBehavior)(b)) {
          clientArgs.push(b);
        } else {
          serverArgs.push(b);
        }
      }
      return MutableEndpoint({
        ...options,
        behaviors: serverArgs.length ? import_SupportedBehaviors.SupportedBehaviors.extend(this.behaviors, serverArgs) : this.behaviors,
        clientClusters: clientArgs.length ? import_SupportedClientClusters.SupportedClientClusters.extend(this.clientClusters, clientArgs) : this.clientClusters
      });
    },
    withBehaviors(...behaviors) {
      return MutableEndpoint({
        ...options,
        behaviors: import_SupportedBehaviors.SupportedBehaviors.extend(this.behaviors, behaviors),
        clientClusters: this.clientClusters
      });
    },
    withClientClusters(...clientClusters2) {
      return MutableEndpoint({
        ...options,
        behaviors: this.behaviors,
        clientClusters: import_SupportedClientClusters.SupportedClientClusters.extend(this.clientClusters, clientClusters2)
      });
    }
  };
}
//# sourceMappingURL=MutableEndpoint.js.map
