import { useFetch } from '@win2win/shared-ui';
import { MapPlace } from 'src/components/maps/MapPlaces.vue';
import { computed, isRef, Ref } from 'vue';
import { Mapeo } from './models';
import { useSeduviPlaces } from './useSeduviPlaces';

export function useFetchSeduvi<T = MapPlace>(idSeduvi: string | Ref<string>, raw = false) {
  const idSeduviRef = computed(() => (isRef(idSeduvi) ? idSeduvi.value : idSeduvi));
  const { formatPlace } = useSeduviPlaces();
  const quertKey = computed(() => ['seduvis', idSeduviRef.value]);
  const url = computed(() => `places/${idSeduviRef.value}`);
  const { data, fetching } = useFetch<T | null>(quertKey, url, undefined, (item) => (raw ? item : formatPlace(item)));

  const { data: mapping, fetching: fetchingMapping } = useFetch<Mapeo[]>(['seduvis', idSeduviRef.value, 'mapeos'], 'places/' + idSeduviRef.value + '/mapping');

  return {
    seduvi: data,
    fetching,
    mapping,
    fetchingMapping,
  };
}
