import { jsx as _jsx } from "react/jsx-runtime";
import { SelectList } from "../design/SelectList.js";
import { optionToSelectListOption, optionValueKey } from "./option-map.js";
export function SelectMulti(props) {
    const selectedValues = props.defaultValue ?? [];
    const focusValue = props.focusValue ?? (props.initialFocusLast
        ? props.options[props.options.length - 1]?.value
        : props.options[0]?.value);
    const selectedIndex = focusValue === undefined
        ? -1
        : props.options.findIndex((option) => optionValueKey(option.value) === optionValueKey(focusValue));
    return (_jsx(SelectList, { options: multiSelectOptionsToList({
            options: props.options,
            selectedValues,
            focusedValue: focusValue,
        }), selectedIndex: selectedIndex, visibleCount: props.visibleOptionCount ?? 5, hideIndexes: props.hideIndexes }));
}
export function multiSelectOptionsToList(input) {
    const selected = new Set(input.selectedValues.map(optionValueKey));
    const focusedKey = input.focusedValue === undefined ? undefined : optionValueKey(input.focusedValue);
    return input.options.map((option) => {
        const key = optionValueKey(option.value);
        return optionToSelectListOption(option, {
            selected: selected.has(key),
            focused: focusedKey !== undefined && key === focusedKey,
        });
    });
}
export default SelectMulti;
