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