"use strict";
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var ramda_1 = require("ramda");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var MoreAttributesButton_1 = require("../../../../../MoreAttributesButton");
var withTableContext_1 = require("../../../../../HOCs/withTableContext");
var MdmModuleContext_1 = require("../../../../../contexts/MdmModuleContext");
var getAddableAttributeTypes_1 = require("./getAddableAttributeTypes");
var styles_1 = require("./styles");
var AddAttributesButton = function (_a) {
    var parentTypeUri = _a.parentTypeUri, parentUri = _a.parentUri, entity = _a.entity, selectedAttributeTypes = _a.selectedAttributeTypes, onSelectAttributeTypes = _a.onSelectAttributeTypes, onAdd = _a.onAdd, _b = _a.personalizationEnabled, personalizationEnabled = _b === void 0 ? false : _b;
    var styles = (0, styles_1.useStyles)();
    var metadata = (0, MdmModuleContext_1.useMdmMetadata)();
    var parentType = __spreadArray(__spreadArray([], metadata.entityTypes, true), (metadata.relationTypes || []), true).find((0, ramda_1.propEq)('uri', parentTypeUri));
    var isNotSelected = function (attrType) { return !(selectedAttributeTypes === null || selectedAttributeTypes === void 0 ? void 0 : selectedAttributeTypes.find((0, ramda_1.propEq)('uri', attrType.uri))); };
    var data = (0, getAddableAttributeTypes_1.getAddableAttributeTypes)(parentType, {
        personalizationEnabled: personalizationEnabled,
        selectedAttributeTypes: selectedAttributeTypes,
        entity: entity,
        metadata: metadata
    });
    var handleAdd = function (addedTypes) {
        var typesToSelect = addedTypes.filter(isNotSelected);
        if (typesToSelect.length > 0) {
            onSelectAttributeTypes(__spreadArray(__spreadArray([], typesToSelect, true), selectedAttributeTypes, true));
        }
        addedTypes.filter(mdm_sdk_1.isNested).forEach(function (attributeType) {
            onAdd({ attributeType: attributeType, parentUri: parentUri });
        });
    };
    return (react_1.default.createElement("div", { className: styles.root },
        react_1.default.createElement(MoreAttributesButton_1.MoreAttributesButton, { label: ui_i18n_1.default.text('Attribute'), popupTitle: ui_i18n_1.default.text('Add attributes'), data: data, onApply: handleAdd })));
};
exports.default = (0, withTableContext_1.withTableContext)((0, ramda_1.pick)([
    'parentUri',
    'parentTypeUri',
    'entity',
    'onAdd',
    'selectedAttributeTypes',
    'onSelectAttributeTypes',
    'personalizationEnabled'
]))(AddAttributesButton);
