<script setup lang="ts">
import { cn } from '@/lib/utils'
import { Check } from 'lucide-vue-next'
import {
  SelectItem,
  SelectItemIndicator,
  type SelectItemProps,
  SelectItemText,
  useForwardProps,
} from 'reka-ui'
import { computed, inject, type HTMLAttributes } from 'vue'

const props = withDefaults(
  defineProps<SelectItemProps & { class?: HTMLAttributes['class']; hideIndicator?: boolean }>(),
  {
    hideIndicator: undefined,
  }
)

// Inject from SelectContent, fallback to false if not provided
const injectedHideIndicator = inject<boolean>('selectHideIndicator', false)

// Local prop takes precedence over injected value
const shouldHideIndicator = computed(() => props.hideIndicator ?? injectedHideIndicator)

const delegatedProps = computed(() => {
  const { class: _, hideIndicator: __, ...delegated } = props

  return delegated
})

const forwardedProps = useForwardProps(delegatedProps)
</script>

<template>
  <SelectItem
    data-slot="select-item"
    v-bind="forwardedProps"
    :class="
      cn(
        `focus:bg-accent focus:text-accent-foreground [&_svg:not([class*='text-'])]:text-muted-foreground relative flex w-full cursor-default items-center gap-2 rounded-sm py-1.5 pl-2 text-sm outline-hidden select-none data-[disabled]:pointer-events-none data-[disabled]:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 *:[span]:last:flex *:[span]:last:items-center *:[span]:last:gap-2`,
        !shouldHideIndicator && 'pr-8',
        shouldHideIndicator && 'pr-2',
        props.class
      )
    "
  >
    <span
      v-if="!shouldHideIndicator"
      class="absolute right-2 flex size-3.5 items-center justify-center"
    >
      <SelectItemIndicator>
        <Check class="size-4" />
      </SelectItemIndicator>
    </span>

    <SelectItemText>
      <slot />
    </SelectItemText>
  </SelectItem>
</template>
