<script setup>
import { reactiveOmit, useCurrentElement } from "@vueuse/core";
import { injectListboxRootContext, ListboxItem, useForwardPropsEmits, useId } from "reka-ui";
import { computed, onMounted, onUnmounted, ref, watch } from "vue";
import { cn } from "../../../utils/cn";
import { useCommand, useCommandGroup } from ".";
const props = defineProps({
  value: { type: null, required: true },
  disabled: { type: Boolean, required: false },
  asChild: { type: Boolean, required: false },
  as: { type: null, required: false },
  class: { type: [Boolean, null, String, Object, Array], required: false, skipCheck: true },
  keywords: { type: String, required: false }
});
const emits = defineEmits(["select"]);
const delegatedProps = reactiveOmit(props, "class", "keywords");
const forwarded = useForwardPropsEmits(delegatedProps, emits);
const id = useId();
const { filterState, allItems, allGroups } = useCommand();
const groupContext = useCommandGroup();
const listboxRoot = injectListboxRootContext();
function handleSelect(event) {
  filterState.search = "";
  if (!listboxRoot.multiple.value) return;
  event.preventDefault();
  listboxRoot.onValueChange(props.value);
  if (currentElement.value instanceof HTMLElement) {
    listboxRoot.changeHighlight(currentElement.value);
  }
}
const isRender = computed(() => {
  if (!filterState.search) {
    return true;
  } else {
    const filteredCurrentItem = filterState.filtered.items.get(id);
    if (filteredCurrentItem === void 0) {
      return true;
    }
    return filteredCurrentItem > 0;
  }
});
const itemRef = ref();
const currentElement = useCurrentElement(itemRef);
function indexedValue() {
  const label = currentElement.value instanceof HTMLElement ? currentElement.value.textContent ?? "" : props.value?.toString() ?? "";
  const kw = props.keywords?.trim();
  return kw ? `${label} ${kw}` : label;
}
onMounted(() => {
  if (!(currentElement.value instanceof HTMLElement))
    return;
  allItems.value.set(id, indexedValue());
  const groupId = groupContext?.id;
  if (groupId) {
    if (!allGroups.value.has(groupId)) {
      allGroups.value.set(groupId, /* @__PURE__ */ new Set([id]));
    } else {
      allGroups.value.get(groupId)?.add(id);
    }
  }
});
watch(() => props.keywords, () => {
  if (allItems.value.has(id)) allItems.value.set(id, indexedValue());
});
onUnmounted(() => {
  allItems.value.delete(id);
});
</script>

<template>
  <ListboxItem
    v-if="isRender"
    v-bind="forwarded"
    :id="id"
    ref="itemRef"
    data-slot="command-item"
    :class="cn('data-highlighted:bg-zinc-100 data-highlighted:text-zinc-700 hover:bg-zinc-50 [&_svg:not([class*=\'text-\'])]:text-zinc-700 cursor-pointer relative flex items-center gap-2 rounded-sm px-2 py-1.5 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', props.class)"
    @select="handleSelect"
    @mousedown.prevent
  >
    <slot />
  </ListboxItem>
</template>
