"use client";

import { useT } from "@/shared/i18n/client";
import {
  Select,
  SelectContent,
  SelectGroup,
  SelectItem,
  SelectLabel,
  SelectSeparator,
  SelectTrigger,
  SelectValue,
} from "@/presentation/components/ui/select";
import type { AnnouncementCatalogs } from "@/domain/entities/announcement";
import { isMenuGroup } from "@/shared/lib/announcements";

const NONE = "__none";

/**
 * Element picker: the elements of the chosen page, then the menu entries and the menu groups
 * (always offered) — or the menu only (spec §4.1).
 */
export function ElementSelect({
  id,
  catalogs,
  pageKey,
  value,
  onChange,
  menuOnly,
  allowNone,
  noneLabel,
  invalid,
  disabled,
}: {
  id?: string;
  catalogs: AnnouncementCatalogs;
  pageKey: string | null;
  value: string | null;
  onChange: (anchorKey: string | null) => void;
  menuOnly?: boolean;
  allowNone?: boolean;
  noneLabel?: string;
  invalid?: boolean;
  disabled?: boolean;
}) {
  const t = useT();
  const e = t.announcements.editor;
  const page = catalogs.pages.find((p) => p.key === pageKey);
  const pageAnchors = menuOnly || !pageKey ? [] : catalogs.anchors.filter((a) => a.page_key === pageKey);
  const menu = catalogs.anchors.filter((a) => a.page_key === null && !isMenuGroup(a.key));
  const groups = catalogs.anchors.filter((a) => a.page_key === null && isMenuGroup(a.key));

  return (
    <Select value={value ?? NONE} onValueChange={(v) => onChange(v === NONE ? null : v)} disabled={disabled}>
      <SelectTrigger id={id} className="w-full" aria-invalid={invalid}>
        <SelectValue placeholder={menuOnly ? e.menuEntry : e.elementPlaceholder} />
      </SelectTrigger>
      <SelectContent>
        {(allowNone || !value) && (
          <SelectItem value={NONE} className="text-muted-foreground">
            {noneLabel ?? (menuOnly ? e.menuEntry : e.elementPlaceholder)}
          </SelectItem>
        )}
        {pageAnchors.length > 0 && page && (
          <SelectGroup>
            <SelectLabel>{e.pageGroup(page.label)}</SelectLabel>
            {pageAnchors.map((a) => (
              <SelectItem key={a.key} value={a.key}>{a.label}</SelectItem>
            ))}
          </SelectGroup>
        )}
        {pageAnchors.length > 0 && <SelectSeparator />}
        <SelectGroup>
          <SelectLabel>{e.menuGroup}</SelectLabel>
          {/* Fleet's French menu label, the English one as a hint. */}
          {menu.map((a) => (
            <SelectItem key={a.key} value={a.key}>
              {a.label}
              {a.hint && <span className="text-muted-foreground">{a.hint}</span>}
            </SelectItem>
          ))}
        </SelectGroup>
        {groups.length > 0 && (
          <>
            <SelectSeparator />
            <SelectGroup>
              <SelectLabel>{e.menuGroups}</SelectLabel>
              {groups.map((a) => (
                <SelectItem key={a.key} value={a.key}>
                  {a.label}
                  {a.hint && <span className="text-muted-foreground">{a.hint}</span>}
                </SelectItem>
              ))}
            </SelectGroup>
          </>
        )}
      </SelectContent>
    </Select>
  );
}
