import { ignoreOverride as s } from "./Options.js";
import { selectParser as u } from "./selectParser.js";
function h(t, e, n = !1) {
  const i = e.seen.get(t);
  if (e.override) {
    const r = e.override?.(t, e, i, n);
    if (r !== s)
      return r;
  }
  if (i && !n) {
    const r = p(i, e);
    if (r !== void 0)
      return r;
  }
  const c = { def: t, path: e.currentPath, jsonSchema: void 0 };
  e.seen.set(t, c);
  const a = u(t, t.typeName, e), o = typeof a == "function" ? h(a(), e) : a;
  if (o && g(t, e, o), e.postProcess) {
    const r = e.postProcess(o, t, e);
    return c.jsonSchema = o, r;
  }
  return c.jsonSchema = o, o;
}
const p = (t, e) => {
  switch (e.$refStrategy) {
    case "root":
      return { $ref: t.path.join("/") };
    case "relative":
      return { $ref: l(e.currentPath, t.path) };
    case "none":
    case "seen":
      return t.path.length < e.currentPath.length && t.path.every((n, i) => e.currentPath[i] === n) ? (console.warn(`Recursive reference detected at ${e.currentPath.join("/")}! Defaulting to any`), {}) : e.$refStrategy === "seen" ? {} : void 0;
  }
}, l = (t, e) => {
  let n = 0;
  for (; n < t.length && n < e.length && t[n] === e[n]; n++)
    ;
  return [(t.length - n).toString(), ...e.slice(n)].join("/");
}, g = (t, e, n) => (t.description && (n.description = t.description, e.markdownDescription && (n.markdownDescription = t.description)), n);
export {
  h as parseDef
};
//# sourceMappingURL=parseDef.js.map
