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);
};
import { formatDataTypeValue, findAttributeTypeByUri, makeAttrTypeUri, getAttrDataTypeDefinition } from '@reltio/mdm-sdk';
import { filter, groupBy, ifElse, map, path, pipe, propOr, values, isNil, includes, reject } from 'ramda';
import i18n from 'ui-i18n';
import { ActivityTypes } from '../types';
var getItemDataType = path(['data', 'type']);
export var getActivityType = function (activity) {
    var items = activity.items, label = activity.label;
    var firstItem = (items || [])[0];
    return getItemDataType(firstItem) || label;
};
export var isValidActivityType = function (type) { return pipe(values, includes(type))(ActivityTypes); };
export var removeIgnoredItems = function (activity) {
    var _a = activity.items, items = _a === void 0 ? [] : _a, label = activity.label;
    var isImplementedItem = function (item) { return isValidActivityType(getItemDataType(item) || label); };
    var filteredItems = filter(isImplementedItem)(items);
    if (items.length !== filteredItems.length) {
        console.warn('Activities not implemented:', reject(isImplementedItem)(items));
    }
    return __assign(__assign({}, activity), { items: filteredItems, itemsTotal: filteredItems.length });
};
export var splitActivityByType = function (activity) {
    return pipe(propOr([], 'items'), groupBy(getItemDataType), values, ifElse(function (_a) {
        var length = _a.length;
        return length > 1;
    }, map(function (items) { return (__assign(__assign({}, activity), { items: items, itemsTotal: items.length })); }), function () { return [activity]; }))(activity);
};
export var stringifyDeltaValue = function (deltaValue, attributeTypeUri, metadata, attributesPresentation) {
    var attributeType = findAttributeTypeByUri(metadata, attributeTypeUri);
    var dataTypeDefinition = getAttrDataTypeDefinition(attributeType);
    var formatValue = formatDataTypeValue({ attributesPresentation: attributesPresentation, dataTypeDefinition: dataTypeDefinition });
    if (isNil(deltaValue)) {
        return '';
    }
    if (typeof deltaValue === 'string') {
        return attributeType ? formatValue(deltaValue) : deltaValue;
    }
    return Object.entries(deltaValue)
        .flatMap(function (_a) {
        var attributeTypeName = _a[0], deltaValues = _a[1];
        return deltaValues.map(function (deltaValue) {
            return stringifyDeltaValue(deltaValue.value, makeAttrTypeUri(attributeTypeUri, attributeTypeName), metadata, attributesPresentation);
        });
    })
        .join(', ');
};
export var getActivityLabel = function (type) {
    var _a;
    var labels = (_a = {},
        _a[ActivityTypes.COMMENT_ADDED] = i18n.text('Comment added'),
        _a[ActivityTypes.COMMENT_DELETED] = i18n.text('Comment deleted'),
        _a[ActivityTypes.COMMENT_UPDATED] = i18n.text('Comment updated'),
        _a[ActivityTypes.ENTITIES_MERGED] = i18n.text('Profile merged'),
        _a[ActivityTypes.ENTITIES_MERGED_MANUALLY] = i18n.text('Profile merged manually'),
        _a[ActivityTypes.ENTITIES_MERGED_ON_THE_FLY] = i18n.text('Profile merged on the fly'),
        _a[ActivityTypes.ENTITIES_SPLITTED] = i18n.text('Profile un-merged'),
        _a[ActivityTypes.ENTITY_CHANGED] = i18n.text('Profile updated'),
        _a[ActivityTypes.ENTITY_CREATED] = i18n.text('Profile created'),
        _a[ActivityTypes.ENTITY_LOST_MERGE] = i18n.text('Profile lost merge'),
        _a[ActivityTypes.ENTITY_REMOVED] = i18n.text('Profile removed'),
        _a[ActivityTypes.GROUP_CHANGED] = i18n.text('Group updated'),
        _a[ActivityTypes.GROUP_CREATED] = i18n.text('Group created'),
        _a[ActivityTypes.GROUP_REMOVED] = i18n.text('Group removed'),
        _a[ActivityTypes.MODEL_UPDATED] = i18n.text('Model was updated'),
        _a[ActivityTypes.NOT_MATCHES_RESET] = i18n.text('Not matches reset'),
        _a[ActivityTypes.NOT_MATCHES_SET] = i18n.text('Not matches set'),
        _a[ActivityTypes.PERIODIC_TASK_SCHEDULED] = i18n.text('Periodic task was scheduled'),
        _a[ActivityTypes.POTENTIAL_MATCHES_FOUND] = i18n.text('Potential matches found'),
        _a[ActivityTypes.POTENTIAL_MATCHES_REMOVED] = i18n.text('Potential matches removed'),
        _a[ActivityTypes.RELATIONSHIP_CHANGED] = i18n.text('Relationship updated'),
        _a[ActivityTypes.RELATIONSHIP_CREATED] = i18n.text('Relationship created'),
        _a[ActivityTypes.RELATIONSHIP_REMOVED] = i18n.text('Relationship removed'),
        _a[ActivityTypes.USER_LOGIN] = i18n.text('Logged in'),
        _a[ActivityTypes.USER_LOGOUT] = i18n.text('Logged out'),
        _a[ActivityTypes.USER_PROFILE_VIEW] = i18n.text('Profile viewed'),
        _a[ActivityTypes.ATTRIBUTE_UNMASKED] = i18n.text('Attribute unmasked'),
        _a[ActivityTypes.USER_SEARCH] = i18n.text('Searched'),
        _a[ActivityTypes.ANALYTICS_ATTRIBUTES_CHANGED] = i18n.text('Analytics attribute changed'),
        _a[ActivityTypes.SFDC_CONNECTOR_SYNCHRONIZATION_ISSUE] = i18n.text('SFDC connector synchronization issue'),
        _a[ActivityTypes.SEGMENT_CREATE] = i18n.text('Segment created'),
        _a[ActivityTypes.SEGMENT_UPDATE] = i18n.text('Segment updated'),
        _a[ActivityTypes.SEGMENT_DELETE] = i18n.text('Segment deleted'),
        _a[ActivityTypes.INTERACTION_CREATE] = i18n.text('Interaction created'),
        _a[ActivityTypes.INTERACTION_UPDATE] = i18n.text('Interaction updated'),
        _a);
    return labels[type] || '';
};
