"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 NameResolver_exports = {};
__export(NameResolver_exports, {
  NameResolver: () => NameResolver
});
module.exports = __toCommonJS(NameResolver_exports);
var import_model = require("@matter/model");
var import_Internal = require("./Internal.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
function NameResolver(supervisor, model, name) {
  if (model === void 0) {
    return;
  }
  if (name.includes(".")) {
    return resolveQualifiedName(supervisor, model, name);
  }
  if (model === supervisor.schema || model.id !== void 0 && supervisor.schema.tag === model.tag && supervisor.schema.id === model.id) {
    if (!supervisor.memberNames.has(name)) {
      return;
    }
    return createDirectResolver(findMemberId(supervisor.schema, name));
  }
  if (!(model instanceof import_model.ValueModel) || model.effectiveMetatype !== "object") {
    return createIndirectResolver();
  }
  const member = supervisor.membersOf(model).find((model2) => model2.propertyName === name);
  if (member) {
    return createDirectResolver(member.id);
  }
  return createIndirectResolver();
  function createDirectResolver(id) {
    if (id === void 0) {
      return (val) => val?.[name];
    }
    return (val) => {
      const struct = val;
      if (struct === void 0 || struct === null) {
        return void 0;
      }
      if (id in struct) {
        return struct[id];
      }
      return struct[name];
    };
  }
  function findMemberId(schema, name2) {
    return supervisor.membersOf(schema).find((model2) => model2.propertyName === name2)?.id;
  }
  function createIndirectResolver() {
    const parentSchema = model.parent;
    if (!(parentSchema instanceof import_model.ValueModel) && !(parentSchema instanceof import_model.ClusterModel)) {
      return;
    }
    const parentReader = NameResolver(supervisor, parentSchema, name);
    if (!parentReader) {
      return;
    }
    return (val) => {
      const parent = val?.[import_Internal.Internal.reference]?.parent?.owner;
      if (parent) {
        return parentReader(parent);
      }
    };
  }
}
function resolveQualifiedName(supervisor, model, name) {
  const segments = name.split(".");
  const baseResolver = NameResolver(supervisor, model, segments[0]);
  if (!baseResolver) {
    return;
  }
  const rest = segments.slice(1);
  return (val) => {
    let result = baseResolver(val);
    for (const segment of rest) {
      if (result === void 0 || result === null) {
        return void 0;
      }
      result = result?.[segment];
    }
    return result;
  };
}
//# sourceMappingURL=NameResolver.js.map
