"use client";

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

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

const Combobox = ({
  options,
  placeholder,
  handleChange,
  defaultValue,
  canUnselect,
  className,
}: any) => {
  const [open, setOpen] = React.useState(false);
  const initialValue =
    defaultValue && typeof defaultValue === "object" && defaultValue.value
      ? defaultValue.value
      : defaultValue;
  const [value, setValue] = React.useState(initialValue ?? "");

  return (
    <Popover open={open} onOpenChange={setOpen}>
      <PopoverTrigger className="dark:bg-form-strokedark" asChild>
        <Button
          variant="outline"
          role="combobox"
          aria-expanded={open}
          className={`w-[200px] justify-between ${className}`}
        >
          {value
            ? options &&
              options.find(
                (option: any) => String(option.value) === String(value)
              )?.label
            : placeholder}
          <ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
        </Button>
      </PopoverTrigger>
      <PopoverContent className="w-[200px] dark:!bg-form-strokedark max-h-80 !overflow-y-auto p-0 z-9999">
        <Command>
          {/* <CommandInput placeholder={placeholder} /> */}
          <CommandEmpty>{placeholder}</CommandEmpty>
          <CommandGroup className="overflow-y-auto ">
            {options &&
              options.map((option: any) => (
                <CommandItem
                  key={String(option.value)}
                  value={String(option.value)}
                  onSelect={(currentValue) => {
                    try {
                      handleChange(currentValue);
                    } catch (e) {
                      console.error("Combobox handleChange error:", e);
                    }

                    const normalized =
                      currentValue !== null && currentValue !== undefined
                        ? String(currentValue)
                        : "";

                    if (canUnselect) {
                      setValue(normalized === String(value) ? "" : normalized);
                    } else {
                      setValue(normalized);
                    }

                    setOpen(false);
                  }}
                >
                  <Check
                    className={cn(
                      "mr-2 h-4 w-4",
                      String(value) === String(option.value)
                        ? "opacity-100"
                        : "opacity-0"
                    )}
                  />
                  {option.label}
                </CommandItem>
              ))}
          </CommandGroup>
        </Command>
      </PopoverContent>
    </Popover>
  );
};
export default Combobox;
