"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAddableAttributeTypes = void 0;
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var ramda_1 = require("ramda");
var getAddableAttributeTypes = function (parentType, _a) {
    var _b = _a === void 0 ? {} : _a, _c = _b.personalizationEnabled, personalizationEnabled = _c === void 0 ? false : _c, selectedAttributeTypes = _b.selectedAttributeTypes, entity = _b.entity, metadata = _b.metadata;
    var isNotSelected = function (attrType) { return !(selectedAttributeTypes === null || selectedAttributeTypes === void 0 ? void 0 : selectedAttributeTypes.find((0, ramda_1.propEq)('uri', attrType.uri))); };
    var canCreate = function (attributeType) { return (0, mdm_sdk_1.checkCanCreateAttribute)({ attributeType: attributeType, mode: mdm_sdk_1.Mode.Editing }); };
    var isVisibleInPicker = (0, ramda_1.either)(mdm_sdk_1.isNested, isNotSelected);
    var hasAttributeOnEntity = function (attributeType) {
        if (!entity || !metadata) {
            return false;
        }
        var values = (0, mdm_sdk_1.findAttributeValuesByTypeUri)(metadata, entity, attributeType.uri);
        return !(0, ramda_1.isNil)(values) && !(0, ramda_1.isEmpty)(values);
    };
    return (0, ramda_1.pipe)((0, ramda_1.propOr)([], 'attributes'), (0, ramda_1.ifElse)(function () { return personalizationEnabled; }, (0, ramda_1.pipe)((0, ramda_1.filter)(mdm_sdk_1.isReadableAttribute), (0, ramda_1.filter)((0, ramda_1.either)(canCreate, hasAttributeOnEntity)), (0, ramda_1.filter)(isVisibleInPicker)), (0, ramda_1.pipe)((0, ramda_1.reject)((0, ramda_1.either)(mdm_sdk_1.isReference, mdm_sdk_1.isImage)), (0, ramda_1.filter)(isVisibleInPicker), (0, ramda_1.filter)((0, ramda_1.both)(mdm_sdk_1.isReadableAttribute, canCreate)))))(parentType);
};
exports.getAddableAttributeTypes = getAddableAttributeTypes;
