<script setup lang="ts">
import type { FormErrors } from '~/types/error'
import FieldGroup from '~/components/pub/my-ui/form/field-group.vue'
import Field from '~/components/pub/my-ui/form/field.vue'
import Select from '~/components/pub/my-ui/form/select.vue'
import { cn } from '~/lib/utils'
import { relationshipCodes } from '~/lib/constants'

const props = defineProps<{
  fieldName: string
  placeholder?: string
  errors?: FormErrors
  class?: string
  selectClass?: string
  fieldGroupClass?: string
  labelClass?: string
  isDisabled?: boolean
}>()

const { fieldName = 'phoneNumber', errors, class: containerClass, selectClass, fieldGroupClass } = props

const emergencyContactOptions = Object.entries(relationshipCodes).map(([value, label]) => ({
  label,
  value,
  ...(value === 'other' && { priority: -1 })
}))
</script>

<template>
  <FieldGroup :class="cn('select-field-group', fieldGroupClass, containerClass)">
    <Field
      :id="fieldName"
      :errors="errors"
      :class="cn('select-field-wrapper')"
    >
      <FormField
        v-slot="{ componentField }"
        :name="fieldName"
      >
        <FormItem>
          <FormControl>
            <Select
              :id="fieldName"
              v-bind="componentField"
              :is-disabled="isDisabled"
              :items="emergencyContactOptions"
              :placeholder="placeholder"
              :preserve-order="true"
              :class="
                cn(
                  'text-sm transition-all duration-200 focus:outline-none focus:ring-1 focus:ring-black focus:ring-offset-0',
                  selectClass,
                )
              "
            />
          </FormControl>
          <FormMessage />
        </FormItem>
      </FormField>
    </Field>
  </FieldGroup>
</template>
