import { Option, SchemaAST } from "effect";
function getTypeLiteral(ast) {
  switch (ast._tag) {
    case "TypeLiteral":
      return ast;
    case "Transformation":
      return getTypeLiteral(ast.to);
    case "Refinement":
      return getTypeLiteral(ast.from);
    case "Suspend":
      return getTypeLiteral(ast.f());
    default:
      return null;
  }
}
function findPropertySignature(schema, fieldName) {
  const tl = getTypeLiteral(schema.ast);
  if (!tl) return null;
  return tl.propertySignatures.find((p) => p.name === fieldName) ?? null;
}
function resolveFieldType(ps) {
  const t = ps.type;
  if (t._tag === "Union" && ps.isOptional) {
    const nonUndef = t.types.find((m) => m._tag !== "UndefinedKeyword");
    if (nonUndef) return nonUndef;
  }
  return t;
}
function getFieldAnnotation(schema, fieldName, getter) {
  const ps = findPropertySignature(schema, fieldName);
  if (!ps) return void 0;
  const fromPS = getter(ps);
  if (Option.isSome(fromPS)) return fromPS.value;
  const resolved = resolveFieldType(ps);
  const fromResolved = getter(resolved);
  if (Option.isSome(fromResolved)) return fromResolved.value;
  if (resolved !== ps.type) {
    const fromType = getter(ps.type);
    if (Option.isSome(fromType)) return fromType.value;
  }
  return void 0;
}
export function getStructFieldTitle(schema, fieldName) {
  return getFieldAnnotation(schema, fieldName, SchemaAST.getTitleAnnotation);
}
export function getStructFieldDescription(schema, fieldName) {
  return getFieldAnnotation(schema, fieldName, SchemaAST.getDescriptionAnnotation);
}
