"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useMultipleSelection = void 0;
const react_1 = require("react");
const errors_1 = require("@blackglory/errors");
const iterable_operator_1 = require("iterable-operator");
function useMultipleSelection(values, defaultSelectedIndexes = []) {
    (0, errors_1.assert)(values.length > 0, 'The parameter values must be a non-empty array');
    for (const index of defaultSelectedIndexes) {
        (0, errors_1.assert)(index >= 0 && index < values.length, 'The index of parameter defaultSelectedIndexes must be in the range of 0 to values.length');
    }
    const [selectedIndexes, setSelectedIndexes] = (0, react_1.useState)(new Set(defaultSelectedIndexes));
    return {
        values: (0, iterable_operator_1.toArray)((0, iterable_operator_1.map)(selectedIndexes, i => values[i])),
        options: values.map((value, index) => {
            const selected = selectedIndexes.has(index);
            const select = () => {
                const set = new Set(selectedIndexes);
                set.add(index);
                setSelectedIndexes(set);
            };
            const unselect = () => {
                const set = new Set(selectedIndexes);
                set.delete(index);
                setSelectedIndexes(set);
            };
            return {
                value,
                index,
                selected,
                select,
                unselect,
                toggle: selected ? unselect : select
            };
        })
    };
}
exports.useMultipleSelection = useMultipleSelection;
//# sourceMappingURL=use-multiple-selection.js.map