"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 ValueCaster_exports = {};
__export(ValueCaster_exports, {
  ValueCaster: () => ValueCaster
});
module.exports = __toCommonJS(ValueCaster_exports);
var import_model = require("@matter/model");
var import_protocol = require("@matter/protocol");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
function ValueCaster(schema, owner) {
  const metatype = schema.effectiveMetatype ?? import_model.Metatype.any;
  let cast;
  switch (metatype) {
    case import_model.Metatype.object:
      cast = StructCaster(schema, owner);
      break;
    case import_model.Metatype.array:
      cast = ListCaster(schema, owner);
      break;
    default:
      cast = import_model.Metatype.cast[metatype];
      break;
  }
  if (schema.quality.nullable) {
    return (value) => {
      if (value === null) {
        return value;
      }
      return cast(value);
    };
  }
  return cast;
}
function StructCaster(schema, owner) {
  const memberConfigs = {};
  for (const member of owner.membersOf(schema)) {
    const config = { name: member.propertyName, cast: owner.get(member).cast };
    memberConfigs[config.name] = config;
    const lowerName = member.name.toLowerCase();
    if (!memberConfigs[lowerName]) {
      memberConfigs[lowerName] = config;
    }
  }
  const castToObject = import_model.Metatype.cast.object;
  return (value) => {
    const input = castToObject(value);
    const output = {};
    for (const key in input) {
      let config = memberConfigs[key];
      if (config === void 0) {
        config = memberConfigs[key.toLowerCase()];
      }
      if (config === void 0) {
        throw new import_model.UnsupportedCastError(`Property "${key}" is unsupported`);
      }
      output[config.name] = config.cast(input[key]);
    }
    return output;
  };
}
function ListCaster(schema, owner) {
  const entry = schema.listEntry;
  if (entry === void 0) {
    throw new import_protocol.SchemaImplementationError(new import_model.DataModelPath(schema.path), "List schema has no entry definition");
  }
  const castToArray = import_model.Metatype.cast.array;
  const castEntry = owner.get(entry).cast;
  return (value) => castToArray(value)?.map(castEntry);
}
//# sourceMappingURL=ValueCaster.js.map
