import React, { useMemo } from 'react';
import { getMatchGroups, partitionByMlMatch, MatchRulesAttrNames, isAvailableEntityType } from '@reltio/mdm-sdk';
import { MultiValueSelector } from '../MultiValueSelector';
import { useMdmMetadata } from '../contexts/MdmModuleContext';
var prepareOptions = function (options) {
    if (options === void 0) { options = []; }
    return options.map(function (_a) {
        var label = _a.label, uri = _a.uri;
        return ({ label: label, value: uri });
    });
};
export var MatchRulesSelector = function (_a) {
    var name = _a.name, values = _a.values, onChange = _a.onChange, entityTypeUris = _a.entityTypeUris, TextFieldProps = _a.TextFieldProps, fullWidth = _a.fullWidth;
    var metadata = useMdmMetadata();
    var _b = useMemo(function () {
        var allEntityTypeUris = metadata.entityTypes.filter(isAvailableEntityType).map(function (_a) {
            var uri = _a.uri;
            return uri;
        });
        var displayedEntityTypeUris = entityTypeUris.length ? entityTypeUris : allEntityTypeUris;
        return partitionByMlMatch(displayedEntityTypeUris.flatMap(function (entityTypeUri) { return getMatchGroups(metadata, entityTypeUri); }));
    }, [entityTypeUris, metadata]), mlMatchRules = _b[0], matchRules = _b[1];
    var options = useMemo(function () { return prepareOptions(name === MatchRulesAttrNames.matchRules ? matchRules : mlMatchRules); }, [name, matchRules, mlMatchRules]);
    return (React.createElement(MultiValueSelector, { values: values, onChange: onChange, options: options, TextFieldProps: TextFieldProps, fullWidth: fullWidth }));
};
