import * as d from "yup";
import { DYNAMIC_FORM_DICTIONARY as c } from "../../../../dictionary.js";
import { resolveFieldMandatory as _, resolveFieldHelperMessage as A } from "../../../resolveFieldContractAliases.js";
import { resolveValidationErrorMessage as u } from "../../../resolveValidationErrorMessage/resolveValidationErrorMessage.js";
import { applyRequiredValidation as M } from "../../../applyRequiredValidation/applyRequiredValidation.js";
const p = {
  validationTypeId: "fvl_required",
  /**
   * Applies required validation when the field is required; otherwise returns the schema unchanged.
   * @param ctx - Context with field, getLabel, baseSchema, schema, and validation
   * @returns Schema with required validation applied, or unchanged schema
   */
  apply(r) {
    const { field: e, getLabel: a, baseSchema: i, schema: t, validation: l } = r;
    if (!_(e))
      return t;
    const o = e.fieldType.componentId === "RHFDateRangePicker", s = A(e), m = o && s ? s : a(o ? c.DATE_RANGE_REQUIRED_MESSAGE : c.IS_REQUIRED_MESSAGE), h = u(l, a, m);
    return M(e, t, i, h);
  }
}, v = {
  validationTypeId: "fvl_max_length",
  /**
   * Applies max length constraint to string schema when validation value is present.
   * @param ctx - Context with field, getLabel, baseSchema, schema, and validation
   * @returns Schema with `.max(length)` applied, or unchanged schema if not applicable
   */
  apply(r) {
    const { validation: e, getLabel: a, baseSchema: i, schema: t } = r;
    if (!(e.validationType || e.type)?.requiresValue || !e.value || !(i instanceof d.StringSchema))
      return t;
    const n = parseInt(e.value, 10);
    if (isNaN(n))
      return t;
    const o = u(
      e,
      a,
      a(c.MAX_LENGTH_MESSAGE, n)
    );
    return t.max(n, o);
  }
}, S = {
  validationTypeId: "fvl_min_length",
  /**
   * Applies min length constraint to string schema when validation value is present.
   * @param ctx - Context with field, getLabel, baseSchema, schema, and validation
   * @returns Schema with `.min(length)` applied, or unchanged schema if not applicable
   */
  apply(r) {
    const { validation: e, getLabel: a, baseSchema: i, schema: t } = r;
    if (!(e.validationType || e.type)?.requiresValue || !e.value || !(i instanceof d.StringSchema))
      return t;
    const n = parseInt(e.value, 10);
    if (isNaN(n))
      return t;
    const o = u(
      e,
      a,
      a(c.MIN_LENGTH_MESSAGE, n)
    );
    return t.min(n, o);
  }
}, y = {
  validationTypeId: "fvl_max",
  /**
   * Applies maximum numeric value constraint when validation value is present.
   * @param ctx - Context with field, getLabel, baseSchema, schema, and validation
   * @returns Schema with `.max(value)` applied, or unchanged schema if not applicable
   */
  apply(r) {
    const { validation: e, getLabel: a, baseSchema: i, schema: t } = r;
    if (!(e.validationType || e.type)?.requiresValue || !e.value || !(i instanceof d.NumberSchema))
      return t;
    const n = parseFloat(e.value);
    if (isNaN(n))
      return t;
    const o = u(
      e,
      a,
      a(c.MAX_VALUE_MESSAGE, n)
    );
    return t.max(n, o);
  }
}, g = {
  validationTypeId: "fvl_min",
  /**
   * Applies minimum numeric value constraint when validation value is present.
   * @param ctx - Context with field, getLabel, baseSchema, schema, and validation
   * @returns Schema with `.min(value)` applied, or unchanged schema if not applicable
   */
  apply(r) {
    const { validation: e, getLabel: a, baseSchema: i, schema: t } = r;
    if (!(e.validationType || e.type)?.requiresValue || !e.value || !(i instanceof d.NumberSchema))
      return t;
    const n = parseFloat(e.value);
    if (isNaN(n))
      return t;
    const o = u(
      e,
      a,
      a(c.MIN_VALUE_MESSAGE, n)
    );
    return t.min(n, o);
  }
}, f = {
  validationTypeId: "fvl_email",
  /**
   * Applies email format validation to string schema.
   * @param ctx - Context with field, getLabel, baseSchema, schema, and validation
   * @returns Schema with `.email()` applied, or unchanged schema if base is not string
   */
  apply(r) {
    const { validation: e, getLabel: a, baseSchema: i, schema: t } = r;
    if (!(i instanceof d.StringSchema))
      return t;
    const l = u(
      e,
      a,
      a(c.EMAIL_INVALID_MESSAGE)
    );
    return t.email(l);
  }
}, I = {
  validationTypeId: "fvl_url",
  /**
   * Applies URL format validation to string schema.
   * @param ctx - Context with field, getLabel, baseSchema, schema, and validation
   * @returns Schema with `.url()` applied, or unchanged schema if base is not string
   */
  apply(r) {
    const { validation: e, getLabel: a, baseSchema: i, schema: t } = r;
    if (!(i instanceof d.StringSchema))
      return t;
    const l = u(
      e,
      a,
      a(c.URL_INVALID_MESSAGE)
    );
    return t.url(l);
  }
}, E = {
  validationTypeId: "fvl_pattern",
  /**
   * Applies regex pattern validation to string schema when validation value is present.
   * Invalid regex is caught and schema is returned unchanged (with console warning).
   * @param ctx - Context with field, getLabel, baseSchema, schema, and validation
   * @returns Schema with `.matches(regex)` applied, or unchanged schema if not applicable
   */
  apply(r) {
    const { validation: e, getLabel: a, baseSchema: i, schema: t } = r;
    if (!(e.validationType || e.type)?.requiresValue || !e.value || !(i instanceof d.StringSchema))
      return t;
    try {
      const n = new RegExp(e.value), o = u(
        e,
        a,
        a(c.PATTERN_INVALID_MESSAGE)
      );
      return t.matches(n, o);
    } catch (n) {
      return console.warn(`Invalid regex pattern: ${e.value}`, n), t;
    }
  }
}, T = {
  validationTypeId: "fvl_custom",
  /**
   * Applies custom validation: for string, regex or exact match; for number, exact value match.
   * When no value is configured, string schema accepts any non-empty value.
   * @param ctx - Context with field, getLabel, baseSchema, schema, and validation
   * @returns Schema with custom `.test()` applied, or unchanged schema if base type not supported
   */
  apply(r) {
    const { validation: e, getLabel: a, baseSchema: i, schema: t } = r, n = (e.validationType || e.type)?.requiresValue && e.value;
    if (i instanceof d.StringSchema) {
      const o = u(
        e,
        a,
        a(c.CUSTOM_VALIDATION_MESSAGE)
      );
      return n ? t.test(
        "custom",
        o,
        (s) => {
          if (!s)
            return !0;
          try {
            return new RegExp(e.value).test(s);
          } catch {
            return s === e.value;
          }
        }
      ) : t.test(
        "custom",
        o,
        (s) => !s || !0
      );
    }
    if (i instanceof d.NumberSchema && n) {
      const o = u(
        e,
        a,
        a(c.CUSTOM_VALIDATION_MESSAGE)
      );
      return t.test(
        "custom",
        o,
        (s) => {
          if (s == null)
            return !0;
          const m = parseFloat(e.value);
          return isNaN(m) ? !0 : s === m;
        }
      );
    }
    return t;
  }
}, R = {
  [p.validationTypeId]: p,
  [v.validationTypeId]: v,
  [S.validationTypeId]: S,
  [y.validationTypeId]: y,
  [g.validationTypeId]: g,
  [f.validationTypeId]: f,
  [I.validationTypeId]: I,
  [E.validationTypeId]: E,
  [T.validationTypeId]: T
};
export {
  R as VALIDATION_STRATEGY_REGISTRY,
  T as customValidationStrategy,
  f as emailValidationStrategy,
  v as maxLengthValidationStrategy,
  y as maxValidationStrategy,
  S as minLengthValidationStrategy,
  g as minValidationStrategy,
  E as patternValidationStrategy,
  p as requiredValidationStrategy,
  I as urlValidationStrategy
};
