/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Constraint } from "#aspects/Constraint.js";
import { isDeepEqual } from "#general";
import { Model as Schema, StructuralModelError } from "#models/Model.js";
import { ValueModel } from "#models/ValueModel.js";
import { Scope } from "./Scope.js";
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 (!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 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 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 = 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 (Schema.types[tag] === void 0) {
        throw new StructuralModelError(`Unknown modifier set key ${pluralTag}`);
      }
      const members = scope.membersOf(target, { conformance: Scope.ConformantConformance, tags: [tag] });
      for (const [name, mod] of Object.entries(mods)) {
        const member = members.for(name);
        if (member === void 0) {
          throw new StructuralModelError(`${member} has no ${tag} "${name}"`);
        }
        if (!(member instanceof ValueModel)) {
          throw new 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 = {}));
export {
  ClusterModifier
};
//# sourceMappingURL=ClusterModifier.js.map
