"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 EncodedBitmap_exports = {};
__export(EncodedBitmap_exports, {
  EncodedBitmap: () => EncodedBitmap
});
module.exports = __toCommonJS(EncodedBitmap_exports);
var import_ElementTag = require("#common/ElementTag.js");
var import_feature_map_element = require("#standard/elements/feature-map.element.js");
var import_general = require("@matter/general");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
function EncodedBitmap(model, value) {
  if (typeof value !== "object") {
    return value;
  }
  let nameGenerator;
  if (model.tag === import_ElementTag.ElementTag.Attribute && model.id === import_feature_map_element.FeatureMap.id) {
    nameGenerator = (model2) => model2.title === void 0 ? model2.propertyName : (0, import_general.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;
}
//# sourceMappingURL=EncodedBitmap.js.map
