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));
};
import { chain, prop } from 'ramda';
import { attributeUriToSearchUri, findRegularAttribute, getAttributesTreeFromAttributes, getBaseUri, getEntityTypeLabel, isInteractionTypeUri, isSpecialAttribute } from '@reltio/mdm-sdk';
var createAttributeItem = function (metadata, attrType, operationalTypeUri, parents) {
    var title = attrType.label || attrType.name;
    if (isSpecialAttribute(attrType)) {
        return { attrType: attrType, title: title, fieldName: attrType.name };
    }
    if (isInteractionTypeUri(operationalTypeUri)) {
        return { attrType: attrType, title: title, fieldName: attributeUriToSearchUri(attrType.uri, parents) };
    }
    var entityTypeLabel = getEntityTypeLabel(metadata, { type: operationalTypeUri });
    return {
        attrType: attrType,
        title: title,
        entityTypeUri: operationalTypeUri,
        pathToTitle: Array.isArray(parents)
            ? __spreadArray(__spreadArray([entityTypeLabel], chain(prop('label'), parents), true), [title], false) : [entityTypeLabel, title],
        fieldName: 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;
};
export var getOperationalTypeUriFromAttrTypes = function (attributeTypes) {
    var regularAttribute = findRegularAttribute(attributeTypes);
    return regularAttribute ? getBaseUri(regularAttribute.uri) : null;
};
export var getAttributeSelectorItems = function (metadata, attributesTypesGroups, attributesTypesGroupFilter) {
    var attributeSelectorItemsGroup = attributesTypesGroups.map(function (_a) {
        var id = _a.id, title = _a.title, attributeTypes = _a.attributeTypes;
        var operationalTypeUri = getOperationalTypeUriFromAttrTypes(attributeTypes);
        return {
            id: id,
            title: title,
            attributes: getAttributesTreeFromAttributes(attributeTypes, metadata).map(function (attrNode) {
                var uri = isSpecialAttribute(attrNode) ? null : operationalTypeUri;
                return createAttributesConfig(metadata, attrNode, uri);
            })
        };
    });
    return attributesTypesGroupFilter
        ? attributesTypesGroupFilter(attributeSelectorItemsGroup)
        : attributeSelectorItemsGroup;
};
