"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModuleValueSelector = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const ValueSelector_1 = require("../ValueSelector");
const AdaptableContext_1 = require("../../AdaptableContext");
const react_1 = require("react");
const ArrayExtensions_1 = tslib_1.__importDefault(require("../../../Utilities/Extensions/ArrayExtensions"));
const ModuleValueSelector = (props) => {
    const { options, value, onChange, selectionBoxPosition = 'top', ...valueSelectorProps } = props;
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const selectedModulesMap = (0, react_1.useMemo)(() => {
        if (!value || !value.length) {
            return new Map();
        }
        return value.reduce((acc, val) => {
            acc.set(val, true);
            return acc;
        }, new Map());
    }, [value]);
    const allowReorder = (0, react_1.useCallback)((option) => {
        return selectedModulesMap.has(option);
    }, [selectedModulesMap]);
    const moduleLabelMap = (0, react_1.useMemo)(() => {
        const labelMap = new Map();
        options.forEach((module) => labelMap.set(module, adaptable.ModuleService.getModuleFriendlyName(module) ?? module));
        return labelMap;
    }, [options]);
    const sortedOptions = (0, react_1.useMemo)(() => {
        return ArrayExtensions_1.default.sortArrayWithOrder(options, value, { sortUnorderedItems: true });
    }, [allowReorder, options, value]);
    return (React.createElement(ValueSelector_1.ValueSelector, { ...valueSelectorProps, options: sortedOptions, value: value, onChange: onChange, allowReorder: allowReorder, toIdentifier: (module) => module, toLabel: (module) => moduleLabelMap.get(module), selectionBoxPosition: selectionBoxPosition, isOptionDisabled: props.isOptionDisabled, disabled: props.disabled }));
};
exports.ModuleValueSelector = ModuleValueSelector;
