"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isDefaultFilter = exports.buildHistoryFilterString = void 0;
var ramda_1 = require("ramda");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var types_1 = require("../../activity-log/types");
var types_2 = require("../types");
var constants_1 = require("../constants");
var convertHistoryTypes = (0, ramda_1.chain)((0, ramda_1.cond)([
    [
        (0, ramda_1.equals)(types_2.HistoryActivityType.MERGE),
        function () { return [
            types_1.ActivityTypes.ENTITIES_MERGED,
            types_1.ActivityTypes.ENTITIES_MERGED_MANUALLY,
            types_1.ActivityTypes.ENTITIES_MERGED_ON_THE_FLY
        ]; }
    ],
    [(0, ramda_1.equals)(types_2.HistoryActivityType.UNMERGE), function () { return [types_1.ActivityTypes.ENTITIES_SPLITTED]; }],
    [(0, ramda_1.equals)(types_2.HistoryActivityType.UPDATE), function () { return [types_1.ActivityTypes.ENTITY_CHANGED]; }]
]));
var buildActivityFilterClause = (0, ramda_1.pipe)((0, ramda_1.defaultTo)([]), convertHistoryTypes, (0, ramda_1.concat)([types_1.ActivityTypes.ENTITY_CREATED, types_1.ActivityTypes.ENTITY_REMOVED, types_1.ActivityTypes.ENTITY_LOST_MERGE]), (0, mdm_sdk_1.multiValueFilter)(function (value) { return "equals(type, '".concat(value, "')"); }));
var buildAttributeFilterClause = function (attribute) { return "changes(".concat((0, mdm_sdk_1.escapeQueryValue)(attribute.value), ")"); };
exports.buildHistoryFilterString = (0, ramda_1.pipe)((0, ramda_1.defaultTo)({}), mdm_sdk_1.rejectEmptyValues, (0, ramda_1.evolve)({
    users: (0, mdm_sdk_1.multiValueFilter)(mdm_sdk_1.buildUserFilterClause),
    attributes: (0, mdm_sdk_1.multiValueFilter)(buildAttributeFilterClause),
    dateRange: mdm_sdk_1.buildDateRangeFilterClause
}), (0, ramda_1.over)((0, ramda_1.lensProp)('activities'), buildActivityFilterClause), ramda_1.values, mdm_sdk_1.rejectEmptyValues, (0, ramda_1.append)("not equals(user, 'collaboration-service')"), (0, ramda_1.map)(mdm_sdk_1.wrapInBrackets), (0, ramda_1.join)(' and '));
var isDefaultFilter = function (filter) {
    return (0, ramda_1.pipe)((0, ramda_1.evolve)({ activities: (0, ramda_1.difference)(constants_1.DEFAULT_HISTORY_FILTER.activities) }), (0, ramda_1.reject)((0, ramda_1.either)(ramda_1.isNil, ramda_1.isEmpty)), ramda_1.isEmpty)(filter || {});
};
exports.isDefaultFilter = isDefaultFilter;
