"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 __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));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAttributeSelectorItems = exports.getOperationalTypeUriFromAttrTypes = void 0;
var ramda_1 = require("ramda");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var createAttributeItem = function (metadata, attrType, operationalTypeUri, parents) {
    var title = attrType.label || attrType.name;
    if ((0, mdm_sdk_1.isSpecialAttribute)(attrType)) {
        return { attrType: attrType, title: title, fieldName: attrType.name };
    }
    if ((0, mdm_sdk_1.isInteractionTypeUri)(operationalTypeUri)) {
        return { attrType: attrType, title: title, fieldName: (0, mdm_sdk_1.attributeUriToSearchUri)(attrType.uri, parents) };
    }
    var entityTypeLabel = (0, mdm_sdk_1.getEntityTypeLabel)(metadata, { type: operationalTypeUri });
    return {
        attrType: attrType,
        title: title,
        entityTypeUri: operationalTypeUri,
        pathToTitle: Array.isArray(parents)
            ? __spreadArray(__spreadArray([entityTypeLabel], (0, ramda_1.chain)((0, ramda_1.prop)('label'), parents), true), [title], false) : [entityTypeLabel, title],
        fieldName: (0, mdm_sdk_1.attributeUriToSearchUri)(attrType.uri, parents)
    };
};
var createAttributesConfig = function (metadata, attrNode, operationalTypeUri, parents) {
    if (parents === void 0) { parents = []; }
    var resConfig = __assign(__assign({}, createAttributeItem(metadata, attrNode, operationalTypeUri, parents)), { uri: attrNode.uri });
    var children = attrNode.children
        ? attrNode.children.map(function (childNode) {
            return createAttributesConfig(metadata, childNode, operationalTypeUri, parents.concat(attrNode));
        })
        : [];
    if (children.length) {
        resConfig.children = children;
    }
    return resConfig;
};
var getOperationalTypeUriFromAttrTypes = function (attributeTypes) {
    var regularAttribute = (0, mdm_sdk_1.findRegularAttribute)(attributeTypes);
    return regularAttribute ? (0, mdm_sdk_1.getBaseUri)(regularAttribute.uri) : null;
};
exports.getOperationalTypeUriFromAttrTypes = getOperationalTypeUriFromAttrTypes;
var getAttributeSelectorItems = function (metadata, attributesTypesGroups, attributesTypesGroupFilter) {
    var attributeSelectorItemsGroup = attributesTypesGroups.map(function (_a) {
        var id = _a.id, title = _a.title, attributeTypes = _a.attributeTypes;
        var operationalTypeUri = (0, exports.getOperationalTypeUriFromAttrTypes)(attributeTypes);
        return {
            id: id,
            title: title,
            attributes: (0, mdm_sdk_1.getAttributesTreeFromAttributes)(attributeTypes, metadata).map(function (attrNode) {
                var uri = (0, mdm_sdk_1.isSpecialAttribute)(attrNode) ? null : operationalTypeUri;
                return createAttributesConfig(metadata, attrNode, uri);
            })
        };
    });
    return attributesTypesGroupFilter
        ? attributesTypesGroupFilter(attributeSelectorItemsGroup)
        : attributeSelectorItemsGroup;
};
exports.getAttributeSelectorItems = getAttributeSelectorItems;
