/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { ElementTag } from "#common/ElementTag.js";
import { FeatureMap } from "#standard/elements/feature-map.element.js";
import { camelize } from "@matter/general";
function EncodedBitmap(model, value) {
  if (typeof value !== "object") {
    return value;
  }
  let nameGenerator;
  if (model.tag === ElementTag.Attribute && model.id === FeatureMap.id) {
    nameGenerator = (model2) => model2.title === void 0 ? model2.propertyName : camelize(model2.title);
  } else {
    nameGenerator = (model2) => model2.propertyName;
  }
  let bitmap = 0n;
  for (const field of model.children) {
    const constraintValue = field.constraint.value;
    let min;
    let max;
    if (typeof constraintValue === "number") {
      min = constraintValue;
      max = constraintValue;
    } else {
      const cm = field.constraint.min;
      const cx = field.constraint.max;
      if (typeof cm !== "number" || typeof cx !== "number") {
        continue;
      }
      min = cm;
      max = cx;
    }
    const name = nameGenerator(field);
    const bitval = value[name];
    if (!bitval) {
      continue;
    }
    if (bitval === true) {
      bitmap |= 1n << BigInt(min);
    } else if (typeof bitval === "number") {
      bitmap |= BigInt(bitval & 2 ** (max - min) - 1) << BigInt(min);
    } else {
      bitmap |= bitval & 2n ** BigInt(max - min) - 1n << BigInt(min);
    }
  }
  if (bitmap < Number.MAX_SAFE_INTEGER) {
    return Number(bitmap);
  }
  return bitmap;
}
export {
  EncodedBitmap
};
//# sourceMappingURL=EncodedBitmap.js.map
