"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 ValueValidator_exports = {};
__export(ValueValidator_exports, {
  ValueValidator: () => ValueValidator
});
module.exports = __toCommonJS(ValueValidator_exports);
var import_general = require("#general");
var import_model = require("#model");
var import_protocol = require("#protocol");
var import_types = require("#types");
var import_Internal = require("../managed/Internal.js");
var import_assertions = require("./assertions.js");
var import_conformance = require("./conformance.js");
var import_constraint = require("./constraint.js");
/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
function ValueValidator(schema, supervisor) {
  if (schema instanceof import_model.ClusterModel) {
    return createStructValidator(schema, supervisor);
  }
  let validator;
  const metabase = schema.metabase;
  switch (metabase?.metatype) {
    case import_model.Metatype.enum:
      validator = createEnumValidator(schema, supervisor);
      break;
    case import_model.Metatype.bitmap:
      validator = createBitmapValidator(schema, supervisor);
      break;
    case import_model.Metatype.integer:
      validator = createIntegerValidator(schema, supervisor, metabase.name);
      break;
    case import_model.Metatype.float:
      validator = createSimpleValidator(schema, supervisor, import_assertions.assertNumeric);
      break;
    case import_model.Metatype.boolean:
      validator = createSimpleValidator(schema, supervisor, import_assertions.assertBoolean);
      break;
    case import_model.Metatype.string:
      validator = createSimpleValidator(schema, supervisor, import_assertions.assertString);
      break;
    case import_model.Metatype.bytes:
      validator = createSimpleValidator(schema, supervisor, import_assertions.assertBytes);
      break;
    case import_model.Metatype.object:
      validator = createStructValidator(schema, supervisor);
      break;
    case import_model.Metatype.array:
      validator = createListValidator(schema, supervisor);
      break;
    case import_model.Metatype.date:
    case import_model.Metatype.duration:
    case import_model.Metatype.any:
      break;
    case void 0:
      const type = schema.effectiveType;
      if (type === void 0) {
        if (schema.isDisallowed || schema.isDeprecated && !schema.type) {
          break;
        }
        throw new import_protocol.SchemaImplementationError((0, import_model.DataModelPath)(schema.path), `No type defined`);
      }
      throw new import_protocol.SchemaImplementationError(
        (0, import_model.DataModelPath)(schema.path),
        `Cannot determine metatype for type "${type}"`
      );
    default:
      throw new import_protocol.SchemaImplementationError(
        (0, import_model.DataModelPath)(schema.path),
        `Unsupported validation metatype ${metabase?.metatype}`
      );
  }
  validator = createNullValidator(schema, validator);
  validator = (0, import_conformance.createConformanceValidator)(schema, supervisor, validator);
  return validator;
}
function createNullValidator(schema, nextValidator) {
  if (schema.effectiveQuality.nullable === true) {
    return (value, options, location) => {
      if (value !== null) {
        nextValidator?.(value, options, location);
      }
    };
  }
  return nextValidator;
}
function createEnumValidator(schema, supervisor) {
  const valid = new Set(
    supervisor.membersOf(schema).map((member) => member.id).filter((e) => e !== void 0)
  );
  const constraint = schema.effectiveConstraint;
  const constraintValidator = constraint.in ? (0, import_constraint.createConstraintValidator)(schema.effectiveConstraint, schema, supervisor) : void 0;
  return (value, session, location) => {
    (0, import_assertions.assertNumber)(value, location);
    if (!valid.has(value)) {
      throw new import_protocol.DatatypeError(location, "defined in enum", value, import_types.Status.ConstraintError);
    }
    constraintValidator?.(value, session, location);
  };
}
function createBitmapValidator(schema, supervisor) {
  const fields = {};
  for (const field of supervisor.membersOf(schema)) {
    const constraint = field.effectiveConstraint;
    let max;
    if (typeof constraint.min === "number" && typeof constraint.max === "number") {
      max = Math.pow(2, constraint.max - constraint.min + 1) - 1;
    } else {
      max = 1;
    }
    let name;
    if (field?.parent?.id === import_model.FeatureMap.id) {
      name = (0, import_general.camelize)(field.title ?? field.name);
    } else {
      name = (0, import_general.camelize)(field.name);
    }
    fields[name] = {
      schema: field,
      max
    };
  }
  return (value, _session, location) => {
    (0, import_assertions.assertObject)(value, location);
    for (const key in value) {
      const field = fields[key];
      const subpath = location.path.at(key);
      if (field === void 0) {
        throw new import_protocol.DatatypeError(subpath, "defined in bitmap", key);
      }
      const fieldValue = value[key];
      if (fieldValue === void 0) {
        continue;
      }
      if (field.max === 1) {
        (0, import_assertions.assertBoolean)(fieldValue, subpath);
      } else {
        (0, import_assertions.assertNumber)(fieldValue, subpath);
        if (fieldValue > field.max) {
          throw new import_protocol.DatatypeError(subpath, "in range of bit field", fieldValue);
        }
      }
    }
  };
}
function createSimpleValidator(schema, supervisor, validateType) {
  const validateConstraint = (0, import_constraint.createConstraintValidator)(schema.effectiveConstraint, schema, supervisor);
  return (value, session, location) => {
    if (value === void 0) {
      return;
    }
    validateType(value, location);
    validateConstraint?.(value, session, location);
  };
}
function createIntegerValidator(schema, supervisor, name) {
  const nullable = schema.effectiveQuality.nullable;
  let assertion;
  if (schema.type === "epoch-s" || schema.type === "epoch-us") {
    if (nullable) {
      assertion = import_assertions.assertInt.nullable[schema.type];
    } else {
      assertion = import_assertions.assertInt.notNullable[schema.type];
    }
  }
  if (assertion === void 0) {
    if (nullable) {
      assertion = import_assertions.assertInt.nullable[name];
    } else {
      assertion = import_assertions.assertInt.notNullable[name];
    }
  }
  if (assertion === void 0) {
    throw new import_general.InternalError(`No integer assertion implemented for integer type ${name}`);
  }
  return createSimpleValidator(schema, supervisor, assertion);
}
function createStructValidator(schema, supervisor) {
  const validators = {};
  for (const field of supervisor.membersOf(schema)) {
    if (import_model.AttributeModel.isGlobal(field) || field.isDeprecated && !field.type) {
      continue;
    }
    const validate = supervisor.get(field).validate;
    if (validate) {
      validators[(0, import_general.camelize)(field.name)] = validate;
    }
  }
  const validateStruct = (struct, session, location) => {
    (0, import_assertions.assertObject)(struct, location);
    const sublocation = {
      path: location.path.at(""),
      siblings: struct,
      choices: {}
    };
    for (const name in validators) {
      let value;
      if (struct[import_protocol.Val.properties]) {
        const rootOwner = struct[import_Internal.Internal.reference];
        const properties = struct[import_protocol.Val.properties](rootOwner, session);
        if (name in properties) {
          value = properties[name];
        } else {
          value = struct[name];
        }
      } else {
        value = struct[name];
      }
      sublocation.path.id = name;
      validators[name](value, session, sublocation);
    }
    for (const name in sublocation.choices) {
      const choice = sublocation.choices[name];
      if (choice.count < choice.target && !choice.orLess) {
        throw new import_protocol.ConformanceError(
          schema,
          location,
          `Too few fields present (${choice.count} of min ${choice.target})`,
          name
        );
      }
      if (choice.count > choice.target && !choice.orMore) {
        throw new import_protocol.ConformanceError(
          schema,
          location,
          `Too many fields present (${choice.count} of max ${choice.target})`,
          name
        );
      }
    }
  };
  return validateStruct;
}
function createListValidator(schema, supervisor) {
  const entry = schema.listEntry;
  let validateEntries;
  if (entry) {
    const entryValidator = supervisor.get(entry).validate;
    if (entryValidator) {
      validateEntries = (list, session, location) => {
        if (!list || typeof list[Symbol.iterator] !== "function") {
          throw new import_protocol.DatatypeError(location, "a list", list);
        }
        let index = 0;
        const sublocation = {
          path: location.path.at("")
        };
        for (const e of list) {
          if (e === void 0 || e === null) {
            continue;
          }
          sublocation.path.id = index;
          entryValidator(e, session, sublocation);
          index++;
        }
      };
    }
  }
  const validateConstraint = (0, import_constraint.createConstraintValidator)(schema.constraint, schema, supervisor);
  return (value, session, location) => {
    (0, import_assertions.assertArray)(value, location);
    validateConstraint?.(value, session, location);
    validateEntries?.(value, session, location);
  };
}
//# sourceMappingURL=ValueValidator.js.map
