import { allPass, always, equals, evolve, filter, has, identity, isNil, length, nth, pipe, prop, propEq, split, startsWith, unless, when } from 'ramda';
import i18n from 'ui-i18n';
import { DataTypes, DCROperationTypes, DCRTypes, EntityAttrTypes, getAttrDataTypeDefinition, getRoleLabel, isUrl } from '@reltio/mdm-sdk';
export var isPrimitiveValue = function (value) {
    return typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean';
};
export var getOperationType = function (type, newPinOrIgnoreValue) {
    switch (type) {
        case DCRTypes.CREATE_RELATIONSHIP:
        case DCRTypes.INSERT_ATTRIBUTE:
            return DCROperationTypes.ADDED;
        case DCRTypes.UPDATE_TAGS:
        case DCRTypes.UPDATE_ROLES:
        case DCRTypes.UPDATE_ATTRIBUTE:
            return DCROperationTypes.EDITED;
        case DCRTypes.DELETE_ATTRIBUTE:
        case DCRTypes.DELETE_RELATIONSHIP:
            return DCROperationTypes.DELETED;
        case DCRTypes.PIN_ATTRIBUTE:
            return newPinOrIgnoreValue ? DCROperationTypes.PINNED : DCROperationTypes.PIN_DELETED;
        case DCRTypes.IGNORE_ATTRIBUTE:
            return newPinOrIgnoreValue ? DCROperationTypes.IGNORED : DCROperationTypes.IGNORE_DELETED;
    }
};
export var getOperationLabel = function (operation) {
    var _a;
    var labels = (_a = {},
        _a[DCROperationTypes.ADDED] = i18n.text('added'),
        _a[DCROperationTypes.EDITED] = i18n.text('edited'),
        _a[DCROperationTypes.DELETED] = i18n.text('deleted'),
        _a[DCROperationTypes.PINNED] = i18n.text('pinned'),
        _a[DCROperationTypes.PIN_DELETED] = i18n.text('pin deleted'),
        _a[DCROperationTypes.IGNORED] = i18n.text('ignored'),
        _a[DCROperationTypes.IGNORE_DELETED] = i18n.text('ignore deleted'),
        _a);
    return labels[operation] || '';
};
export var getAttributeDescription = function (attrType, value, currentEntityUri, objectsInfo) {
    if (objectsInfo === void 0) { objectsInfo = {}; }
    var objectLabel, objectUri;
    var relation = objectsInfo[value.refObjectURI];
    if (relation) {
        objectUri = currentEntityUri === relation.startObjectUri ? relation.endObjectUri : relation.startObjectUri;
        objectLabel =
            currentEntityUri === relation.startObjectUri ? relation.endObjectLabel : relation.startObjectLabel;
    }
    else if (value.refEntity) {
        objectUri = value.refEntity.objectURI;
        objectLabel = objectsInfo[objectUri].label;
    }
    return {
        objectUri: objectUri,
        objectLabel: objectLabel
    };
};
export var getDataTypeDefinition = function (attributeType, attributeValue) {
    return when(allPass([pipe(prop('type'), equals(DataTypes.TYPE_STRING)), function () { return isUrl(attributeValue); }]), evolve({ type: always(DataTypes.TYPE_URL) }))(getAttrDataTypeDefinition(attributeType));
};
export var getSuggestionsCount = pipe(filter(has('operation')), length);
export var getDCRUri = function (objectUris) {
    if (objectUris === void 0) { objectUris = []; }
    return objectUris.find(startsWith('changeRequests/'));
};
export var getDCRId = function (objectUris) {
    if (objectUris === void 0) { objectUris = []; }
    return pipe(getDCRUri, unless(isNil, pipe(split('/'), nth(1))))(objectUris);
};
export var isOpenTask = function (isOpen) { return isOpen === undefined || isOpen === true; };
export var isRequestedTask = function (processTypes, assignee) { return function (task) {
    return (!processTypes || processTypes.includes(task.processType)) && (!assignee || assignee === task.assignee);
}; };
export var findWorkflowDefinitionByType = function (definitions, type) { return definitions.find(propEq('type', type)); };
export var prepareRolesOrTagsValue = function (metadata, value, type) {
    var getLabel = type === EntityAttrTypes.roles.uri ? getRoleLabel(metadata) : identity;
    return value.map(getLabel).join(', ');
};
