import { __exports as p } from "../../../../../../../../_virtual/index17.js";
import { __require as k } from "../../compile/codegen/index.js";
import { __require as I } from "./types.js";
import { __require as C } from "../../compile/index.js";
import { __require as N } from "../../compile/ref_error.js";
import { __require as A } from "../../compile/util.js";
var b;
function F() {
  if (b) return p;
  b = 1, Object.defineProperty(p, "__esModule", { value: !0 });
  const c = k(), m = I(), h = C(), R = N(), M = A(), O = {
    keyword: "discriminator",
    type: "object",
    schemaType: "object",
    error: {
      message: ({ params: { discrError: i, tagName: o } }) => i === m.DiscrError.Tag ? `tag "${o}" must be string` : `value of tag "${o}" must be in oneOf`,
      params: ({ params: { discrError: i, tag: o, tagName: d } }) => (0, c._)`{error: ${i}, tag: ${d}, tagValue: ${o}}`
    },
    code(i) {
      const { gen: o, data: d, schema: v, parentSchema: w, it: a } = i, { oneOf: l } = w;
      if (!a.opts.discriminator)
        throw new Error("discriminator: requires discriminator option");
      const t = v.propertyName;
      if (typeof t != "string")
        throw new Error("discriminator: requires propertyName");
      if (v.mapping)
        throw new Error("discriminator: mapping is not supported");
      if (!l)
        throw new Error("discriminator: requires oneOf keyword");
      const q = o.let("valid", !1), f = o.const("tag", (0, c._)`${d}${(0, c.getProperty)(t)}`);
      o.if((0, c._)`typeof ${f} == "string"`, () => D(), () => i.error(!1, { discrError: m.DiscrError.Tag, tag: f, tagName: t })), i.ok(q);
      function D() {
        const n = T();
        o.if(!1);
        for (const s in n)
          o.elseIf((0, c._)`${f} === ${s}`), o.assign(q, S(n[s]));
        o.else(), i.error(!1, { discrError: m.DiscrError.Mapping, tag: f, tagName: t }), o.endIf();
      }
      function S(n) {
        const s = o.name("valid"), g = i.subschema({ keyword: "oneOf", schemaProp: n }, s);
        return i.mergeEvaluated(g, c.Name), s;
      }
      function T() {
        var n;
        const s = {}, g = y(w);
        let _ = !0;
        for (let e = 0; e < l.length; e++) {
          let r = l[e];
          if (r?.$ref && !(0, M.schemaHasRulesButRef)(r, a.self.RULES)) {
            const $ = r.$ref;
            if (r = h.resolveRef.call(a.self, a.schemaEnv.root, a.baseId, $), r instanceof h.SchemaEnv && (r = r.schema), r === void 0)
              throw new R.default(a.opts.uriResolver, a.baseId, $);
          }
          const u = (n = r?.properties) === null || n === void 0 ? void 0 : n[t];
          if (typeof u != "object")
            throw new Error(`discriminator: oneOf subschemas (or referenced schemas) must have "properties/${t}"`);
          _ = _ && (g || y(r)), j(u, e);
        }
        if (!_)
          throw new Error(`discriminator: "${t}" must be required`);
        return s;
        function y({ required: e }) {
          return Array.isArray(e) && e.includes(t);
        }
        function j(e, r) {
          if (e.const)
            E(e.const, r);
          else if (e.enum)
            for (const u of e.enum)
              E(u, r);
          else
            throw new Error(`discriminator: "properties/${t}" must have "const" or "enum"`);
        }
        function E(e, r) {
          if (typeof e != "string" || e in s)
            throw new Error(`discriminator: "${t}" values must be unique strings`);
          s[e] = r;
        }
      }
    }
  };
  return p.default = O, p;
}
export {
  F as __require
};
//# sourceMappingURL=index.js.map
