import { ControlDetail } from 'src/widgets/hipotecaWidgets/types';

export function useControlDetail(tipos: any[]) {
  const getDetail = (tipo: string) => tipos.find((t) => t.value === tipo)?.detail || [];
  const validateCondition = (control: ControlDetail, item: { detalle?: { [key: string]: any } }) => {
    if (!control.conditions) return true;
    return control.conditions.every((condition) => {
      if (!item.detalle || !item.detalle[condition.key]) return false;
      const value = item.detalle[condition.key];
      const conditionValue = condition.value;
      return {
        '==': value === conditionValue,
        '!=': value !== conditionValue,
        '>': value > conditionValue,
        '<': value < conditionValue,
      }[condition.operator];
    });
  };
  return { getDetail, validateCondition };
}
