
<template>
  <UiInputAutocomplete
      :model-value="modelValue"
      :field="field"
      :items="items"
      :is-loading="pending"
      :return-object="false"
      item-title="label"
      item-value="value"
      @update:model-value="$emit('update:model-value', $event)"
  />
</template>

<script setup lang="ts">


import {useEnumFetch} from "~/composables/data/useEnumFetch";
import ArrayUtils from "~/services/utils/arrayUtils";
import {ComputedRef} from "@vue/reactivity";
import {Enum} from "~/types/data";

const props = defineProps({
  modelValue: {
    type: String,
    required: false,
    default: null
  },
  enumName: {
    type: String,
    required: true
  },
  field: {
    type: String,
    required: false,
    default: null
  },
  label: {
    type: String,
    required: false,
    default: null
  }
})

const { fetch } = useEnumFetch()

const { data: enumItems, pending } = fetch(props.enumName)

const items: ComputedRef<Array<Enum>> = computed(() => {
  if (!enumItems.value) {
    return []
  }
  return ArrayUtils.sortObjectsByProp(enumItems.value, 'label') as Array<Enum>
})

</script>

<style scoped lang="scss">

</style>
