import * as u from "yup";
import { SELECT_ASYNC_OPERATORS_MULTIPLE as A, SELECT_ASYNC_OPERATORS_SINGLE as _ } from "../../../constants.js";
import { SelectAsyncFilter as g } from "./index.js";
import { DYNAMIC_FILTER_DICTIONARY_ID as y, DICCTIONARY as f } from "../../../dictionary.js";
import { createValidationSchema as b, createValidationFullObject as S, createValidationPartialObject as I } from "../../../../hook-form/RHFormProvider/schema.js";
function O(c, e) {
  const o = Array.isArray(c) ? c : [c], r = e?.selectAsyncOptions?.getOptionId, a = e?.selectAsyncOptions?.getOptionLabel;
  return o?.filter((t) => typeof t == "object" && r?.(t) && a?.(t));
}
class M {
  getComponent() {
    return g;
  }
  /**
   * getDefaultFilter devuelve un filtro por defecto para un campo de selección asíncrona.
   */
  getDefaultFilter(e, o) {
    const r = e, a = r.defaultOperandsArray ?? [], t = r.defaultOperand1, l = r?.selectAsyncOptions?.multiple ?? !1;
    let i, n = !1, s = [];
    l ? (i = r.defaultOperator ?? "in", n = a !== null && a.length > 0, r.defaultOperandsArray !== void 0 && Array.isArray(r.defaultOperandsArray) && (s = O(
      r.defaultOperandsArray,
      r
    ))) : (i = r.defaultOperator ?? "e", n = !!t);
    const p = {
      id: 0,
      fieldType: "selectAsync",
      field: r,
      fixed: o,
      isSet: n,
      operator: i
    }, d = {
      ...p,
      multiple: !0,
      operandsArray: s
    }, m = {
      ...p,
      multiple: !1,
      operand1: t
    };
    return l ? d : m;
  }
  /**
   * getFormValue convierte un valor de filtro en un valor de formulario.
   */
  getFormValue(e, o, r) {
    const a = e, t = r ?? void 0, l = a?.selectAsyncOptions?.multiple ?? !1;
    let i, n, s;
    l ? (s = t ? t.operator : a.defaultOperator ?? "in", i = r?.operandsArray) : (s = t ? t.operator : a.defaultOperator ?? "e", n = r?.operand1);
    const p = {
      fieldType: "selectAsync",
      formValueOperator: {
        id: s,
        label: o(`${y}.operator_${s}`)
      }
    }, d = {
      ...p,
      multiple: !0,
      formValueOperandsArray: i
    }, m = {
      ...p,
      multiple: !1,
      formValueOperand1: n
    };
    return l ? d : m;
  }
  /**
   * getFilterFromFormValue convierte un valor de formulario en un valor de filtro.
   */
  getFilterFromFormValue(e) {
    const o = e.formValueOperator.id, r = e.multiple, a = {
      fieldType: "selectAsync",
      operator: o
    }, t = {
      ...a,
      multiple: !0,
      operandsArray: e.formValueOperandsArray
    }, l = {
      ...a,
      operand1: e.formValueOperand1
    };
    return r ? t : l;
  }
  /**
   * getLabels devuelve las etiquetas para el campo, operador y operandos.
   */
  getLabels(e, o, r, a) {
    const t = a, l = t?.selectAsyncOptions?.multiple ?? !1, i = e.operandsArray;
    let n = "";
    return l ? n = i?.reduce?.((s, p) => {
      const d = t.selectAsyncOptions?.getOptionLabel?.(
        p
      ) || "";
      return s ? `${s} | ${d}` : d;
    }, "")?.toString() || "?" : n = (t.selectAsyncOptions?.getOptionLabel?.(
      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 el filtro.
   */
  getSchema(e, o) {
    return b(S(I({
      formValueOperator: u.object().nullable().required(e(f.error_operator_required)),
      formValueOperandsArray: u.mixed().when(["multiple"], {
        is: !0,
        then: u.array().min(1, e(f.error_operand_required)).required(e(f.error_operand_required))
      }),
      formValueOperand1: u.mixed().when(["multiple"], {
        is: !1,
        then: u.object().nullable().required(e(f.error_operand_required))
      })
    })));
  }
  /**
   * verifyFilter verifica si el filtro es válido.
   */
  verifyFilter(e, o) {
    const r = o, a = r?.selectAsyncOptions?.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,
        r
      );
      if (!t || t?.length === 0)
        return !1;
    } else if (_.findIndex(
      (t) => t === e.operator
    ) === -1 || !e.operand1 || typeof e.operand1 != "object")
      return !1;
    return !0;
  }
}
export {
  M as SelectAsyncFilterHelpers,
  O as filterValidOperandsArraySelectAsync
};
