"use client";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import * as React from "react";
import { Check, ChevronsUpDown } from "lucide-react";
import { useEditContext } from "../../editor/client/editContext";
import { cn } from "../../lib/utils";
import { Button } from "./button";
import { Switch } from "./switch";
import { FilterInput, highlightMatch } from "../FilterInput";
import { Command, CommandEmpty, CommandGroup, CommandItem, CommandList, } from "./command";
import { Popover, PopoverContent, PopoverTrigger } from "./popover";
export function LanguageSelector({ selectedLanguage, onLanguageSelected, showAllLanguagesSwitch, showAllLanguages = false, disabled = false, }) {
    const [open, setOpen] = React.useState(false);
    const [showAllLanguagesInternal, setShowAllLanguagesInternal] = React.useState(showAllLanguages);
    const [filterText, setFilterText] = React.useState("");
    const triggerRef = React.useRef(null);
    const [portalContainer, setPortalContainer] = React.useState(null);
    const editContext = useEditContext();
    const allLanguages = editContext?.itemLanguages || [];
    const currentLanguage = allLanguages.find((x) => x.languageCode === selectedLanguage);
    let languages = showAllLanguagesInternal
        ? allLanguages
        : allLanguages.filter((x) => x.versions > 0);
    // Apply text filter
    if (filterText.trim()) {
        languages = languages.filter((x) => x.name.toLowerCase().includes(filterText.toLowerCase()));
    }
    languages.sort((a, b) => a.name.localeCompare(b.name));
    const selectLanguage = async (language) => {
        onLanguageSelected(language);
        setOpen(false);
        // Reset filter when language is selected
        setFilterText("");
        return;
    };
    const handleOpenChange = (newOpen) => {
        if (newOpen && triggerRef.current) {
            const dialogContent = triggerRef.current.closest('[data-slot="dialog-content"]');
            setPortalContainer(dialogContent);
        }
        setOpen(newOpen);
        if (!newOpen) {
            // Reset filter when popover closes
            setFilterText("");
        }
    };
    return (_jsxs(Popover, { open: open, onOpenChange: handleOpenChange, enableIframeClickDetection: false, children: [_jsx(PopoverTrigger, { asChild: true, children: _jsxs(Button, { ref: triggerRef, variant: "select", role: "combobox", "aria-expanded": open, className: cn("min-w-0 max-w-full", disabled && "cursor-not-allowed opacity-50"), "data-testid": "language-selector", disabled: disabled, title: currentLanguage?.name ?? undefined, children: [_jsxs("div", { className: "flex min-w-0 items-center gap-2", children: [currentLanguage && (_jsxs(_Fragment, { children: [_jsx("img", { src: currentLanguage.icon, className: "h-5 shrink-0", alt: currentLanguage.name }), _jsx("span", { "data-testid": "language-selector-label", className: "truncate min-w-[5ch]", children: currentLanguage.name })] })), !currentLanguage && _jsx("div", { children: "Select language" })] }), _jsx(ChevronsUpDown, { className: "h-4 w-4 shrink-0 opacity-50 hidden md:block", strokeWidth: 1 })] }) }), _jsx(PopoverContent, { className: "w-64 p-0", align: "start", container: portalContainer, onMouseDown: (e) => e.stopPropagation(), onClick: (e) => e.stopPropagation(), children: _jsxs(Command, { children: [showAllLanguagesSwitch && (_jsxs("div", { className: "flex items-center justify-center gap-2 p-2 text-xs font-light", children: [_jsx("span", { className: "cursor-pointer", onClick: () => setShowAllLanguagesInternal(false), children: "Item languages" }), _jsx(Switch, { checked: showAllLanguagesInternal, className: "cursor-pointer", onCheckedChange: (checked) => setShowAllLanguagesInternal(checked) }), _jsx("span", { className: "cursor-pointer", onClick: () => setShowAllLanguagesInternal(true), children: "All languages" })] })), _jsx("div", { className: "p-2", children: _jsx(FilterInput, { value: filterText, onChange: setFilterText, placeholder: "Filter languages...", className: "w-full", onClear: () => setFilterText("") }) }), _jsxs(CommandList, { className: "max-h-[50vh]", children: [_jsx(CommandEmpty, { children: filterText.trim()
                                        ? `No languages found matching "${filterText}"`
                                        : "This item has no languages" }), _jsx(CommandGroup, { children: languages.map((language) => (_jsxs(CommandItem, { value: language.languageCode, onSelect: () => selectLanguage(language), className: cn("flex items-center gap-2 px-2 py-2 text-xs font-light", 
                                        // Remove background when selected (auto-focused) but not hovered
                                        "data-[selected=true]:bg-transparent", 
                                        // Force hover styles to override selected state
                                        "[&:hover]:!bg-accent [&:hover]:!text-accent-foreground"), children: [_jsx("img", { src: language.icon, className: "h-5 w-5 shrink-0", alt: language.name }), _jsxs("span", { className: "flex-1", children: [highlightMatch(language.name, filterText), " (", language.versions, ")"] }), _jsx(Check, { className: cn("h-4 w-4", selectedLanguage === language.languageCode
                                                    ? "opacity-100"
                                                    : "opacity-0"), strokeWidth: 1 })] }, language.languageCode))) })] })] }) })] }));
}
//# sourceMappingURL=LanguageSelector.js.map