"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChangeItemRow = void 0;
var classnames_1 = __importDefault(require("classnames"));
var react_1 = __importStar(require("react"));
var react_context_selector_1 = require("@fluentui/react-context-selector");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var helpers_1 = require("../../helpers/helpers");
var ChangeRequestEditorContext_1 = require("../../../contexts/ChangeRequestEditorContext");
var ChangeItemEditor_1 = require("../ChangeItemEditor");
var merge_1 = require("../../../helpers/merge");
var ErrorWrapper_1 = require("../../../../../ErrorWrapper");
var ScrollToElementContext_1 = require("../../../../../contexts/ScrollToElementContext");
var useScrollToAttributeError_1 = require("../../../../../hooks/useScrollToAttributeError");
var ChangeItemRow_module_css_1 = __importDefault(require("./ChangeItemRow.module.css"));
var ChangeItemRow = function (_a) {
    var _b;
    var change = _a.change, entityInfo = _a.entityInfo;
    var lineIds = change.lineIds || (0, merge_1.extractLineIds)(change === null || change === void 0 ? void 0 : change.newValue);
    var isParentLineRejected = (0, react_context_selector_1.useContextSelector)(ChangeRequestEditorContext_1.ChangeRequestEditorContext, function (context) {
        return context.getIsParentLineRejected(lineIds === null || lineIds === void 0 ? void 0 : lineIds[0]);
    });
    var isLineRejected = (0, react_context_selector_1.useContextSelector)(ChangeRequestEditorContext_1.ChangeRequestEditorContext, function (context) {
        return context.getIsLineRejected(lineIds === null || lineIds === void 0 ? void 0 : lineIds[0]);
    });
    var isUpdated = (0, react_context_selector_1.useContextSelector)(ChangeRequestEditorContext_1.ChangeRequestEditorContext, function (context) {
        return context.getIsLineUpdated(lineIds === null || lineIds === void 0 ? void 0 : lineIds[0]);
    });
    var errorByLineId = (0, react_context_selector_1.useContextSelector)(ChangeRequestEditorContext_1.ChangeRequestEditorContext, function (context) {
        return context.getErrorByLineId(lineIds === null || lineIds === void 0 ? void 0 : lineIds[0]);
    });
    var error = errorByLineId === null || errorByLineId === void 0 ? void 0 : errorByLineId.error;
    var errorLineIds = (errorByLineId === null || errorByLineId === void 0 ? void 0 : errorByLineId.lineIds) || [];
    var scrollToElementContext = (0, react_1.useContext)(ScrollToElementContext_1.ScrollToElementContext);
    var highlightedError = scrollToElementContext === null || scrollToElementContext === void 0 ? void 0 : scrollToElementContext.element;
    var isHighlighted = (0, ScrollToElementContext_1.isHighlightedErrorType)(scrollToElementContext === null || scrollToElementContext === void 0 ? void 0 : scrollToElementContext.type) &&
        highlightedError &&
        errorLineIds.includes(highlightedError.uri);
    var _c = (0, useScrollToAttributeError_1.useScrollToAttributeError)({
        highlightedError: isHighlighted ? scrollToElementContext : null,
        className: ChangeItemRow_module_css_1.default.changeItemRowWithError
    }), ref = _c.ref, errorClassName = _c.errorClassName;
    var isEditedOperation = change.operation === mdm_sdk_1.DCROperationTypes.EDITED;
    var editable = (0, helpers_1.isEditableChange)(change, entityInfo);
    return (react_1.default.createElement("div", { ref: ref, className: (0, classnames_1.default)(ChangeItemRow_module_css_1.default.changeItemRow, errorClassName, (_b = {},
            _b[ChangeItemRow_module_css_1.default.editableRow] = editable,
            _b[ChangeItemRow_module_css_1.default.changedRow] = isUpdated,
            _b[ChangeItemRow_module_css_1.default.rejectedRow] = isLineRejected || isParentLineRejected,
            _b)) },
        react_1.default.createElement(ErrorWrapper_1.ErrorWrapper, { errorMessage: error === null || error === void 0 ? void 0 : error.message, classes: { helperText: ChangeItemRow_module_css_1.default.errorText } },
            isEditedOperation && react_1.default.createElement(ChangeItemEditor_1.ChangeItemOldValue, { change: change }),
            react_1.default.createElement(ChangeItemEditor_1.ChangeItemEditor, { change: change, editable: editable, classes: { rejectButton: ChangeItemRow_module_css_1.default.rejectButton } }))));
};
exports.ChangeItemRow = ChangeItemRow;
