<template>
  <div
    :class="[
      cn.b(),
      cn.is('checked', checked),
      cn.is('disabled', disabled),
      cn.is('focused', focused),
      cn.has('label', hasLabel),
    ]"
  >
    <label :class="[cn.e('wrapper')]">
      <input
        :name="id"
        :class="[cn.e('input')]"
        type="checkbox"
        :disabled="disabled"
        :checked="checked"
        @focus="focused = true"
        @blur="focused = false"
        @change="handleChange"
      >

      <div :class="[cn.e('inner')]" />

      <p :class="[cn.e('label')]">
        <slot>{{ label }}</slot>
      </p>
    </label>
  </div>
</template>

<script setup lang="ts">
import { useSlots } from 'vue'
import useClassname from '../../composables/use-classname'

export interface Props {
  id: string
  value: string | number
  label?: string
  disabled?: boolean
  modelValue?: string | number
  required?: boolean
}

defineOptions({
  name: 'UiRadio',
  modelValue: undefined,
})

const props = withDefaults(defineProps<Props>(), {
  disabled: false,
  required: false,
})

defineEmits<{
  'update:modelValue': [state: boolean | string | number]
  focus: []
  blur: []
}>()

const slots = useSlots()

const cn = useClassname('ui-radio')

const { checked, focused, hasLabel, handleChange } = useRadio(props, slots)
</script>

<style lang="scss">
@use 'styles';
</style>
