import { append, cond, defaultTo, equals, identity, join, map, pipe, toPairs } from 'ramda';
import { ActivityTypes } from '../types';
import { rejectEmptyValues, wrapInBrackets, multiValueFilter, buildUserFilterClause, buildSourceFilterClause, buildEntityTypeFilterClause, buildDateRangeFilterClause, buildEntityUriFilterClause } from '@reltio/mdm-sdk';
var buildActivityFilterClause = function (activityType) {
    if (activityType.startsWith('USER_') ||
        activityType.startsWith('COMMENT_') ||
        activityType.startsWith('SFDC_') ||
        activityType === ActivityTypes.ATTRIBUTE_UNMASKED) {
        return "startsWith(label, '".concat(activityType, "')");
    }
    else {
        return "equals(items.data.type, ".concat(activityType, ")");
    }
};
var buildAttributeFilterClause = function (attribute) {
    return "equals(items.delta.attributeType, '".concat(attribute.value, "')");
};
var buildSegmentFilterClause = function (segmentId) {
    return "equals(items.objectUri, 'segments/".concat(segmentId, "')");
};
var getFilterClauseBuilder = cond([
    [equals('users'), function () { return multiValueFilter(buildUserFilterClause); }],
    [equals('activities'), function () { return multiValueFilter(buildActivityFilterClause); }],
    [equals('sources'), function () { return multiValueFilter(buildSourceFilterClause); }],
    [equals('attributes'), function () { return multiValueFilter(buildAttributeFilterClause); }],
    [equals('entityTypes'), function () { return multiValueFilter(buildEntityTypeFilterClause); }],
    [equals('dateRange'), function () { return buildDateRangeFilterClause; }],
    [equals('entityUri'), function () { return buildEntityUriFilterClause; }],
    [equals('segmentId'), function () { return buildSegmentFilterClause; }],
    [equals('rawFilter'), function () { return identity; }]
]);
export var buildActivitiesFilterString = pipe(defaultTo({}), rejectEmptyValues, toPairs, append(['rawFilter', "not equals(user, 'collaboration-service')"]), map(function (_a) {
    var key = _a[0], value = _a[1];
    return getFilterClauseBuilder(key)(value);
}), rejectEmptyValues, map(wrapInBrackets), join(' and '));
