import { __exports as f } from "../../../../../../../../_virtual/ref.js";
import { __require as S } from "../../compile/ref_error.js";
import { __require as P } from "../code.js";
import { __require as k } from "../../compile/codegen/index.js";
import { __require as I } from "../../compile/names.js";
import { __require as F } from "../../compile/index.js";
import { __require as O } from "../../compile/util.js";
var C;
function D() {
  if (C) return f;
  C = 1, Object.defineProperty(f, "__esModule", { value: !0 }), f.callRef = f.getValidate = void 0;
  const N = S(), R = P(), o = k(), d = I(), V = F(), p = O(), b = {
    keyword: "$ref",
    schemaType: "string",
    code(t) {
      const { gen: i, schema: n, it: _ } = t, { baseId: e, schemaEnv: a, validateName: g, opts: h, self: y } = _, { root: u } = a;
      if ((n === "#" || n === "#/") && e === u.baseId)
        return q();
      const c = V.resolveRef.call(y, u, e, n);
      if (c === void 0)
        throw new N.default(_.opts.uriResolver, e, n);
      if (c instanceof V.SchemaEnv)
        return E(c);
      return $(c);
      function q() {
        if (a === u)
          return v(t, g, a, a.$async);
        const r = i.scopeValue("root", { ref: u });
        return v(t, (0, o._)`${r}.validate`, u, u.$async);
      }
      function E(r) {
        const s = w(t, r);
        v(t, s, r, r.$async);
      }
      function $(r) {
        const s = i.scopeValue("schema", h.code.source === !0 ? { ref: r, code: (0, o.stringify)(r) } : { ref: r }), l = i.name("valid"), m = t.subschema({
          schema: r,
          dataTypes: [],
          schemaPath: o.nil,
          topSchemaRef: s,
          errSchemaPath: n
        }, l);
        t.mergeEvaluated(m), t.ok(l);
      }
    }
  };
  function w(t, i) {
    const { gen: n } = t;
    return i.validate ? n.scopeValue("validate", { ref: i.validate }) : (0, o._)`${n.scopeValue("wrapper", { ref: i })}.validate`;
  }
  f.getValidate = w;
  function v(t, i, n, _) {
    const { gen: e, it: a } = t, { allErrors: g, schemaEnv: h, opts: y } = a, u = y.passContext ? d.default.this : o.nil;
    _ ? c() : q();
    function c() {
      if (!h.$async)
        throw new Error("async schema referenced by sync schema");
      const r = e.let("valid");
      e.try(() => {
        e.code((0, o._)`await ${(0, R.callValidateCode)(t, i, u)}`), $(i), g || e.assign(r, !0);
      }, (s) => {
        e.if((0, o._)`!(${s} instanceof ${a.ValidationError})`, () => e.throw(s)), E(s), g || e.assign(r, !1);
      }), t.ok(r);
    }
    function q() {
      t.result((0, R.callValidateCode)(t, i, u), () => $(i), () => E(i));
    }
    function E(r) {
      const s = (0, o._)`${r}.errors`;
      e.assign(d.default.vErrors, (0, o._)`${d.default.vErrors} === null ? ${s} : ${d.default.vErrors}.concat(${s})`), e.assign(d.default.errors, (0, o._)`${d.default.vErrors}.length`);
    }
    function $(r) {
      var s;
      if (!a.opts.unevaluated)
        return;
      const l = (s = n?.validate) === null || s === void 0 ? void 0 : s.evaluated;
      if (a.props !== !0)
        if (l && !l.dynamicProps)
          l.props !== void 0 && (a.props = p.mergeEvaluated.props(e, l.props, a.props));
        else {
          const m = e.var("props", (0, o._)`${r}.evaluated.props`);
          a.props = p.mergeEvaluated.props(e, m, a.props, o.Name);
        }
      if (a.items !== !0)
        if (l && !l.dynamicItems)
          l.items !== void 0 && (a.items = p.mergeEvaluated.items(e, l.items, a.items));
        else {
          const m = e.var("items", (0, o._)`${r}.evaluated.items`);
          a.items = p.mergeEvaluated.items(e, m, a.items, o.Name);
        }
    }
  }
  return f.callRef = v, f.default = b, f;
}
export {
  D as __require
};
//# sourceMappingURL=ref.js.map
