<script setup lang="ts">
import type {
  DropdownMenuRadioItemEmits,
  DropdownMenuRadioItemProps,
} from 'reka-ui';
import type { HTMLAttributes } from 'vue';
import { reactiveOmit } from '@vueuse/core';
import { UiRadioGroup, UiRadioGroupItem } from '@/components/UiRadioGroup';
import { DropdownMenuRadioItem, useForwardPropsEmits } from 'reka-ui';
import { cn } from '@/lib/utils';
import { computed } from 'vue';

const props = defineProps<
  DropdownMenuRadioItemProps & { class?: HTMLAttributes['class'] }
>();

const emits = defineEmits<DropdownMenuRadioItemEmits>();

const delegatedProps = reactiveOmit(props, 'class');

const forwarded = useForwardPropsEmits(delegatedProps, emits);

const visualRadioValue = computed(() => String(props.value ?? ''));
</script>

<template>
  <DropdownMenuRadioItem
    data-slot="dropdown-menu-radio-item"
    v-bind="forwarded"
    :class="
      cn(
        'group/dropdown-menu-radio-item hover:bg-background-surface-muted-subtle data-[highlighted]:bg-background-surface-muted-subtle focus:text-foreground relative flex cursor-default items-center gap-2 rounded-sm py-1.5 pr-2 pl-8 text-sm outline-hidden select-none data-[disabled]:pointer-events-none data-[disabled]:opacity-50',
        props.class,
      )
    "
  >
    <span
      data-slot="dropdown-menu-radio-indicator"
      aria-hidden="true"
      class="pointer-events-none absolute left-2 flex size-4 items-center justify-center"
    >
      <slot name="indicator-icon">
        <UiRadioGroup
          name="dropdown-menu-radio-indicator"
          :model-value="visualRadioValue"
          class="pointer-events-none contents"
        >
          <UiRadioGroupItem
            :id="`dropdown-menu-radio-indicator-${visualRadioValue}`"
            :value="visualRadioValue"
            tabindex="-1"
            class="pointer-events-none border-input group-data-[state=checked]/dropdown-menu-radio-item:border-accent-default [&_[data-slot=radio-group-indicator]]:opacity-0 group-data-[state=checked]/dropdown-menu-radio-item:[&_[data-slot=radio-group-indicator]]:opacity-100"
          />
        </UiRadioGroup>
      </slot>
    </span>
    <slot />
  </DropdownMenuRadioItem>
</template>
