import { jsx as _jsx } from "react/jsx-runtime";
import { SelectList } from "../design/SelectList.js";
import { optionToSelectListOption, optionValueKey } from "./option-map.js";
export function Select(props) {
    const selectedIndex = selectFocusIndex({
        options: props.options,
        selectedValue: props.focusValue ?? props.defaultFocusValue ?? props.defaultValue,
    });
    return (_jsx(SelectList, { options: selectOptionsToList({
            options: props.options,
            selectedValue: props.defaultValue,
            focusedValue: props.focusValue ?? props.defaultFocusValue ?? props.defaultValue,
        }), selectedIndex: selectedIndex, visibleCount: props.visibleOptionCount ?? 5, hideIndexes: props.hideIndexes }));
}
export function selectOptionsToList(input) {
    const selectedKey = input.selectedValue === undefined ? undefined : optionValueKey(input.selectedValue);
    const focusedKey = input.focusedValue === undefined ? selectedKey : optionValueKey(input.focusedValue);
    return input.options.map((option) => {
        const key = optionValueKey(option.value);
        return optionToSelectListOption(option, {
            selected: selectedKey !== undefined && key === selectedKey,
            focused: focusedKey !== undefined && key === focusedKey,
        });
    });
}
export function selectFocusIndex(input) {
    if (input.options.length === 0)
        return -1;
    const selectedKey = input.selectedValue === undefined ? undefined : optionValueKey(input.selectedValue);
    const selectedIndex = selectedKey === undefined
        ? -1
        : input.options.findIndex((option) => optionValueKey(option.value) === selectedKey);
    if (selectedIndex >= 0 && !input.options[selectedIndex]?.disabled)
        return selectedIndex;
    const firstEnabled = input.options.findIndex((option) => !option.disabled);
    return firstEnabled >= 0 ? firstEnabled : 0;
}
export default Select;
