import {QUERY_TYPE} from "~/types/enums";
import {Country} from "~/models/Core/Country";
import { useFetch, computed } from '@nuxtjs/composition-api'
import DataProvider from "~/services/data/dataProvider";
import {repositoryHelper} from "~/services/store/repository";

/**
 * @category composables/data
 * @param $dataProvider
 * Composable Classe qui va récupérer les Countries
 */
export function useCountryProvider($dataprovider: DataProvider){
  const {fetch, fetchState} = useFetch(async () => {
    await $dataprovider.invoke({
      type: QUERY_TYPE.MODEL,
      model: Country
    })
  })

  const countries =  computed(() => {
    return repositoryHelper.findCollectionFromModel(Country)
  })

  return {
    countries,
    fetch,
    fetchState
  }
}
