var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import React, { useCallback, useMemo } from 'react';
import i18n from 'ui-i18n';
import classnames from 'classnames';
import { getAttributeTypeSubAttributes } from '@reltio/mdm-sdk';
import Typography from '@mui/material/Typography';
import { MultiSelect } from '../MultiSelect';
import { ReactSelectMultiValue } from '../ReactSelectMultiValue';
import { ReactSelectDropdownIndicatorWithIconButton } from '../ReactSelectDropdownIndicatorWithIconButton';
import { AttributeListItem } from '../AttributeListItem';
import { useMdmMetadata } from '../contexts/MdmModuleContext';
import { customStyles, useStyles } from './styles';
export var AttributeSelector = function (_a) {
    var entityType = _a.entityType, selectedAttributes = _a.selectedAttributes, className = _a.className, onChange = _a.onChange, title = _a.title, disableUnderline = _a.disableUnderline, _b = _a.menuPlacement, menuPlacement = _b === void 0 ? 'bottom' : _b;
    var styles = useStyles();
    var metadata = useMdmMetadata();
    var prepareItems = useCallback(function (data, level, prevLabel) {
        if (level === void 0) { level = 0; }
        if (prevLabel === void 0) { prevLabel = ''; }
        return data.reduce(function (acc, item) {
            if (item.hidden)
                return acc;
            var chipLabel = "".concat(prevLabel && prevLabel + ' / ').concat(item.label);
            acc.push({
                label: item.label,
                value: item.uri,
                attrType: item,
                chipLabel: chipLabel,
                level: level
            });
            var subAttributes = getAttributeTypeSubAttributes(metadata, item);
            return acc.concat(subAttributes ? prepareItems(subAttributes, level + 1, chipLabel) : []);
        }, []);
    }, [metadata]);
    var options = useMemo(function () { return prepareItems((entityType === null || entityType === void 0 ? void 0 : entityType.attributes) || []); }, [entityType, prepareItems]);
    var CustomOption = function (_a) {
        var isFocused = _a.isFocused, isSelected = _a.isSelected, data = _a.data, label = _a.label, innerProps = _a.innerProps;
        return (React.createElement("div", __assign({}, innerProps),
            React.createElement(AttributeListItem, { checked: isSelected, level: data.level, data: data, label: label, labelInText: label, isFocused: isFocused })));
    };
    var CustomMultiValue = function (_a) {
        var data = _a.data, props = __rest(_a, ["data"]);
        return (React.createElement(ReactSelectMultiValue, __assign({}, props), data.chipLabel));
    };
    return (React.createElement("div", { className: classnames(styles.root, className), "data-reltio-id": "attribute-selector" },
        React.createElement(Typography, { className: styles.title, variant: 'subtitle1' }, title || i18n.text('Attribute')),
        React.createElement(MultiSelect, { value: selectedAttributes, options: options, TextFieldProps: {
                InputProps: { disableUnderline: disableUnderline },
                classes: { root: styles.dropDownInput }
            }, components: {
                DropdownIndicator: ReactSelectDropdownIndicatorWithIconButton,
                MultiValue: CustomMultiValue,
                Option: CustomOption
            }, onChange: onChange, styles: customStyles, classes: styles, menuPlacement: menuPlacement, menuPortalTarget: document.body, menuShouldBlockScroll: true, captureMenuScroll: false })));
};
