import { __exports as l } from "../../../../../../../_virtual/index21.js";
import { __require as F } from "./codegen/index.js";
import { __require as O } from "../runtime/validation_error.js";
import { __require as U } from "./names.js";
import { __require as z } from "./resolve.js";
import { __require as D } from "./util.js";
import { __require as k } from "./validate/index.js";
var q;
function M() {
  if (q) return l;
  q = 1, Object.defineProperty(l, "__esModule", { value: !0 }), l.resolveSchema = l.getCompilingSchema = l.resolveRef = l.compileSchema = l.SchemaEnv = void 0;
  const c = F(), C = O(), f = U(), d = z(), I = D(), b = k();
  class v {
    constructor(t) {
      var s;
      this.refs = {}, this.dynamicAnchors = {};
      let o;
      typeof t.schema == "object" && (o = t.schema), this.schema = t.schema, this.schemaId = t.schemaId, this.root = t.root || this, this.baseId = (s = t.baseId) !== null && s !== void 0 ? s : (0, d.normalizeId)(o?.[t.schemaId || "$id"]), this.schemaPath = t.schemaPath, this.localRefs = t.localRefs, this.meta = t.meta, this.$async = o?.$async, this.refs = {};
    }
  }
  l.SchemaEnv = v;
  function y(e) {
    const t = E.call(this, e);
    if (t)
      return t;
    const s = (0, d.getFullPath)(this.opts.uriResolver, e.root.baseId), { es5: o, lines: n } = this.opts.code, { ownProperties: a } = this.opts, i = new c.CodeGen(this.scope, { es5: o, lines: n, ownProperties: a });
    let r;
    e.$async && (r = i.scopeValue("Error", {
      ref: C.default,
      code: (0, c._)`require("ajv/dist/runtime/validation_error").default`
    }));
    const h = i.scopeName("validate");
    e.validateName = h;
    const m = {
      gen: i,
      allErrors: this.opts.allErrors,
      data: f.default.data,
      parentData: f.default.parentData,
      parentDataProperty: f.default.parentDataProperty,
      dataNames: [f.default.data],
      dataPathArr: [c.nil],
      // TODO can its length be used as dataLevel if nil is removed?
      dataLevel: 0,
      dataTypes: [],
      definedProperties: /* @__PURE__ */ new Set(),
      topSchemaRef: i.scopeValue("schema", this.opts.code.source === !0 ? { ref: e.schema, code: (0, c.stringify)(e.schema) } : { ref: e.schema }),
      validateName: h,
      ValidationError: r,
      schema: e.schema,
      schemaEnv: e,
      rootId: s,
      baseId: e.baseId || s,
      schemaPath: c.nil,
      errSchemaPath: e.schemaPath || (this.opts.jtd ? "" : "#"),
      errorPath: (0, c._)`""`,
      opts: this.opts,
      self: this
    };
    let p;
    try {
      this._compilations.add(e), (0, b.validateFunctionCode)(m), i.optimize(this.opts.code.optimize);
      const g = i.toString();
      p = `${i.scopeRefs(f.default.scope)}return ${g}`, this.opts.code.process && (p = this.opts.code.process(p, e));
      const u = new Function(`${f.default.self}`, `${f.default.scope}`, p)(this, this.scope.get());
      if (this.scope.value(h, { ref: u }), u.errors = null, u.schema = e.schema, u.schemaEnv = e, e.$async && (u.$async = !0), this.opts.code.source === !0 && (u.source = { validateName: h, validateCode: g, scopeValues: i._values }), this.opts.unevaluated) {
        const { props: P, items: S } = m;
        u.evaluated = {
          props: P instanceof c.Name ? void 0 : P,
          items: S instanceof c.Name ? void 0 : S,
          dynamicProps: P instanceof c.Name,
          dynamicItems: S instanceof c.Name
        }, u.source && (u.source.evaluated = (0, c.stringify)(u.evaluated));
      }
      return e.validate = u, e;
    } catch (g) {
      throw delete e.validate, delete e.validateName, p && this.logger.error("Error compiling schema, function code:", p), g;
    } finally {
      this._compilations.delete(e);
    }
  }
  l.compileSchema = y;
  function $(e, t, s) {
    var o;
    s = (0, d.resolveUrl)(this.opts.uriResolver, t, s);
    const n = e.refs[s];
    if (n)
      return n;
    let a = V.call(this, e, s);
    if (a === void 0) {
      const i = (o = e.localRefs) === null || o === void 0 ? void 0 : o[s], { schemaId: r } = this.opts;
      i && (a = new v({ schema: i, schemaId: r, root: e, baseId: t }));
    }
    if (a !== void 0)
      return e.refs[s] = N.call(this, a);
  }
  l.resolveRef = $;
  function N(e) {
    return (0, d.inlineRef)(e.schema, this.opts.inlineRefs) ? e.schema : e.validate ? e : y.call(this, e);
  }
  function E(e) {
    for (const t of this._compilations)
      if (w(t, e))
        return t;
  }
  l.getCompilingSchema = E;
  function w(e, t) {
    return e.schema === t.schema && e.root === t.root && e.baseId === t.baseId;
  }
  function V(e, t) {
    let s;
    for (; typeof (s = this.refs[t]) == "string"; )
      t = s;
    return s || this.schemas[t] || _.call(this, e, t);
  }
  function _(e, t) {
    const s = this.opts.uriResolver.parse(t), o = (0, d._getFullPath)(this.opts.uriResolver, s);
    let n = (0, d.getFullPath)(this.opts.uriResolver, e.baseId, void 0);
    if (Object.keys(e.schema).length > 0 && o === n)
      return R.call(this, s, e);
    const a = (0, d.normalizeId)(o), i = this.refs[a] || this.schemas[a];
    if (typeof i == "string") {
      const r = _.call(this, e, i);
      return typeof r?.schema != "object" ? void 0 : R.call(this, s, r);
    }
    if (typeof i?.schema == "object") {
      if (i.validate || y.call(this, i), a === (0, d.normalizeId)(t)) {
        const { schema: r } = i, { schemaId: h } = this.opts, m = r[h];
        return m && (n = (0, d.resolveUrl)(this.opts.uriResolver, n, m)), new v({ schema: r, schemaId: h, root: e, baseId: n });
      }
      return R.call(this, s, i);
    }
  }
  l.resolveSchema = _;
  const j = /* @__PURE__ */ new Set([
    "properties",
    "patternProperties",
    "enum",
    "dependencies",
    "definitions"
  ]);
  function R(e, { baseId: t, schema: s, root: o }) {
    var n;
    if (((n = e.fragment) === null || n === void 0 ? void 0 : n[0]) !== "/")
      return;
    for (const r of e.fragment.slice(1).split("/")) {
      if (typeof s == "boolean")
        return;
      const h = s[(0, I.unescapeFragment)(r)];
      if (h === void 0)
        return;
      s = h;
      const m = typeof s == "object" && s[this.opts.schemaId];
      !j.has(r) && m && (t = (0, d.resolveUrl)(this.opts.uriResolver, t, m));
    }
    let a;
    if (typeof s != "boolean" && s.$ref && !(0, I.schemaHasRulesButRef)(s, this.RULES)) {
      const r = (0, d.resolveUrl)(this.opts.uriResolver, t, s.$ref);
      a = _.call(this, o, r);
    }
    const { schemaId: i } = this.opts;
    if (a = a || new v({ schema: s, schemaId: i, root: o, baseId: t }), a.schema !== a.root.schema)
      return a;
  }
  return l;
}
export {
  M as __require
};
//# sourceMappingURL=index.js.map
