import { useCallback } from 'react';
import { isEmpty } from 'ramda';
import { useContextSelector } from '@fluentui/react-context-selector';
import { DCROperationTypes } from '@reltio/mdm-sdk';
import { ChangeRequestEditorContext } from '../../contexts/ChangeRequestEditorContext';
import { extractLineIds } from '../../helpers/merge';
import { isSimpleAttributeValue } from '../helpers/helpers';
var emptyLineIds = [];
export var useChangeRequestEditor = function (_a) {
    var change = _a.change;
    var lineIds = change.lineIds || extractLineIds(change === null || change === void 0 ? void 0 : change.newValue) || emptyLineIds;
    var lineData = useContextSelector(ChangeRequestEditorContext, function (context) { return context.getLineData(lineIds[0]); });
    var updateChange = useContextSelector(ChangeRequestEditorContext, function (context) { return context.updateChange; });
    var rejectChange = useContextSelector(ChangeRequestEditorContext, function (context) { return context.rejectChange; });
    var revertChangeUpdateOrReject = useContextSelector(ChangeRequestEditorContext, function (context) { return context.revertChangeUpdateOrReject; });
    var isParentLineRejected = useContextSelector(ChangeRequestEditorContext, function (context) {
        return context.getIsParentLineRejected(lineIds[0]);
    });
    var isUpdated = useContextSelector(ChangeRequestEditorContext, function (context) { return context.getIsLineUpdated(lineIds[0]); });
    var isLineRejected = lineData === null || lineData === void 0 ? void 0 : lineData.isRejected;
    var hasChangeId = lineIds.length > 0;
    var isSubAttributeOfDeletedAttribute = change.operation === DCROperationTypes.DELETED &&
        change.level > 0 &&
        lineIds.length > 0 &&
        lineIds[0].includes('/value/');
    var handleChange = useCallback(function (newValue) {
        updateChange(lineIds, isSimpleAttributeValue(newValue) ? newValue : { value: newValue });
    }, [lineIds, updateChange]);
    var handleReject = useCallback(function () {
        rejectChange(lineIds);
    }, [rejectChange, lineIds]);
    var handleRevert = useCallback(function () {
        revertChangeUpdateOrReject(lineIds);
    }, [revertChangeUpdateOrReject, lineIds]);
    var handleImageUpload = useCallback(function (value) {
        if (isEmpty(lineIds))
            return;
        updateChange(lineIds, value);
    }, [lineIds, updateChange]);
    return {
        handleChange: handleChange,
        handleReject: handleReject,
        handleRevert: handleRevert,
        handleImageUpload: handleImageUpload,
        lineData: lineData,
        isLineRejected: isLineRejected,
        isParentLineRejected: isParentLineRejected,
        isUpdated: isUpdated,
        hasChangeId: hasChangeId,
        isSubAttributeOfDeletedAttribute: isSubAttributeOfDeletedAttribute
    };
};
