"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_general = require("#general");
var import_model = require("#model");
var import_errors = require("../../../errors.js");
/**
 * @license
 * Copyright 2022-2024 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
function ValueCaster(schema, owner) {
  const metatype = schema.effectiveMetatype ?? import_model.Metatype.any;
  switch (metatype) {
    case import_model.Metatype.object:
      return StructCaster(schema, owner);
    case import_model.Metatype.array:
      return ListCaster(schema, owner);
    default:
      return import_model.Metatype.cast[metatype];
  }
}
function StructCaster(schema, owner) {
  const memberConfigs = {};
  for (const member of schema.activeMembers) {
    if (member.isDeprecated) {
      continue;
    }
    const config = { name: (0, import_general.camelize)(member.name), 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_errors.SchemaImplementationError((0, 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
