import { useFetch } from '@win2win/shared-ui';
import { computed } from 'vue';
import { useCaptacionLite } from '../useStoreLite';

export enum TipoHipoteca {
  NUEVA_HIPOTECA = 1,
  CAMBIO_BANCO = 2,
  REFINANCIACION = 3,
}

export const tipoHipotecaLabel = {
  [TipoHipoteca.NUEVA_HIPOTECA]: 'Nueva hipoteca',
  [TipoHipoteca.CAMBIO_BANCO]: 'Cambio de banco',
  [TipoHipoteca.REFINANCIACION]: 'Refinanciación',
};

export function useTipoHipoteca() {
  const { data: captacion } = useCaptacionLite();
  const key = computed(() => ['captacion', captacion.value.ID_CAPTACION || null, 'tipo_hipoteca']);
  const query = computed(() => `w2w-query/pedido(${captacion.value.ID_PEDIDO})->ped_producto->producto(id=ped_producto.id_producto)`);
  const { data } = useFetch<any>(key, query, {
    pedido: ['id'],
    ped_producto: ['id'],
    producto: ['id', 'nombre'],
  });
  const tipo = computed(() => {
    if (!data.value) return null;
    const id = data.value.ped_producto.producto.id;
    return id as TipoHipoteca;
  });
  const label = computed(() => tipoHipotecaLabel[tipo.value ?? 0]);
  return { tipo, label };
}
