<template>
  <USwitch v-model="modelValue" :label="label" />
</template>

<script setup lang="ts">
import type { FormOptionDto } from '~~/.api-client'

const props = defineProps<{
  formOptions: FormOptionDto[]
}>()

const emit = defineEmits<{
  (e: 'update:formOptions', value: FormOptionDto[]): void
}>()

const modelValue = computed({
  get: () => props.formOptions[0]?.value === 'true',
  set: (val) => {
    const firstOption = props.formOptions[0]
    if (firstOption) {
      const updatedModelValue = [...props.formOptions]
      updatedModelValue[0] = { ...firstOption, value: val.toString() }
      emit('update:formOptions', updatedModelValue)
    }
  }
})

const label = computed(() => props.formOptions[0]?.label ?? '')
</script>
