"use strict";
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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RolesInput = void 0;
var react_1 = __importStar(require("react"));
var ramda_1 = require("ramda");
var classnames_1 = __importDefault(require("classnames"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var SimpleDropDownSelector_1 = require("../SimpleDropDownSelector");
var MultiValueChip_1 = require("../MultiValueChip");
var MdmModuleContext_1 = require("../contexts/MdmModuleContext");
var RolesInput_module_css_1 = __importDefault(require("./RolesInput.module.css"));
var components = { MultiValue: MultiValueChip_1.MultiValueChip };
var RolesInput = function (_a) {
    var value = _a.value, onChange = _a.onChange, className = _a.className, _b = _a.disabled, disabled = _b === void 0 ? false : _b;
    var metadata = (0, MdmModuleContext_1.useMdmMetadata)();
    var entity = (0, MdmModuleContext_1.useMdmEntity)();
    var options = (0, react_1.useMemo)(function () {
        return (0, mdm_sdk_1.getRolesForEntityType)(metadata, entity.type).map(function (role) { return ({
            value: role.uri,
            label: role.label
        }); });
    }, [metadata, entity.type]);
    var formattedValues = (0, react_1.useMemo)(function () {
        return (value || []).map(function (value) {
            var _a;
            var label = (_a = options.find((0, ramda_1.propEq)('value', value))) === null || _a === void 0 ? void 0 : _a.label;
            return {
                value: value,
                label: label || (0, mdm_sdk_1.getLastUriPart)(value)
            };
        });
    }, [value, options]);
    return (react_1.default.createElement(SimpleDropDownSelector_1.SimpleDropDownSelector, { label: '', height: 40, isMulti: true, options: options, value: formattedValues, onChange: onChange, components: components, classes: RolesInput_module_css_1.default, className: (0, classnames_1.default)(RolesInput_module_css_1.default.roleContainer, className), disabled: disabled }));
};
exports.RolesInput = RolesInput;
