"use client";

import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select";
import type { ItemSearchSort } from "@/lib/item-search-params";

const SORT_LABELS: Record<ItemSearchSort, string> = {
  relevance: "Relevance",
  newest: "Newest",
  price_asc: "Price: Low to High",
  price_desc: "Price: High to Low",
  popular: "Most Popular",
};

interface SortDropdownProps {
  value: ItemSearchSort;
  hasQuery: boolean;
  onChange: (next: ItemSearchSort) => void;
}

export function SortDropdown({ value, hasQuery, onChange }: SortDropdownProps) {
  // Hide 'relevance' when there is no query — it's only meaningful with text match
  const visibleOptions = (Object.keys(SORT_LABELS) as ItemSearchSort[]).filter(
    (k) => hasQuery || k !== "relevance",
  );

  return (
    <Select value={value} onValueChange={(v) => onChange(v as ItemSearchSort)}>
      <SelectTrigger className="h-10 w-[200px]">
        <SelectValue placeholder="Sort" />
      </SelectTrigger>
      <SelectContent>
        {visibleOptions.map((k) => (
          <SelectItem key={k} value={k}>
            {SORT_LABELS[k]}
          </SelectItem>
        ))}
      </SelectContent>
    </Select>
  );
}
