import { __exports as h } from "../../../../../../../../_virtual/index18.js";
import { __require as ue } from "./boolSchema.js";
import { __require as j } from "./dataType.js";
import { __require as fe } from "./applicability.js";
import { __require as le } from "./defaults.js";
import { __require as de } from "./keyword.js";
import { __require as he } from "./subschema.js";
import { __require as me } from "../codegen/index.js";
import { __require as pe } from "../names.js";
import { __require as $e } from "../resolve.js";
import { __require as ye } from "../util.js";
import { __require as _e } from "../errors.js";
var V;
function xe() {
  if (V) return h;
  V = 1, Object.defineProperty(h, "__esModule", { value: !0 }), h.getData = h.KeywordCxt = h.validateFunctionCode = void 0;
  const w = ue(), S = j(), v = fe(), _ = j(), A = le(), $ = de(), E = he(), s = me(), o = pe(), J = $e(), d = ye(), y = _e();
  function M(e) {
    if (k(e) && (D(e), C(e))) {
      H(e);
      return;
    }
    T(e, () => (0, w.topBoolOrEmptySchema)(e));
  }
  h.validateFunctionCode = M;
  function T({ gen: e, validateName: t, schema: r, schemaEnv: a, opts: n }, i) {
    n.code.es5 ? e.func(t, (0, s._)`${o.default.data}, ${o.default.valCxt}`, a.$async, () => {
      e.code((0, s._)`"use strict"; ${b(r, n)}`), F(e, n), e.code(i);
    }) : e.func(t, (0, s._)`${o.default.data}, ${B(n)}`, a.$async, () => e.code(b(r, n)).code(i));
  }
  function B(e) {
    return (0, s._)`{${o.default.instancePath}="", ${o.default.parentData}, ${o.default.parentDataProperty}, ${o.default.rootData}=${o.default.data}${e.dynamicRef ? (0, s._)`, ${o.default.dynamicAnchors}={}` : s.nil}}={}`;
  }
  function F(e, t) {
    e.if(o.default.valCxt, () => {
      e.var(o.default.instancePath, (0, s._)`${o.default.valCxt}.${o.default.instancePath}`), e.var(o.default.parentData, (0, s._)`${o.default.valCxt}.${o.default.parentData}`), e.var(o.default.parentDataProperty, (0, s._)`${o.default.valCxt}.${o.default.parentDataProperty}`), e.var(o.default.rootData, (0, s._)`${o.default.valCxt}.${o.default.rootData}`), t.dynamicRef && e.var(o.default.dynamicAnchors, (0, s._)`${o.default.valCxt}.${o.default.dynamicAnchors}`);
    }, () => {
      e.var(o.default.instancePath, (0, s._)`""`), e.var(o.default.parentData, (0, s._)`undefined`), e.var(o.default.parentDataProperty, (0, s._)`undefined`), e.var(o.default.rootData, o.default.data), t.dynamicRef && e.var(o.default.dynamicAnchors, (0, s._)`{}`);
    });
  }
  function H(e) {
    const { schema: t, opts: r, gen: a } = e;
    T(e, () => {
      r.$comment && t.$comment && q(e), Q(e), a.let(o.default.vErrors, null), a.let(o.default.errors, 0), r.unevaluated && W(e), R(e), Z(e);
    });
  }
  function W(e) {
    const { gen: t, validateName: r } = e;
    e.evaluated = t.const("evaluated", (0, s._)`${r}.evaluated`), t.if((0, s._)`${e.evaluated}.dynamicProps`, () => t.assign((0, s._)`${e.evaluated}.props`, (0, s._)`undefined`)), t.if((0, s._)`${e.evaluated}.dynamicItems`, () => t.assign((0, s._)`${e.evaluated}.items`, (0, s._)`undefined`));
  }
  function b(e, t) {
    const r = typeof e == "object" && e[t.schemaId];
    return r && (t.code.source || t.code.process) ? (0, s._)`/*# sourceURL=${r} */` : s.nil;
  }
  function L(e, t) {
    if (k(e) && (D(e), C(e))) {
      G(e, t);
      return;
    }
    (0, w.boolOrEmptySchema)(e, t);
  }
  function C({ schema: e, self: t }) {
    if (typeof e == "boolean")
      return !e;
    for (const r in e)
      if (t.RULES.all[r])
        return !0;
    return !1;
  }
  function k(e) {
    return typeof e.schema != "boolean";
  }
  function G(e, t) {
    const { schema: r, gen: a, opts: n } = e;
    n.$comment && r.$comment && q(e), X(e), Y(e);
    const i = a.const("_errs", o.default.errors);
    R(e, i), a.var(t, (0, s._)`${i} === ${o.default.errors}`);
  }
  function D(e) {
    (0, d.checkUnknownRules)(e), z(e);
  }
  function R(e, t) {
    if (e.opts.jtd)
      return x(e, [], !1, t);
    const r = (0, S.getSchemaTypes)(e.schema), a = (0, S.coerceAndCheckDataType)(e, r);
    x(e, r, !a, t);
  }
  function z(e) {
    const { schema: t, errSchemaPath: r, opts: a, self: n } = e;
    t.$ref && a.ignoreKeywordsWithRef && (0, d.schemaHasRulesButRef)(t, n.RULES) && n.logger.warn(`$ref: keywords ignored in schema at path "${r}"`);
  }
  function Q(e) {
    const { schema: t, opts: r } = e;
    t.default !== void 0 && r.useDefaults && r.strictSchema && (0, d.checkStrictMode)(e, "default is ignored in the schema root");
  }
  function X(e) {
    const t = e.schema[e.opts.schemaId];
    t && (e.baseId = (0, J.resolveUrl)(e.opts.uriResolver, e.baseId, t));
  }
  function Y(e) {
    if (e.schema.$async && !e.schemaEnv.$async)
      throw new Error("async schema in sync schema");
  }
  function q({ gen: e, schemaEnv: t, schema: r, errSchemaPath: a, opts: n }) {
    const i = r.$comment;
    if (n.$comment === !0)
      e.code((0, s._)`${o.default.self}.logger.log(${i})`);
    else if (typeof n.$comment == "function") {
      const c = (0, s.str)`${a}/$comment`, p = e.scopeValue("root", { ref: t.root });
      e.code((0, s._)`${o.default.self}.opts.$comment(${i}, ${c}, ${p}.schema)`);
    }
  }
  function Z(e) {
    const { gen: t, schemaEnv: r, validateName: a, ValidationError: n, opts: i } = e;
    r.$async ? t.if((0, s._)`${o.default.errors} === 0`, () => t.return(o.default.data), () => t.throw((0, s._)`new ${n}(${o.default.vErrors})`)) : (t.assign((0, s._)`${a}.errors`, o.default.vErrors), i.unevaluated && ee(e), t.return((0, s._)`${o.default.errors} === 0`));
  }
  function ee({ gen: e, evaluated: t, props: r, items: a }) {
    r instanceof s.Name && e.assign((0, s._)`${t}.props`, r), a instanceof s.Name && e.assign((0, s._)`${t}.items`, a);
  }
  function x(e, t, r, a) {
    const { gen: n, schema: i, data: c, allErrors: p, opts: f, self: l } = e, { RULES: u } = l;
    if (i.$ref && (f.ignoreKeywordsWithRef || !(0, d.schemaHasRulesButRef)(i, u))) {
      n.block(() => N(e, "$ref", u.all.$ref.definition));
      return;
    }
    f.jtd || te(e, t), n.block(() => {
      for (const m of u.rules)
        O(m);
      O(u.post);
    });
    function O(m) {
      (0, v.shouldUseGroup)(i, m) && (m.type ? (n.if((0, _.checkDataType)(m.type, c, f.strictNumbers)), P(e, m), t.length === 1 && t[0] === m.type && r && (n.else(), (0, _.reportTypeError)(e)), n.endIf()) : P(e, m), p || n.if((0, s._)`${o.default.errors} === ${a || 0}`));
    }
  }
  function P(e, t) {
    const { gen: r, schema: a, opts: { useDefaults: n } } = e;
    n && (0, A.assignDefaults)(e, t.type), r.block(() => {
      for (const i of t.rules)
        (0, v.shouldUseRule)(a, i) && N(e, i.keyword, i.definition, t.type);
    });
  }
  function te(e, t) {
    e.schemaEnv.meta || !e.opts.strictTypes || (re(e, t), e.opts.allowUnionTypes || ae(e, t), se(e, e.dataTypes));
  }
  function re(e, t) {
    if (t.length) {
      if (!e.dataTypes.length) {
        e.dataTypes = t;
        return;
      }
      t.forEach((r) => {
        I(e.dataTypes, r) || g(e, `type "${r}" not allowed by context "${e.dataTypes.join(",")}"`);
      }), oe(e, t);
    }
  }
  function ae(e, t) {
    t.length > 1 && !(t.length === 2 && t.includes("null")) && g(e, "use allowUnionTypes to allow union type keyword");
  }
  function se(e, t) {
    const r = e.self.RULES.all;
    for (const a in r) {
      const n = r[a];
      if (typeof n == "object" && (0, v.shouldUseRule)(e.schema, n)) {
        const { type: i } = n.definition;
        i.length && !i.some((c) => ne(t, c)) && g(e, `missing type "${i.join(",")}" for keyword "${a}"`);
      }
    }
  }
  function ne(e, t) {
    return e.includes(t) || t === "number" && e.includes("integer");
  }
  function I(e, t) {
    return e.includes(t) || t === "integer" && e.includes("number");
  }
  function oe(e, t) {
    const r = [];
    for (const a of e.dataTypes)
      I(t, a) ? r.push(a) : t.includes("integer") && a === "number" && r.push("integer");
    e.dataTypes = r;
  }
  function g(e, t) {
    const r = e.schemaEnv.baseId + e.errSchemaPath;
    t += ` at "${r}" (strictTypes)`, (0, d.checkStrictMode)(e, t, e.opts.strictTypes);
  }
  class K {
    constructor(t, r, a) {
      if ((0, $.validateKeywordUsage)(t, r, a), this.gen = t.gen, this.allErrors = t.allErrors, this.keyword = a, this.data = t.data, this.schema = t.schema[a], this.$data = r.$data && t.opts.$data && this.schema && this.schema.$data, this.schemaValue = (0, d.schemaRefOrVal)(t, this.schema, a, this.$data), this.schemaType = r.schemaType, this.parentSchema = t.schema, this.params = {}, this.it = t, this.def = r, this.$data)
        this.schemaCode = t.gen.const("vSchema", U(this.$data, t));
      else if (this.schemaCode = this.schemaValue, !(0, $.validSchemaType)(this.schema, r.schemaType, r.allowUndefined))
        throw new Error(`${a} value must be ${JSON.stringify(r.schemaType)}`);
      ("code" in r ? r.trackErrors : r.errors !== !1) && (this.errsCount = t.gen.const("_errs", o.default.errors));
    }
    result(t, r, a) {
      this.failResult((0, s.not)(t), r, a);
    }
    failResult(t, r, a) {
      this.gen.if(t), a ? a() : this.error(), r ? (this.gen.else(), r(), this.allErrors && this.gen.endIf()) : this.allErrors ? this.gen.endIf() : this.gen.else();
    }
    pass(t, r) {
      this.failResult((0, s.not)(t), void 0, r);
    }
    fail(t) {
      if (t === void 0) {
        this.error(), this.allErrors || this.gen.if(!1);
        return;
      }
      this.gen.if(t), this.error(), this.allErrors ? this.gen.endIf() : this.gen.else();
    }
    fail$data(t) {
      if (!this.$data)
        return this.fail(t);
      const { schemaCode: r } = this;
      this.fail((0, s._)`${r} !== undefined && (${(0, s.or)(this.invalid$data(), t)})`);
    }
    error(t, r, a) {
      if (r) {
        this.setParams(r), this._error(t, a), this.setParams({});
        return;
      }
      this._error(t, a);
    }
    _error(t, r) {
      (t ? y.reportExtraError : y.reportError)(this, this.def.error, r);
    }
    $dataError() {
      (0, y.reportError)(this, this.def.$dataError || y.keyword$DataError);
    }
    reset() {
      if (this.errsCount === void 0)
        throw new Error('add "trackErrors" to keyword definition');
      (0, y.resetErrorsCount)(this.gen, this.errsCount);
    }
    ok(t) {
      this.allErrors || this.gen.if(t);
    }
    setParams(t, r) {
      r ? Object.assign(this.params, t) : this.params = t;
    }
    block$data(t, r, a = s.nil) {
      this.gen.block(() => {
        this.check$data(t, a), r();
      });
    }
    check$data(t = s.nil, r = s.nil) {
      if (!this.$data)
        return;
      const { gen: a, schemaCode: n, schemaType: i, def: c } = this;
      a.if((0, s.or)((0, s._)`${n} === undefined`, r)), t !== s.nil && a.assign(t, !0), (i.length || c.validateSchema) && (a.elseIf(this.invalid$data()), this.$dataError(), t !== s.nil && a.assign(t, !1)), a.else();
    }
    invalid$data() {
      const { gen: t, schemaCode: r, schemaType: a, def: n, it: i } = this;
      return (0, s.or)(c(), p());
      function c() {
        if (a.length) {
          if (!(r instanceof s.Name))
            throw new Error("ajv implementation error");
          const f = Array.isArray(a) ? a : [a];
          return (0, s._)`${(0, _.checkDataTypes)(f, r, i.opts.strictNumbers, _.DataType.Wrong)}`;
        }
        return s.nil;
      }
      function p() {
        if (n.validateSchema) {
          const f = t.scopeValue("validate$data", { ref: n.validateSchema });
          return (0, s._)`!${f}(${r})`;
        }
        return s.nil;
      }
    }
    subschema(t, r) {
      const a = (0, E.getSubschema)(this.it, t);
      (0, E.extendSubschemaData)(a, this.it, t), (0, E.extendSubschemaMode)(a, t);
      const n = { ...this.it, ...a, items: void 0, props: void 0 };
      return L(n, r), n;
    }
    mergeEvaluated(t, r) {
      const { it: a, gen: n } = this;
      a.opts.unevaluated && (a.props !== !0 && t.props !== void 0 && (a.props = d.mergeEvaluated.props(n, t.props, a.props, r)), a.items !== !0 && t.items !== void 0 && (a.items = d.mergeEvaluated.items(n, t.items, a.items, r)));
    }
    mergeValidEvaluated(t, r) {
      const { it: a, gen: n } = this;
      if (a.opts.unevaluated && (a.props !== !0 || a.items !== !0))
        return n.if(r, () => this.mergeEvaluated(t, s.Name)), !0;
    }
  }
  h.KeywordCxt = K;
  function N(e, t, r, a) {
    const n = new K(e, r, t);
    "code" in r ? r.code(n, a) : n.$data && r.validate ? (0, $.funcKeywordCode)(n, r) : "macro" in r ? (0, $.macroKeywordCode)(n, r) : (r.compile || r.validate) && (0, $.funcKeywordCode)(n, r);
  }
  const ie = /^\/(?:[^~]|~0|~1)*$/, ce = /^([0-9]+)(#|\/(?:[^~]|~0|~1)*)?$/;
  function U(e, { dataLevel: t, dataNames: r, dataPathArr: a }) {
    let n, i;
    if (e === "")
      return o.default.rootData;
    if (e[0] === "/") {
      if (!ie.test(e))
        throw new Error(`Invalid JSON-pointer: ${e}`);
      n = e, i = o.default.rootData;
    } else {
      const l = ce.exec(e);
      if (!l)
        throw new Error(`Invalid JSON-pointer: ${e}`);
      const u = +l[1];
      if (n = l[2], n === "#") {
        if (u >= t)
          throw new Error(f("property/index", u));
        return a[t - u];
      }
      if (u > t)
        throw new Error(f("data", u));
      if (i = r[t - u], !n)
        return i;
    }
    let c = i;
    const p = n.split("/");
    for (const l of p)
      l && (i = (0, s._)`${i}${(0, s.getProperty)((0, d.unescapeJsonPointer)(l))}`, c = (0, s._)`${c} && ${i}`);
    return c;
    function f(l, u) {
      return `Cannot access ${l} ${u} levels up, current level is ${t}`;
    }
  }
  return h.getData = U, h;
}
export {
  xe as __require
};
//# sourceMappingURL=index.js.map
