"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useCrosswalkActions = void 0;
var react_1 = require("react");
var ramda_1 = require("ramda");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var useCrosswalkActions = function (_a) {
    var _b = _a.onLoad, onLoad = _b === void 0 ? ramda_1.identity : _b, _c = _a.onSuccess, onSuccess = _c === void 0 ? ramda_1.identity : _c, _d = _a.onError, onError = _d === void 0 ? ramda_1.identity : _d;
    var handleRequest = (0, react_1.useCallback)(function (request) {
        onLoad();
        request
            .then(function (_a) {
            var errors = _a.errors;
            if (errors)
                throw errors;
            onSuccess();
        })
            .catch(onError);
    }, [onLoad, onError, onSuccess]);
    var addCrosswalkAction = (0, react_1.useCallback)(function (_a) {
        var entity = _a.entity, contributorCrosswalk = _a.contributorCrosswalk, crosswalk = _a.crosswalk;
        var addRequest = contributorCrosswalk
            ? (0, mdm_sdk_1.addCrosswalkInContributor)({
                entityTypeUri: entity.type,
                contributorCrosswalkType: contributorCrosswalk.type,
                contributorCrosswalkValue: contributorCrosswalk.value,
                contributorCrosswalkSourceTable: contributorCrosswalk.sourceTable,
                crosswalkType: crosswalk.type,
                crosswalkValue: crosswalk.value,
                crosswalkSourceTable: crosswalk.sourceTable,
                crosswalkDeleteDate: crosswalk.deleteDate
            })
            : (0, mdm_sdk_1.addCrosswalk)({ entityUri: entity.uri, crosswalk: crosswalk });
        (0, ramda_1.pipe)((0, ramda_1.andThen)(function (_a) {
            var response = _a[0];
            return response;
        }), handleRequest)(addRequest);
    }, [handleRequest]);
    var editCrosswalkAction = (0, react_1.useCallback)(function (_a) {
        var entityUri = _a.entityUri, crosswalkId = _a.crosswalkId, attributeName = _a.attributeName, value = _a.value;
        (0, ramda_1.pipe)((0, ramda_1.ifElse)(function (_a) {
            var value = _a.value;
            return (0, ramda_1.isNil)(value);
        }, mdm_sdk_1.deleteCrosswalkAttribute, mdm_sdk_1.editCrosswalkAttribute), (0, ramda_1.andThen)(function (_a) {
            var response = _a[0];
            return response;
        }), handleRequest)({
            entityUri: entityUri,
            crosswalkId: crosswalkId,
            attributeName: attributeName,
            value: value
        });
    }, [handleRequest]);
    var deleteCrosswalkAction = (0, react_1.useCallback)(function (_a) {
        var entityUri = _a.entityUri, crosswalkId = _a.crosswalkId;
        (0, ramda_1.pipe)(mdm_sdk_1.deleteCrosswalk, handleRequest)({
            entityUri: entityUri,
            crosswalkId: crosswalkId
        });
    }, [handleRequest]);
    return {
        addCrosswalk: addCrosswalkAction,
        editCrosswalk: editCrosswalkAction,
        deleteCrosswalk: deleteCrosswalkAction
    };
};
exports.useCrosswalkActions = useCrosswalkActions;
