"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useChangeRequestEditor = void 0;
var react_1 = require("react");
var react_context_selector_1 = require("@fluentui/react-context-selector");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var ChangeRequestEditorContext_1 = require("../../contexts/ChangeRequestEditorContext");
var merge_1 = require("../../helpers/merge");
var emptyLineIds = [];
var useChangeRequestEditor = function (_a) {
    var change = _a.change;
    var lineIds = change.lineIds || (0, merge_1.extractLineIds)(change === null || change === void 0 ? void 0 : change.newValue) || emptyLineIds;
    var lineData = (0, react_context_selector_1.useContextSelector)(ChangeRequestEditorContext_1.ChangeRequestEditorContext, function (context) { return context.getLineData(lineIds[0]); });
    var updateChange = (0, react_context_selector_1.useContextSelector)(ChangeRequestEditorContext_1.ChangeRequestEditorContext, function (context) { return context.updateChange; });
    var rejectChange = (0, react_context_selector_1.useContextSelector)(ChangeRequestEditorContext_1.ChangeRequestEditorContext, function (context) { return context.rejectChange; });
    var revertChangeUpdateOrReject = (0, react_context_selector_1.useContextSelector)(ChangeRequestEditorContext_1.ChangeRequestEditorContext, function (context) { return context.revertChangeUpdateOrReject; });
    var isParentLineRejected = (0, react_context_selector_1.useContextSelector)(ChangeRequestEditorContext_1.ChangeRequestEditorContext, function (context) {
        return context.getIsParentLineRejected(lineIds[0]);
    });
    var isUpdated = (0, react_context_selector_1.useContextSelector)(ChangeRequestEditorContext_1.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 === mdm_sdk_1.DCROperationTypes.DELETED &&
        change.level > 0 &&
        lineIds.length > 0 &&
        lineIds[0].includes('/value/');
    var handleChange = (0, react_1.useCallback)(function (newValue) {
        updateChange(lineIds, typeof newValue === 'object' && 'value' in newValue ? newValue : { value: newValue });
    }, [lineIds, updateChange]);
    var handleReject = (0, react_1.useCallback)(function () {
        rejectChange(lineIds);
    }, [rejectChange, lineIds]);
    var handleRevert = (0, react_1.useCallback)(function () {
        revertChangeUpdateOrReject(lineIds);
    }, [revertChangeUpdateOrReject, lineIds]);
    return {
        handleChange: handleChange,
        handleReject: handleReject,
        handleRevert: handleRevert,
        lineData: lineData,
        isLineRejected: isLineRejected,
        isParentLineRejected: isParentLineRejected,
        isUpdated: isUpdated,
        hasChangeId: hasChangeId,
        isSubAttributeOfDeletedAttribute: isSubAttributeOfDeletedAttribute
    };
};
exports.useChangeRequestEditor = useChangeRequestEditor;
