/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import {
  camelize,
  FLOAT32_MAX,
  FLOAT32_MIN,
  INT16_MAX,
  INT16_MIN,
  INT32_MAX,
  INT32_MIN,
  INT64_MAX,
  INT64_MIN,
  INT8_MAX,
  INT8_MIN,
  UINT16_MAX,
  UINT24_MAX,
  UINT32_MAX,
  UINT64_MAX,
  UINT8_MAX
} from "#general";
import { FieldValue } from "#model";
var ModelBounds;
((ModelBounds2) => {
  function createLengthBounds(model) {
    const constraint = extractApplicableConstraint(model);
    const value = FieldValue.numericValue(constraint.value, model.type);
    if (value !== void 0) {
      return { length: value };
    }
    return createRangeBounds(constraint, "minLength", "maxLength");
  }
  ModelBounds2.createLengthBounds = createLengthBounds;
  function createNumberBounds(model) {
    const constraint = model.effectiveConstraint;
    const value = FieldValue.numericValue(constraint.value, model.type);
    if (value !== void 0) {
      return { min: value, max: value };
    }
    return createRangeBounds(constraint, "min", "max", model.type);
  }
  ModelBounds2.createNumberBounds = createNumberBounds;
  ModelBounds2.NumericRanges = {
    uint8: { min: 0, max: UINT8_MAX },
    uint16: { min: 0, max: UINT16_MAX },
    uint24: { min: 0, max: UINT24_MAX },
    uint32: { min: 0, max: UINT32_MAX },
    uint64: { min: 0, max: UINT64_MAX },
    int8: { min: INT8_MIN, max: INT8_MAX },
    int16: { min: INT16_MIN, max: INT16_MAX },
    int32: { min: INT32_MIN, max: INT32_MAX },
    int64: { min: INT64_MIN, max: INT64_MAX },
    float32: { min: FLOAT32_MIN, max: FLOAT32_MAX },
    percent: { min: 0, max: 100 },
    percent100ths: { min: 0, max: 1e4 }
  };
})(ModelBounds || (ModelBounds = {}));
function createRangeBounds(constraint, minKey, maxKey, type) {
  let min = FieldValue.numericValue(constraint.min, type);
  let max = FieldValue.numericValue(constraint.max, type);
  if (min === ModelBounds.NumericRanges[type]?.min) {
    min = void 0;
  }
  if (max === ModelBounds.NumericRanges[type]?.max) {
    max = void 0;
  }
  if (min === void 0 && max === void 0) {
    return;
  }
  const bounds = {};
  if (min !== void 0) {
    bounds[minKey] = min;
  }
  if (max !== void 0) {
    bounds[maxKey] = max;
  }
  return bounds;
}
function extractApplicableConstraint(model) {
  let constraint = model.effectiveConstraint;
  if (constraint.in) {
    const siblingName = FieldValue.referenced(constraint.in);
    if (siblingName) {
      const sibling = model.parent?.member(camelize(siblingName, true));
      const siblingConstraint = sibling.effectiveConstraint;
      if (siblingConstraint.entry) {
        constraint = siblingConstraint.entry;
      }
    }
  }
  return constraint;
}
export {
  ModelBounds,
  extractApplicableConstraint
};
//# sourceMappingURL=ModelBounds.js.map
