import * as f from "yup";
import { SELECT_OPERATORS_MULTIPLE as A, SELECT_OPERATORS_SINGLE as _ } from "../../../constants.js";
import { SelectFilter as g } from "./index.js";
import { DYNAMIC_FILTER_DICTIONARY_ID as y, DICCTIONARY as m } from "../../../dictionary.js";
import { createValidationSchema as F, createValidationFullObject as S, createValidationPartialObject as T } from "../../../../hook-form/RHFormProvider/schema.js";
function O(c, e) {
  return (Array.isArray(c) ? c : [c])?.filter((r) => typeof r == "string" || typeof r == "number");
}
class M {
  getComponent() {
    return g;
  }
  /**
   * getDefaultFilter devuelve un filtro por defecto para un campo de selección.
   * Cuando es initialized o fixed
   */
  getDefaultFilter(e, o) {
    const r = e, a = r.defaultOperandsArray ?? [], t = r.defaultOperand1, l = r?.selectOptions?.multiple ?? !1;
    let s, n = !1, i = [];
    l ? (s = r.defaultOperator ?? "in", n = a !== null && a.length > 0, r.defaultOperandsArray !== void 0 && Array.isArray(r.defaultOperandsArray) && (i = O(
      r.defaultOperandsArray
    ))) : (s = r.defaultOperator ?? "e", n = t !== null);
    const p = {
      id: 0,
      fieldType: "select",
      field: r,
      fixed: o,
      isSet: n,
      operator: s
    }, d = {
      ...p,
      fieldType: "select",
      multiple: !0,
      operandsArray: i
    }, u = {
      ...p,
      fieldType: "select",
      multiple: !1,
      operand1: t
    };
    return l ? d : u;
  }
  /**
   * Función para obtener el valor en terminos de HookForm desde los ids de los values.
   */
  getFormValue(e, o, r) {
    const a = e, t = r ?? void 0, l = a?.selectOptions?.multiple ?? !1;
    let s, n, i;
    l ? (i = t ? t.operator : a.defaultOperator ?? "in", s = r?.operandsArray) : (i = t ? t.operator : a.defaultOperator ?? "e", n = r?.operand1);
    const p = {
      fieldType: "select",
      formValueOperator: {
        id: i,
        label: o(`${y}.operator_${i}`)
      }
    }, d = {
      ...p,
      multiple: !0,
      formValueOperandsArray: s
    }, u = {
      ...p,
      multiple: !1,
      formValueOperand1: n
    };
    return l ? d : u;
  }
  /**
   * getFilterFromFormValue convierte un valor de formulario en un valor de filtro.
   */
  getFilterFromFormValue(e) {
    const o = e.formValueOperator.id, r = e.multiple, a = {
      fieldType: "select",
      operator: o
    }, t = {
      ...a,
      multiple: !0,
      operandsArray: e.formValueOperandsArray
    }, l = {
      ...a,
      operand1: e.formValueOperand1
    };
    return r ? t : l;
  }
  /**
   * getLabels devuelve las etiquetas para el filtro de selección.
   */
  getLabels(e, o, r, a) {
    const t = a, l = t?.selectOptions?.multiple ?? !1, s = e.operandsArray;
    let n = "";
    return l ? n = s?.reduce?.((i, p) => {
      const d = t.selectOptions?.options.find(
        (u) => u.id === p
      )?.label || "";
      return i ? `${i} | ${d}` : d;
    }, "")?.toString() || "?" : n = (e.operand1 ?? "?") + "", {
      labelField: e.field.label ?? o(e.field.dictionaryId),
      labelOperator: o(
        `${y}.operator_${e.operator}`
      ),
      labelOperands: n
    };
  }
  /**
   * getSchema devuelve el esquema de validación de Yup para un filtro de selección.
   */
  getSchema(e, o) {
    return F(S(T({
      formValueOperator: f.object().nullable().required(e(m.error_operator_required)),
      formValueOperandsArray: f.mixed().when(["multiple"], {
        is: !0,
        then: f.array().min(1, e(m.error_operand_required)).required(e(m.error_operand_required))
      }),
      formValueOperand1: f.mixed().when(["multiple"], {
        is: !1,
        then: f.mixed().test(
          "is-string-or-number",
          e(m.error_operand_required),
          (r) => typeof r == "string" || typeof r == "number"
        ).required(e(m.error_operand_required))
      })
    })));
  }
  /**
   * TODO: Documentar
   */
  verifyFilter(e, o) {
    const r = o, a = r?.selectOptions?.multiple ?? !1;
    if (typeof e.operator != "string" || !r)
      return !1;
    if (a) {
      if (A.findIndex((l) => l === e.operator) === -1 || !e.operandsArray || typeof e.operandsArray != "object" || Array.isArray(e.operandsArray) && e.operandsArray.length === 0)
        return !1;
      const t = O(
        e.operandsArray
      );
      if (!t || t?.length === 0)
        return !1;
    } else if (_.findIndex((t) => t === e.operator) === -1 || !e.operand1 || typeof e.operand1 != "string" && typeof e.operand1 != "number")
      return !1;
    return !0;
  }
}
export {
  M as SelectFilterHelpers,
  O as filterValidOperandsArraySelect
};
