import { i18n } from '@plugins/i18n';
import { NumberRuleFunctionType } from './types/main';

export function hittingTheIntervalRule(
  min: number,
  max: number,
  includes = true
): NumberRuleFunctionType {
  return (val: string | number) => {
    return val
      ? (includes
          ? parseInt(String(val)) >= min && parseInt(String(val)) <= max
          : parseInt(String(val)) > min && parseInt(String(val)) < max) ||
          String(
            i18n.t(
              `validation.numbers.hittingTheInterval${
                includes ? 'Includes' : 'Excludes'
              }`,
              {
                min,
                max,
              }
            )
          )
      : !val;
  };
}

export function moreThanRule(
  num: number,
  includes = true
): NumberRuleFunctionType {
  return (val: string | number) => {
    return val
      ? (includes
          ? parseInt(String(val)) >= num
          : parseInt(String(val)) > num) ||
          String(
            i18n.t(
              `validation.numbers.moreThan${
                includes ? 'Includes' : 'Excludes'
              }`,
              {
                num,
              }
            )
          )
      : !val;
  };
}

export function lowerThanRule(
  num: number,
  includes = true
): NumberRuleFunctionType {
  return (val: string | number) => {
    return val
      ? (includes
          ? parseInt(String(val)) <= num
          : parseInt(String(val)) < num) ||
          String(
            i18n.t(
              `validation.numbers.lowerThan${
                includes ? 'Includes' : 'Excludes'
              }`,
              {
                num,
              }
            )
          )
      : !val;
  };
}
