import * as p from "yup";
import { NUMBER_OPERATORS as m } from "../../../constants.js";
import { NumberFilter as f } from "./index.js";
import { DYNAMIC_FILTER_DICTIONARY_ID as u, DICCTIONARY as d } from "../../../dictionary.js";
import { createValidationSchema as s, createValidationFullObject as i, createValidationPartialObject as O } from "../../../../hook-form/RHFormProvider/schema.js";
class I {
  getComponent() {
    return f;
  }
  /**
   * getDefaultFilter devuelve un filtro por defecto para un campo de número.
   */
  getDefaultFilter(e, o) {
    const a = e, r = a.defaultOperator ?? "e", n = a.defaultOperand1 ?? null, l = a.defaultOperand2 ?? null;
    let t = !1;
    return ["b"].includes(r) ? t = n !== null && l !== null : t = n !== null, {
      id: 0,
      fieldType: "number",
      field: a,
      fixed: o,
      operator: r,
      operand1: n,
      operand2: l,
      isSet: t
    };
  }
  /**
   * getFormValue convierte un valor de filtro en un valor de formulario.
   */
  getFormValue(e, o, a) {
    const r = a, n = r ? r.operator : e.defaultOperator ?? "e", l = r ? r.operand1 : null, t = r && r?.operator === "b" ? r.operand2 : null;
    return {
      fieldType: "number",
      formValueOperator: {
        id: n,
        label: o(`${u}.operator_${n}`)
      },
      formValueOperand1: l,
      formValueOperand2: t
    };
  }
  /**
   * getFilterFromFormValue convierte un valor de formulario en un valor de filtro.
   */
  getFilterFromFormValue(e) {
    const o = e.formValueOperator.id, a = e.formValueOperand1 ? e.formValueOperand1 : 0;
    if (o === "b") {
      const r = e.formValueOperand2 ? e.formValueOperand2 : 0;
      return {
        fieldType: "number",
        operator: o,
        operand1: a,
        operand2: r
      };
    } else
      return {
        fieldType: "number",
        operator: o,
        operand1: a
      };
  }
  /**
   * getlabels devuelve las etiquetas para el filtro.
   */
  getLabels(e, o, a, r) {
    let n;
    return e.operator === "b" ? n = e.operand1 && e.operand2 ? `${e.operand1} - ${e.operand2}` : "" : n = (e.operand1 ?? "?") + "", {
      labelField: e.field.label ?? o(e.field.dictionaryId),
      labelOperator: o(
        `${u}.operator_${e.operator}`
      ),
      labelOperands: n
    };
  }
  /**
   * getSchema devuelve el esquema de validación para el filtro.
   */
  getSchema(e, o) {
    return s(i(O({
      formValueOperator: p.object().nullable().required(e(d.error_operator_required)),
      formValueOperand1: p.number().typeError(
        e(d.error_operand_mustbe_number)
      ),
      formValueOperand2: p.mixed().when(["formValueOperator.id"], {
        is: "b",
        then: p.number().typeError(e(d.error_operand_mustbe_number)).min(
          p.ref("formValueOperand1"),
          e(d.error_less_than_start)
        )
      })
    })));
  }
  /**
   * verifyFilter verifica si el filtro es válido.
   */
  verifyFilter(e, o) {
    return !(typeof e.operator != "string" || m.findIndex((a) => a === e.operator) === -1 || typeof e.operand1 != "number" || e.operator === "b" && typeof e.operand2 != "number");
  }
}
export {
  I as NumberFilterHelpers
};
