import * as p from "yup";
import { DATE_TIME_OPERATORS as c } from "../../../constants.js";
import { DYNAMIC_FILTER_DICTIONARY_ID as f, DICCTIONARY as d } from "../../../dictionary.js";
import { DateTimeFilter as D } from "./index.js";
import { startOfDay as _, endOfDay as y, differenceInDays as V } from "date-fns";
import { isValidDate as i } from "../../../../../utils/isValidDate.js";
import { createValidationSchema as F, createValidationFullObject as T, createValidationPartialObject as b } from "../../../../hook-form/RHFormProvider/schema.js";
class v {
  getComponent() {
    return D;
  }
  /**
   * getDefaultFilter devuelve un filtro por defecto para un campo de fecha y hora.
   */
  getDefaultFilter(e, o) {
    const a = /* @__PURE__ */ new Date(), r = /* @__PURE__ */ new Date();
    a.setHours(0, 0, 0, 0), r.setHours(23, 59, 59, 999);
    const t = e.defaultOperand1 && e.defaultOperand1 instanceof Date ? e.defaultOperand1 : a, n = e.defaultOperand2 && e.defaultOperand2 instanceof Date ? e.defaultOperand2 : r;
    let u = !1;
    return t !== void 0 && (u = !0), {
      id: 0,
      fieldType: "datetime",
      field: e,
      fixed: o,
      isSet: u,
      operator: "b",
      operand1: t,
      operand2: n
    };
  }
  /**
   * getFormValue convierte un valor de filtro en un valor de formulario.
   */
  getFormValue(e, o, a) {
    const r = a, t = r ? r.operator : e.defaultOperator ?? "b", n = _(/* @__PURE__ */ new Date()), u = y(/* @__PURE__ */ new Date()), s = e.defaultOperand1 && e.defaultOperand1 instanceof Date ? e.defaultOperand1 : n, l = e.defaultOperand2 && e.defaultOperand2 instanceof Date ? e.defaultOperand2 : u, m = r ? r.operand1 : s, O = r && r.operator === "b" ? r.operand2 : l;
    return {
      fieldType: "datetime",
      formValueOperator: {
        id: t,
        label: o(`${f}.operator_${t}`)
      },
      formValueOperand1: m,
      formValueOperand2: O
    };
  }
  /**
   * getFilterFromFormValue convierte un valor de formulario en un valor de filtro.
   */
  getFilterFromFormValue(e) {
    const o = e.formValueOperator.id, a = e.formValueOperand1;
    if (o === "b") {
      const r = e.formValueOperand2;
      return {
        fieldType: "datetime",
        operator: o,
        operand1: a,
        operand2: r
      };
    } else
      return {
        fieldType: "datetime",
        operator: o,
        operand1: a
      };
  }
  /**
   * getlabels devuelve las etiquetas de un filtro de fecha y hora.
   */
  getLabels(e, o, a, r) {
    const { dateFormatter: t } = a;
    let n;
    return e.operator === "b" ? n = e.operand1 && e.operand2 ? `${t.formatDate(
      e.operand1,
      t.datetimeFormat
    )} - ${t.formatDate(
      e.operand2,
      t.datetimeFormat
    )}` : "" : n = e.operand1 ? t.formatDate(
      e.operand1,
      t.datetimeFormat
    ) : "", {
      labelField: e.field.label ?? o(e.field.dictionaryId),
      labelOperator: o(
        `${f}.operator_${e.operator}`
      ),
      labelOperands: n
    };
  }
  /**
   * getschema devuelve el esquema de validación de Yup para un filtro de fecha y hora.
   */
  getSchema(e, o) {
    const a = o?.maxDays;
    return F(T(b({
      formValueOperator: p.object().nullable().required(e(d.error_operator_required)),
      formValueOperand1: p.date().required(e(d.error_invalid_date)).typeError(e(d.error_invalid_date)),
      formValueOperand2: p.mixed().when(["formValueOperator.id"], {
        is: "b",
        then: p.date().typeError(e(d.error_invalid_date)).min(
          p.ref("formValueOperand1"),
          e(d.error_invalid_date)
        ).test(
          "max-days",
          `${e(d.error_max_days_exceeded)} (Máx. ${a})`,
          function(r) {
            if (!a || !r)
              return !0;
            const t = this.parent.formValueOperand1;
            return t ? V(r, t) <= a : !0;
          }
        )
      })
    })));
  }
  /**
   * verifyFilter verifica si un filtro de fecha y hora es válido.
   */
  verifyFilter(e, o) {
    return !(typeof e.operator != "string" || c.findIndex((a) => a === e.operator) === -1 || typeof e.operand1 != "string" || !i(new Date(e.operand1)) || e.operator === "b" && (typeof e.operand2 != "string" || !i(new Date(e.operand1))));
  }
}
export {
  v as DateTimeFilterHelpers
};
