"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 ClusterModifier_exports = {};
__export(ClusterModifier_exports, {
  ClusterModifier: () => ClusterModifier
});
module.exports = __toCommonJS(ClusterModifier_exports);
var import_Constraint = require("#aspects/Constraint.js");
var import_general = require("#general");
var import_Model = require("#models/Model.js");
var import_ValueModel = require("#models/ValueModel.js");
var import_Scope = require("./Scope.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
var ClusterModifier;
((ClusterModifier2) => {
  function applyRequirements(target, modifications) {
    return apply(target, modifications, (element, patch) => {
      if (patch.optional !== void 0) {
        const desiredConformance = patch.optional ? "O" : "M";
        if (element.conformance.toString() !== desiredConformance) {
          if (element.isFinal) {
            element = element.clone();
          }
          element.conformance = desiredConformance;
        }
      }
      if (patch.default !== void 0) {
        if (!(0, import_general.isDeepEqual)(element.default, patch.default)) {
          if (element.isFinal) {
            element = element.clone();
          }
          element.default = patch.default;
        }
      }
      if (patch.min !== void 0) {
        const constraint = element.constraint;
        if (constraint.min !== patch.min) {
          if (element.isFinal) {
            element = element.clone();
          }
          element.constraint = new import_Constraint.Constraint({ ...element.constraint, min: patch.min });
        }
      }
      if (patch.max !== void 0) {
        const constraint = element.constraint;
        if (constraint.max !== patch.max) {
          if (element.isFinal) {
            element = element.clone();
          }
          element.constraint = new import_Constraint.Constraint({ ...element.constraint, max: patch.max });
        }
      }
      return element;
    });
  }
  ClusterModifier2.applyRequirements = applyRequirements;
  function applyPresence(target, modifications) {
    return apply(target, modifications, (element, isSupported) => {
      if (element.effectiveIsSupported === isSupported) {
        return;
      }
      if (element.isFinal) {
        element = element.clone();
      }
      element.operationalIsSupported = isSupported;
      return element;
    });
  }
  ClusterModifier2.applyPresence = applyPresence;
  function apply(target, modifications, apply2) {
    const scope = (0, import_Scope.Scope)(target);
    let model;
    for (const [pluralTag, mods] of Object.entries(modifications)) {
      const tag = pluralTag.endsWith("s") ? pluralTag.substring(0, pluralTag.length - 1) : pluralTag;
      if (import_Model.Model.types[tag] === void 0) {
        throw new import_Model.StructuralModelError(`Unknown modifier set key ${pluralTag}`);
      }
      const members = scope.membersOf(target, { conformance: import_Scope.Scope.ConformantConformance, tags: [tag] });
      for (const [name, mod] of Object.entries(mods)) {
        const member = members.for(name);
        if (member === void 0) {
          throw new import_Model.StructuralModelError(`${member} has no ${tag} "${name}"`);
        }
        if (!(member instanceof import_ValueModel.ValueModel)) {
          throw new import_Model.StructuralModelError(`Tag ${tag} cannot be modified because it is not a value element`);
        }
        const replacement = apply2(member, mod);
        if (replacement === member || replacement === void 0) {
          continue;
        }
        if (model === void 0) {
          model = target.extend();
        }
        model.children.push(replacement);
      }
    }
    return model ?? target;
  }
  ClusterModifier2.apply = apply;
})(ClusterModifier || (ClusterModifier = {}));
//# sourceMappingURL=ClusterModifier.js.map
