"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 ClusterTypeModifier_exports = {};
__export(ClusterTypeModifier_exports, {
  ClusterTypeModifier: () => ClusterTypeModifier
});
module.exports = __toCommonJS(ClusterTypeModifier_exports);
/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class ClusterTypeModifier {
  constructor(cluster) {
    this.cluster = cluster;
  }
  /**
   * Create a new cluster with modified elements.
   */
  alter(alterations) {
    return modifyElements(this.cluster, alterations, (element, alteration) => {
      for (const property in alteration) {
        element[property] = alteration[property];
      }
    });
  }
  /**
   * Create a new cluster with different attribute defaults.
   *
   * This "sets" the values of attributes when the cluster is created
   * for the first time with no persistent state.
   */
  set(values) {
    return modifyElements(this.cluster, { attributes: values }, (element, defaultValue) => {
      element.default = defaultValue;
    });
  }
  /**
   * Create a new cluster with additional features marked "mandatory".
   *
   * This informs matter.js that an application supports these elements.
   */
  enable(flags) {
    return modifyElements(this.cluster, flags, (element, flag) => {
      if (flag === true) {
        element.optional = false;
      } else if (flag === false) {
        element.optional = true;
      }
    });
  }
}
function modifyElements(definition, modifications, modifier) {
  const result = { ...definition };
  for (const elementType of ["attributes", "commands", "events"]) {
    const mods = modifications[elementType];
    if (mods === void 0) {
      continue;
    }
    let elements = result[elementType];
    if (elements === void 0) {
      continue;
    }
    let replaced = false;
    for (const elementName in mods) {
      const mod = mods[elementName];
      if (mod === void 0) {
        continue;
      }
      const element = elements[elementName];
      if (!element) {
        continue;
      }
      if (!replaced) {
        elements = result[elementType] = { ...result[elementType] };
        replaced = true;
      }
      elements[elementName] = { ...element };
      modifier(elements[elementName], mod);
    }
  }
  return result;
}
//# sourceMappingURL=ClusterTypeModifier.js.map
