"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.prepareRolesOrTagsValue = exports.findWorkflowDefinitionByType = exports.isRequestedTask = exports.isOpenTask = exports.getDCRId = exports.getDCRUri = exports.getSuggestionsCount = exports.getDataTypeDefinition = exports.getAttributeDescription = exports.getOperationLabel = exports.getOperationType = exports.isPrimitiveValue = void 0;
var ramda_1 = require("ramda");
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var isPrimitiveValue = function (value) {
    return typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean';
};
exports.isPrimitiveValue = isPrimitiveValue;
var getOperationType = function (type, newPinOrIgnoreValue) {
    switch (type) {
        case mdm_sdk_1.DCRTypes.CREATE_RELATIONSHIP:
        case mdm_sdk_1.DCRTypes.INSERT_ATTRIBUTE:
            return mdm_sdk_1.DCROperationTypes.ADDED;
        case mdm_sdk_1.DCRTypes.UPDATE_TAGS:
        case mdm_sdk_1.DCRTypes.UPDATE_ROLES:
        case mdm_sdk_1.DCRTypes.UPDATE_ATTRIBUTE:
            return mdm_sdk_1.DCROperationTypes.EDITED;
        case mdm_sdk_1.DCRTypes.DELETE_ATTRIBUTE:
        case mdm_sdk_1.DCRTypes.DELETE_RELATIONSHIP:
            return mdm_sdk_1.DCROperationTypes.DELETED;
        case mdm_sdk_1.DCRTypes.PIN_ATTRIBUTE:
            return newPinOrIgnoreValue ? mdm_sdk_1.DCROperationTypes.PINNED : mdm_sdk_1.DCROperationTypes.PIN_DELETED;
        case mdm_sdk_1.DCRTypes.IGNORE_ATTRIBUTE:
            return newPinOrIgnoreValue ? mdm_sdk_1.DCROperationTypes.IGNORED : mdm_sdk_1.DCROperationTypes.IGNORE_DELETED;
    }
};
exports.getOperationType = getOperationType;
var getOperationLabel = function (operation) {
    var _a;
    var labels = (_a = {},
        _a[mdm_sdk_1.DCROperationTypes.ADDED] = ui_i18n_1.default.text('added'),
        _a[mdm_sdk_1.DCROperationTypes.EDITED] = ui_i18n_1.default.text('edited'),
        _a[mdm_sdk_1.DCROperationTypes.DELETED] = ui_i18n_1.default.text('deleted'),
        _a[mdm_sdk_1.DCROperationTypes.PINNED] = ui_i18n_1.default.text('pinned'),
        _a[mdm_sdk_1.DCROperationTypes.PIN_DELETED] = ui_i18n_1.default.text('pin deleted'),
        _a[mdm_sdk_1.DCROperationTypes.IGNORED] = ui_i18n_1.default.text('ignored'),
        _a[mdm_sdk_1.DCROperationTypes.IGNORE_DELETED] = ui_i18n_1.default.text('ignore deleted'),
        _a);
    return labels[operation] || '';
};
exports.getOperationLabel = getOperationLabel;
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
    };
};
exports.getAttributeDescription = getAttributeDescription;
var getDataTypeDefinition = function (attributeType, attributeValue) {
    return (0, ramda_1.when)((0, ramda_1.allPass)([(0, ramda_1.pipe)((0, ramda_1.prop)('type'), (0, ramda_1.equals)(mdm_sdk_1.DataTypes.TYPE_STRING)), function () { return (0, mdm_sdk_1.isUrl)(attributeValue); }]), (0, ramda_1.evolve)({ type: (0, ramda_1.always)(mdm_sdk_1.DataTypes.TYPE_URL) }))((0, mdm_sdk_1.getAttrDataTypeDefinition)(attributeType));
};
exports.getDataTypeDefinition = getDataTypeDefinition;
exports.getSuggestionsCount = (0, ramda_1.pipe)((0, ramda_1.filter)((0, ramda_1.has)('operation')), ramda_1.length);
var getDCRUri = function (objectUris) {
    if (objectUris === void 0) { objectUris = []; }
    return objectUris.find((0, ramda_1.startsWith)('changeRequests/'));
};
exports.getDCRUri = getDCRUri;
var getDCRId = function (objectUris) {
    if (objectUris === void 0) { objectUris = []; }
    return (0, ramda_1.pipe)(exports.getDCRUri, (0, ramda_1.unless)(ramda_1.isNil, (0, ramda_1.pipe)((0, ramda_1.split)('/'), (0, ramda_1.nth)(1))))(objectUris);
};
exports.getDCRId = getDCRId;
var isOpenTask = function (isOpen) { return isOpen === undefined || isOpen === true; };
exports.isOpenTask = isOpenTask;
var isRequestedTask = function (processTypes, assignee) { return function (task) {
    return (!processTypes || processTypes.includes(task.processType)) && (!assignee || assignee === task.assignee);
}; };
exports.isRequestedTask = isRequestedTask;
var findWorkflowDefinitionByType = function (definitions, type) { return definitions.find((0, ramda_1.propEq)('type', type)); };
exports.findWorkflowDefinitionByType = findWorkflowDefinitionByType;
var prepareRolesOrTagsValue = function (metadata, value, type) {
    var getLabel = type === mdm_sdk_1.EntityAttrTypes.roles.uri ? (0, mdm_sdk_1.getRoleLabel)(metadata) : ramda_1.identity;
    return value.map(getLabel).join(', ');
};
exports.prepareRolesOrTagsValue = prepareRolesOrTagsValue;
