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