import { REGEX as a } from "./index-regex.js";
import { isValidHexColor as z } from "./index-hex-color.js";
import { translationService as G } from "./index-translation.service.js";
import { extractSetErrorsFromConfig as Y } from "./index-extract-set-errors-from-config.js";
import { ErrorsKeys as u } from "./index-validate-input-errors.js";
import { isValidPhoneNumber as K } from "./index-isValidPhoneNumber.js";
const f = (e) => e != null, $ = (e, t = !1) => {
  if (!e || !e.validation || !e.getValue || !e.setErrors) return !0;
  const r = e.getValue(), l = Y(e);
  if (l)
    return e.errors = [], s(e, !1, l, t);
  if (e.validation.required && !M(r))
    return s(e, !1, u.REQUIRED, t);
  if (e.fieldType === "InputNumber") {
    const {
      gte: N,
      lte: _,
      lt: p,
      gt: n,
      isValid: R
    } = e.validation;
    let {
      notIn: A
    } = e.validation;
    if (R && isNaN(parseInt(r)))
      return s(e, !1, u.NR_IS_VALID, t);
    if (f(N) && !I(r, N, "gte"))
      return s(e, !1, u.NR_GTE, t);
    if (f(_) && !I(r, _, "lte"))
      return s(e, !1, u.NR_LTE, t);
    if (f(n) && !I(r, n, "gt"))
      return s(e, !1, u.NR_GT, t);
    if (f(p) && !I(r, p, "lt"))
      return s(e, !1, u.NR_LT, t);
    if (A && (u.NOT_IN_ARRAY, Array.isArray(A) || (A == null || A.customErrorMessage, A = A == null ? void 0 : A.value), A.length && A.find((i) => i === r)))
      return s(e, !1, u.NOT_IN_ARRAY, t);
  }
  if (e.fieldType === "Input" || e.fieldType === "Textarea" || e.fieldType === "Autocomplete") {
    const {
      minLength: N,
      maxLength: _,
      regex: p,
      urlNotAllowed: n,
      urlFormat: R,
      specialCharsNotAllowed: A,
      multipleSpacesNotAllowed: i,
      pathFormat: S,
      fullCapitalizationNotAllowed: O,
      excessiveCapitalisationNotAllowed: w,
      pathNotAllowed: x,
      domain: d,
      rootDomain: c,
      isPassword: W,
      onlySpacesNotAllowed: H,
      exclamationMarksNotAllowed: F,
      fullStopMarkNotAllowed: V,
      textAfterCommaOrPeriodNotAllowed: b
    } = e.validation;
    let {
      notIn: E
    } = e.validation;
    if (N && r && r.length < N)
      return s(e, !1, u.MIN_LEN, t);
    if (_ && r && r.length > _)
      return s(e, !1, u.MAX_LEN, t);
    if (r && p && !p.test(r))
      return s(e, !1, u.NO_REGEX_MATCH, t);
    if (r && n && r && r.search(a.url) >= 0)
      return s(e, !1, u.URL_NOT_ALLOWED, t);
    if (r && R && r.search(a.url) === -1)
      return s(e, !1, u.URL_FORMAT, t);
    if (r && S && r && r.search(a.invalidPathChars) >= 0)
      return s(e, !1, u.PATH_FORMAT, t);
    if (r && A && r && r.search(a.specialChars) >= 0)
      return s(e, !1, u.SPECIAL_CHARS_NOT_ALLOWED, t);
    if (r && i && r && r.search(a.multipleSpaces) >= 0)
      return s(e, !1, u.MULTIPLE_SPACES_NOT_ALLOWED, t);
    if (r && H && r && r.search(a.onlySpaces) >= 0)
      return s(e, !1, u.ONLY_SPACES_NOT_ALLOWED, t);
    if (r && F && r && r.search(a.exclamationMark) >= 0)
      return s(e, !1, u.EXCLAMATION_MARKS_NOT_ALLOWED, t);
    if (r && V && r && r.search(a.fullStopMark) >= 0)
      return s(e, !1, u.FULL_STOP_MARKS_NOT_ALLOWED, t);
    if (r && b && r && a.textAfterCommaOrPeriod.test(r))
      return s(e, !1, u.TEXT_AFTER_COMMA_OR_PERIOD_NOT_ALLOWED, t);
    if (r && O && r && r.search(a.fullCapitalization) >= 0)
      return s(e, !1, u.FULL_CAPITALIZATION_NOT_ALLOWED, t);
    if (r && w && r && r.search(a.excessiveCapitalisation) >= 0)
      return s(e, !1, u.EXCESSIVE_CAPITALIZATION_NOT_ALLOWED, t);
    if (r && x && r && r.search(a.path) >= 0)
      return s(e, !1, u.PATH_NOT_ALLOWED, t);
    if (d && r && r.search(a.at) >= 0) {
      const L = r.substring(r.indexOf("@") + 1, r.length);
      return L === d ? s(e, !1, u.AT_DOMAIN_NOT_ALLOWED, t) : L.length > 0 ? s(e, !1, u.WRONG_DOMAIN, t) : s(e, !1, u.AT_NOT_ALLOWED, t);
    }
    if (c) {
      const L = r.match(a.domain);
      let T = "", h;
      if (typeof c == "string" ? T = c : (T = c.value, h = G.get(c.customErrorMessage, {
        rootDomain: T
      }).value), L && L[1]) {
        const D = L[1], X = D.startsWith("www.") ? D.slice(4) : D, U = T.startsWith("www.") ? T.slice(4) : T;
        if (X !== U)
          return s(e, !1, h ?? u.ROOT_DOMAIN_INVALID, t);
      } else
        return s(e, !1, h ?? u.ROOT_DOMAIN_INVALID, t);
    }
    if (W) {
      if (!M(r))
        return s(e, !1, u.REQUIRED, t);
      if (r && r.length < 4)
        return s(e, !1, u.PASSWORD_MIN_LEN, t);
      if (r && r.length > 100)
        return s(e, !1, u.PASSWORD_MAX_LEN, t);
    }
    if (E) {
      let L = u.NOT_IN_ARRAY;
      if (Array.isArray(E) || (L = E == null ? void 0 : E.customErrorMessage, E = E == null ? void 0 : E.value), E.length && E.find((T) => T === r))
        return s(e, !1, L, t);
    }
  }
  if (e.fieldType === "Input" || e.fieldType === "InputBase" || e.fieldType === "ColorPicker") {
    const {
      validatePhone: N,
      validatePhoneNumberForCountry: _,
      validateEmail: p,
      validateHexColor: n
    } = e.validation;
    if (r && N && !a.phone.test(r))
      return s(e, !1, u.INVALID_PHONE, t);
    if (r && _ && !K(r, _))
      return s(e, !1, u.INCORRECT_PHONE, t);
    if (r && p && !a.email.test(r))
      return s(e, !1, u.INVALID_EMAIL, t);
    if (r && n && !z(r))
      return s(e, !1, u.INVALID_HEX_COLOR, t);
  }
  if (e.fieldType === "Autocomplete") {
    const {
      minCount: N,
      maxCount: _
    } = e.validation;
    if (N && Array.isArray(r) && r.length < N || _ && Array.isArray(r) && r.length > _)
      return s(e, !1, void 0, t);
  }
  return e.setErrors([]), s(e, !0, void 0, t);
}, M = (e) => !(!e || (Array.isArray(e) || typeof e == "string") && e.length === 0), I = (e, t, r) => {
  if (typeof t > "u" || (t = Number(t), t == null)) return !0;
  let l = null;
  if ((typeof e == "number" || typeof e == "string") && (l = Number(e)), l == null || isNaN(l)) return !0;
  switch (r) {
    case "gt":
      return l > t;
    case "lt":
      return l < t;
    case "gte":
      return l >= t;
    case "lte":
      return l <= t;
  }
}, s = (e, t, r, l = !1) => t ? (e.hasError = !1, e.setHasError && e.setHasError(!1), !0) : (l || (e.hasError = !0, e.setHasError && e.setHasError(!0), e.setErrors && r && (!e.errors || e.errors.indexOf(r) < 0) && e.setErrors([r])), !1);
export {
  s as setFieldValidity,
  $ as validInput
};
