import { __exports as i } from "../../../../../../../../_virtual/dataType.js";
import { __require as S } from "../rules.js";
import { __require as I } from "./applicability.js";
import { __require as O } from "../errors.js";
import { __require as j } from "../codegen/index.js";
import { __require as J } from "../util.js";
var h;
function H() {
  if (h) return i;
  h = 1, Object.defineProperty(i, "__esModule", { value: !0 }), i.reportTypeError = i.checkDataTypes = i.checkDataType = i.coerceAndCheckDataType = i.getJSONTypes = i.getSchemaTypes = i.DataType = void 0;
  const T = S(), m = I(), d = O(), t = j(), _ = J();
  var u;
  (function(r) {
    r[r.Correct = 0] = "Correct", r[r.Wrong = 1] = "Wrong";
  })(u || (i.DataType = u = {}));
  function k(r) {
    const n = g(r.type);
    if (n.includes("null")) {
      if (r.nullable === !1)
        throw new Error("type: null contradicts nullable: false");
    } else {
      if (!n.length && r.nullable !== void 0)
        throw new Error('"nullable" cannot be used without "type"');
      r.nullable === !0 && n.push("null");
    }
    return n;
  }
  i.getSchemaTypes = k;
  function g(r) {
    const n = Array.isArray(r) ? r : r ? [r] : [];
    if (n.every(T.isJSONType))
      return n;
    throw new Error("type must be JSONType or JSONType[]: " + n.join(","));
  }
  i.getJSONTypes = g;
  function E(r, n) {
    const { gen: l, data: o, opts: e } = r, s = C(n, e.coerceTypes), a = n.length > 0 && !(s.length === 0 && n.length === 1 && (0, m.schemaHasRulesForType)(r, n[0]));
    if (a) {
      const c = y(n, o, e.strictNumbers, u.Wrong);
      l.if(c, () => {
        s.length ? q(r, n, s) : $(r);
      });
    }
    return a;
  }
  i.coerceAndCheckDataType = E;
  const b = /* @__PURE__ */ new Set(["string", "number", "integer", "boolean", "null"]);
  function C(r, n) {
    return n ? r.filter((l) => b.has(l) || n === "array" && l === "array") : [];
  }
  function q(r, n, l) {
    const { gen: o, data: e, opts: s } = r, a = o.let("dataType", (0, t._)`typeof ${e}`), c = o.let("coerced", (0, t._)`undefined`);
    s.coerceTypes === "array" && o.if((0, t._)`${a} == 'object' && Array.isArray(${e}) && ${e}.length == 1`, () => o.assign(e, (0, t._)`${e}[0]`).assign(a, (0, t._)`typeof ${e}`).if(y(n, e, s.strictNumbers), () => o.assign(c, e))), o.if((0, t._)`${c} !== undefined`);
    for (const f of l)
      (b.has(f) || f === "array" && s.coerceTypes === "array") && N(f);
    o.else(), $(r), o.endIf(), o.if((0, t._)`${c} !== undefined`, () => {
      o.assign(e, c), w(r, c);
    });
    function N(f) {
      switch (f) {
        case "string":
          o.elseIf((0, t._)`${a} == "number" || ${a} == "boolean"`).assign(c, (0, t._)`"" + ${e}`).elseIf((0, t._)`${e} === null`).assign(c, (0, t._)`""`);
          return;
        case "number":
          o.elseIf((0, t._)`${a} == "boolean" || ${e} === null
              || (${a} == "string" && ${e} && ${e} == +${e})`).assign(c, (0, t._)`+${e}`);
          return;
        case "integer":
          o.elseIf((0, t._)`${a} === "boolean" || ${e} === null
              || (${a} === "string" && ${e} && ${e} == +${e} && !(${e} % 1))`).assign(c, (0, t._)`+${e}`);
          return;
        case "boolean":
          o.elseIf((0, t._)`${e} === "false" || ${e} === 0 || ${e} === null`).assign(c, !1).elseIf((0, t._)`${e} === "true" || ${e} === 1`).assign(c, !0);
          return;
        case "null":
          o.elseIf((0, t._)`${e} === "" || ${e} === 0 || ${e} === false`), o.assign(c, null);
          return;
        case "array":
          o.elseIf((0, t._)`${a} === "string" || ${a} === "number"
              || ${a} === "boolean" || ${e} === null`).assign(c, (0, t._)`[${e}]`);
      }
    }
  }
  function w({ gen: r, parentData: n, parentDataProperty: l }, o) {
    r.if((0, t._)`${n} !== undefined`, () => r.assign((0, t._)`${n}[${l}]`, o));
  }
  function p(r, n, l, o = u.Correct) {
    const e = o === u.Correct ? t.operators.EQ : t.operators.NEQ;
    let s;
    switch (r) {
      case "null":
        return (0, t._)`${n} ${e} null`;
      case "array":
        s = (0, t._)`Array.isArray(${n})`;
        break;
      case "object":
        s = (0, t._)`${n} && typeof ${n} == "object" && !Array.isArray(${n})`;
        break;
      case "integer":
        s = a((0, t._)`!(${n} % 1) && !isNaN(${n})`);
        break;
      case "number":
        s = a();
        break;
      default:
        return (0, t._)`typeof ${n} ${e} ${r}`;
    }
    return o === u.Correct ? s : (0, t.not)(s);
    function a(c = t.nil) {
      return (0, t.and)((0, t._)`typeof ${n} == "number"`, c, l ? (0, t._)`isFinite(${n})` : t.nil);
    }
  }
  i.checkDataType = p;
  function y(r, n, l, o) {
    if (r.length === 1)
      return p(r[0], n, l, o);
    let e;
    const s = (0, _.toHash)(r);
    if (s.array && s.object) {
      const a = (0, t._)`typeof ${n} != "object"`;
      e = s.null ? a : (0, t._)`!${n} || ${a}`, delete s.null, delete s.array, delete s.object;
    } else
      e = t.nil;
    s.number && delete s.integer;
    for (const a in s)
      e = (0, t.and)(e, p(a, n, l, o));
    return e;
  }
  i.checkDataTypes = y;
  const A = {
    message: ({ schema: r }) => `must be ${r}`,
    params: ({ schema: r, schemaValue: n }) => typeof r == "string" ? (0, t._)`{type: ${r}}` : (0, t._)`{type: ${n}}`
  };
  function $(r) {
    const n = D(r);
    (0, d.reportError)(n, A);
  }
  i.reportTypeError = $;
  function D(r) {
    const { gen: n, data: l, schema: o } = r, e = (0, _.schemaRefOrVal)(r, o, "type");
    return {
      gen: n,
      keyword: "type",
      data: l,
      schema: o.type,
      schemaCode: e,
      schemaValue: e,
      parentSchema: o,
      params: {},
      it: r
    };
  }
  return i;
}
export {
  H as __require
};
//# sourceMappingURL=dataType.js.map
