"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useSingleSelection = void 0;
const react_1 = require("react");
const types_1 = require("@blackglory/types");
const errors_1 = require("@blackglory/errors");
function useSingleSelection(values, defaultIndex) {
    (0, errors_1.assert)(values.length > 0, 'The parameter values must be a non-empty array');
    if ((0, types_1.isntUndefined)(defaultIndex)) {
        (0, errors_1.assert)(defaultIndex >= 0 && defaultIndex < values.length, 'The parameter defaultIndex must be in the range of 0 to values.length');
    }
    const [selectedIndex, setSelectedIndex] = (0, react_1.useState)(defaultIndex);
    return {
        value: (0, types_1.isUndefined)(selectedIndex) ? undefined : values[selectedIndex],
        options: values.map((value, index) => ({
            value,
            index,
            selected: index === selectedIndex,
            select: () => setSelectedIndex(index)
        }))
    };
}
exports.useSingleSelection = useSingleSelection;
//# sourceMappingURL=use-single-selection.js.map