"use client"

import * as React from "react"
import { Check, ChevronDown, ChevronsUpDown, Loader2 } from "lucide-react"

import { cn } from "@/lib/utils"
import { Button } from "@/components/ui/button"
import {
  Command,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
} from "@/components/ui/command"
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@/components/ui/popover"


type Data = {
    value:string;
    label:string;
    keywords?: string[] | undefined;
}

interface ComboboxProps {
    data:Data[]
    value?: string | null;
    placeholder?:string;
    loading?:boolean;
    onChange: (value: string) => void;
    onInputChange?: (value: string) => void;
}

export function Combobox({ data, value,placeholder, loading, onChange, onInputChange }: ComboboxProps) {
  const [open, setOpen] = React.useState(false)
  return (  
    <Popover open={open} onOpenChange={setOpen}>
      <PopoverTrigger asChild>
        <Button
          variant="outline"
          role="combobox"
          aria-expanded={open}
          className={cn(
            "w-full justify-between ",
            !value && "text-muted-foreground"
          )}
        >
          <div className="flex flex-row items-center gap-2">
            {loading ? <Loader2 className="mr-2 h-4 w-4 animate-spin" /> : null}
            {value && !loading
              ? data.find((item) => item.value === value)?.label
              : placeholder ? placeholder : "Select..."}
          </div>
          <ChevronDown className="opacity-50" />
        </Button>
      </PopoverTrigger>
      <PopoverContent  className="w-[var(--radix-popper-anchor-width)] p-0"
            side="bottom"
            align="start">
        <Command 
         filter={(value, search, keywords) => {
            const extendValue = value + " " + keywords?.join(" ");
            if (extendValue.includes(search)) return 1;
            return 0;
          }}>
          <CommandInput 
            placeholder="Cari..."  
            className="border-none hover:border-none focus:outline-none"
            onValueChange={onInputChange}
          />
          <CommandList>
            <CommandEmpty>No data found.</CommandEmpty>
            <CommandGroup>
              {data?.map((item) => (
                <CommandItem
                  key={item.value}
                  value={item.value}
                  keywords={[
                    item.label.toLowerCase(),
                    ...(item.keywords || []),
                  ]}
                  onSelect={(currentValue) => {
                    onChange(currentValue === value ? "" : currentValue)
                    setOpen(false)
                  }}
                >
                  {item.label}
                  <Check
                    className={cn(
                      "ml-auto",
                      value === item.value ? "opacity-100" : "opacity-0"
                    )}
                  />
                </CommandItem>
              ))}
            </CommandGroup>
          </CommandList>
        </Command>
      </PopoverContent>
    </Popover>
  )
}
