"use strict";
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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AttributeSelector = void 0;
var react_1 = __importStar(require("react"));
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var classnames_1 = __importDefault(require("classnames"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var Typography_1 = __importDefault(require("@mui/material/Typography"));
var MultiSelect_1 = require("../MultiSelect");
var ReactSelectMultiValue_1 = require("../ReactSelectMultiValue");
var ReactSelectDropdownIndicatorWithIconButton_1 = require("../ReactSelectDropdownIndicatorWithIconButton");
var AttributeListItem_1 = require("../AttributeListItem");
var MdmModuleContext_1 = require("../contexts/MdmModuleContext");
var styles_1 = require("./styles");
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 = (0, styles_1.useStyles)();
    var metadata = (0, MdmModuleContext_1.useMdmMetadata)();
    var prepareItems = (0, react_1.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 = (0, mdm_sdk_1.getAttributeTypeSubAttributes)(metadata, item);
            return acc.concat(subAttributes ? prepareItems(subAttributes, level + 1, chipLabel) : []);
        }, []);
    }, [metadata]);
    var options = (0, react_1.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_1.default.createElement("div", __assign({}, innerProps),
            react_1.default.createElement(AttributeListItem_1.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_1.default.createElement(ReactSelectMultiValue_1.ReactSelectMultiValue, __assign({}, props), data.chipLabel));
    };
    return (react_1.default.createElement("div", { className: (0, classnames_1.default)(styles.root, className), "data-reltio-id": "attribute-selector" },
        react_1.default.createElement(Typography_1.default, { className: styles.title, variant: 'subtitle1' }, title || ui_i18n_1.default.text('Attribute')),
        react_1.default.createElement(MultiSelect_1.MultiSelect, { value: selectedAttributes, options: options, TextFieldProps: {
                InputProps: { disableUnderline: disableUnderline },
                classes: { root: styles.dropDownInput }
            }, components: {
                DropdownIndicator: ReactSelectDropdownIndicatorWithIconButton_1.ReactSelectDropdownIndicatorWithIconButton,
                MultiValue: CustomMultiValue,
                Option: CustomOption
            }, onChange: onChange, styles: styles_1.customStyles, classes: styles, menuPlacement: menuPlacement, menuPortalTarget: document.body, menuShouldBlockScroll: true, captureMenuScroll: false })));
};
exports.AttributeSelector = AttributeSelector;
