"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useRelationTypeSelector = void 0;
var react_1 = require("react");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var ramda_1 = require("ramda");
var useRelationTypeSelector = function (_a) {
    var reversedContextLabels = _a.reversedContextLabels;
    var getRelationTypesOptions = (0, react_1.useCallback)(function (inRelations, outRelations) {
        var labelsMap = {};
        var addLabelInMap = function (label) { return (labelsMap[label] = (0, ramda_1.propOr)(0, label, labelsMap) + 1); };
        var getDirectionalLabel = (0, ramda_1.pipe)(mdm_sdk_1.getDirectionalLabelFromObject, (0, ramda_1.tap)(addLabelInMap));
        var byLabel = (0, ramda_1.ascend)((0, ramda_1.prop)('label'));
        var prepareRelation = (0, ramda_1.curry)(function (direction, _a) {
            var uri = _a.uri, label = _a.label, startObject = _a.startObject, endObject = _a.endObject;
            var dirObject = direction === (reversedContextLabels ? mdm_sdk_1.Directions.OUT : mdm_sdk_1.Directions.IN)
                ? startObject
                : endObject;
            return {
                label: { directionalLabel: getDirectionalLabel(dirObject), typeLabel: label },
                value: "".concat(uri, ",").concat(direction),
                startObject: startObject,
                endObject: endObject
            };
        });
        return inRelations
            .map(prepareRelation(mdm_sdk_1.Directions.IN))
            .concat(outRelations.map(prepareRelation(mdm_sdk_1.Directions.OUT)))
            .map((0, ramda_1.evolve)({
            label: function (_a) {
                var directionalLabel = _a.directionalLabel, typeLabel = _a.typeLabel;
                return directionalLabel
                    ? labelsMap[directionalLabel] > 1
                        ? "".concat(directionalLabel, " (").concat(typeLabel, ")")
                        : directionalLabel
                    : typeLabel;
            }
        }))
            .sort(byLabel);
    }, [reversedContextLabels]);
    var fromEditorValue = (0, react_1.useCallback)(function (inRelationTypes, outRelationTypes) { return function (relation) {
        var _a = relation.value.split(','), uri = _a[0], direction = _a[1];
        var type = (direction === mdm_sdk_1.Directions.IN ? inRelationTypes : outRelationTypes).find((0, ramda_1.propEq)('uri', uri));
        return { type: type, direction: direction };
    }; }, []);
    var toEditorValue = (0, react_1.useCallback)(function (relation, relationsTypesOptions) {
        return relationsTypesOptions.find((0, ramda_1.propEq)('value', "".concat(relation.relationType, ",").concat(relation.direction)));
    }, []);
    return {
        getRelationTypesOptions: getRelationTypesOptions,
        fromEditorValue: fromEditorValue,
        toEditorValue: toEditorValue
    };
};
exports.useRelationTypeSelector = useRelationTypeSelector;
