import { __exports as l } from "../../../../../../../../_virtual/keyword.js";
import { __require as V } from "../codegen/index.js";
import { __require as O } from "../names.js";
import { __require as T } from "../../vocabularies/code.js";
import { __require as U } from "../errors.js";
var $;
function G() {
  if ($) return l;
  $ = 1, Object.defineProperty(l, "__esModule", { value: !0 }), l.validateKeywordUsage = l.validSchemaType = l.funcKeywordCode = l.macroKeywordCode = void 0;
  const n = V(), y = O(), E = T(), S = U();
  function K(r, e) {
    const { gen: o, keyword: a, schema: t, parentSchema: d, it: i } = r, u = e.macro.call(i.self, t, d, i), c = w(o, a, u);
    i.opts.validateSchema !== !1 && i.self.validateSchema(u, !0);
    const v = o.name("valid");
    r.subschema({
      schema: u,
      schemaPath: n.nil,
      errSchemaPath: `${i.errSchemaPath}/${a}`,
      topSchemaRef: c,
      compositeRule: !0
    }, v), r.pass(v, () => r.error(!0));
  }
  l.macroKeywordCode = K;
  function q(r, e) {
    var o;
    const { gen: a, keyword: t, schema: d, parentSchema: i, $data: u, it: c } = r;
    A(c, e);
    const v = !u && e.compile ? e.compile.call(c.self, d, i, c) : e.validate, _ = w(a, t, v), m = a.let("valid");
    r.block$data(m, j), r.ok((o = e.valid) !== null && o !== void 0 ? o : m);
    function j() {
      if (e.errors === !1)
        p(), e.modifying && h(r), g(() => r.error());
      else {
        const s = e.async ? P() : R();
        e.modifying && h(r), g(() => C(r, s));
      }
    }
    function P() {
      const s = a.let("ruleErrs", null);
      return a.try(() => p((0, n._)`await `), (f) => a.assign(m, !1).if((0, n._)`${f} instanceof ${c.ValidationError}`, () => a.assign(s, (0, n._)`${f}.errors`), () => a.throw(f))), s;
    }
    function R() {
      const s = (0, n._)`${_}.errors`;
      return a.assign(s, null), p(n.nil), s;
    }
    function p(s = e.async ? (0, n._)`await ` : n.nil) {
      const f = c.opts.passContext ? y.default.this : y.default.self, D = !("compile" in e && !u || e.schema === !1);
      a.assign(m, (0, n._)`${s}${(0, E.callValidateCode)(r, _, f, D)}`, e.modifying);
    }
    function g(s) {
      var f;
      a.if((0, n.not)((f = e.valid) !== null && f !== void 0 ? f : m), s);
    }
  }
  l.funcKeywordCode = q;
  function h(r) {
    const { gen: e, data: o, it: a } = r;
    e.if(a.parentData, () => e.assign(o, (0, n._)`${a.parentData}[${a.parentDataProperty}]`));
  }
  function C(r, e) {
    const { gen: o } = r;
    o.if((0, n._)`Array.isArray(${e})`, () => {
      o.assign(y.default.vErrors, (0, n._)`${y.default.vErrors} === null ? ${e} : ${y.default.vErrors}.concat(${e})`).assign(y.default.errors, (0, n._)`${y.default.vErrors}.length`), (0, S.extendErrors)(r);
    }, () => r.error());
  }
  function A({ schemaEnv: r }, e) {
    if (e.async && !r.$async)
      throw new Error("async keyword in sync schema");
  }
  function w(r, e, o) {
    if (o === void 0)
      throw new Error(`keyword "${e}" failed to compile`);
    return r.scopeValue("keyword", typeof o == "function" ? { ref: o } : { ref: o, code: (0, n.stringify)(o) });
  }
  function k(r, e, o = !1) {
    return !e.length || e.some((a) => a === "array" ? Array.isArray(r) : a === "object" ? r && typeof r == "object" && !Array.isArray(r) : typeof r == a || o && typeof r > "u");
  }
  l.validSchemaType = k;
  function b({ schema: r, opts: e, self: o, errSchemaPath: a }, t, d) {
    if (Array.isArray(t.keyword) ? !t.keyword.includes(d) : t.keyword !== d)
      throw new Error("ajv implementation error");
    const i = t.dependencies;
    if (i?.some((u) => !Object.prototype.hasOwnProperty.call(r, u)))
      throw new Error(`parent schema must have dependencies of ${d}: ${i.join(",")}`);
    if (t.validateSchema && !t.validateSchema(r[d])) {
      const c = `keyword "${d}" value is invalid at path "${a}": ` + o.errorsText(t.validateSchema.errors);
      if (e.validateSchema === "log")
        o.logger.error(c);
      else
        throw new Error(c);
    }
  }
  return l.validateKeywordUsage = b, l;
}
export {
  G as __require
};
//# sourceMappingURL=keyword.js.map
