"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 editors_1 = require("../editors");
var merge_1 = require("../../../helpers/merge");
var ErrorWrapper_1 = require("../../../../../ErrorWrapper");
var ScrollToElementContext_1 = require("../../../../../contexts/ScrollToElementContext");
var useScrollToAttributeError_1 = require("../../../../../hooks/useScrollToAttributeError");
var addedAttributes_1 = require("../../helpers/addedAttributes");
var ScrollToAddedAttributeContext_1 = require("../../../contexts/ScrollToAddedAttributeContext");
var ChangeItemRow_module_css_1 = __importDefault(require("./ChangeItemRow.module.css"));
var ChangeItemRow = function (_a) {
    var _b;
    var _c;
    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 lineData = (0, react_context_selector_1.useContextSelector)(ChangeRequestEditorContext_1.ChangeRequestEditorContext, function (context) { return context.getLineData(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 _d = (0, useScrollToAttributeError_1.useScrollToAttributeError)({
        highlightedError: isHighlighted ? scrollToElementContext : null,
        className: ChangeItemRow_module_css_1.default.changeItemRowWithError
    }), ref = _d.ref, errorClassName = _d.errorClassName;
    var addedId = (0, addedAttributes_1.getAddedId)(change);
    var _e = (0, react_1.useContext)(ScrollToAddedAttributeContext_1.ScrollToAddedAttributeContext), highlightedIds = _e.highlightedIds, scrollToFirst = _e.scrollToFirst;
    var isHighlightedAdded = !!addedId && highlightedIds.has(addedId);
    var scrollRef = (0, react_1.useRef)(null);
    (0, react_1.useEffect)(function () {
        if (isHighlightedAdded) {
            scrollToFirst(scrollRef);
        }
    }, [isHighlightedAdded, scrollToFirst]);
    var combinedRef = function (node) {
        ref.current = node;
        scrollRef.current = node;
    };
    var isEditedOperation = change.operation === mdm_sdk_1.DCROperationTypes.EDITED;
    var editable = (0, helpers_1.isEditableChange)(change, entityInfo);
    var isAdded = (0, addedAttributes_1.isAddedAttribute)(change);
    var currentValue = (_c = lineData === null || lineData === void 0 ? void 0 : lineData.value) !== null && _c !== void 0 ? _c : change.newValue;
    var hasValue = !!(0, mdm_sdk_1.getAttributeValue)(currentValue);
    var isAddedWithValue = isAdded && hasValue;
    return (react_1.default.createElement("div", { ref: combinedRef, className: (0, classnames_1.default)(ChangeItemRow_module_css_1.default.changeItemRow, errorClassName, (_b = {},
            _b[ChangeItemRow_module_css_1.default.changedRow] = isUpdated && !isAdded,
            _b[ChangeItemRow_module_css_1.default.addedWithValueRow] = isAddedWithValue,
            _b[ChangeItemRow_module_css_1.default.rejectedRow] = isLineRejected || isParentLineRejected,
            _b[ChangeItemRow_module_css_1.default.highlightedRow] = isHighlightedAdded,
            _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 } }, isAdded ? (react_1.default.createElement(editors_1.ChangeItemEditorRecentlyAdded, { change: change, editable: editable })) : (react_1.default.createElement(react_1.default.Fragment, null,
            isEditedOperation && react_1.default.createElement(editors_1.ChangeItemOldValue, { change: change }),
            react_1.default.createElement(editors_1.ChangeItemEditor, { change: change, editable: editable, classes: { rejectButton: ChangeItemRow_module_css_1.default.rejectButton } }))))));
};
exports.ChangeItemRow = ChangeItemRow;
