"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useNewAttributesEditor = void 0;
var react_1 = require("react");
var react_context_selector_1 = require("@fluentui/react-context-selector");
var ChangeRequestEditorContext_1 = require("../../contexts/ChangeRequestEditorContext");
var merge_1 = require("../../helpers/merge");
var helpers_1 = require("../helpers/helpers");
var addedAttributes_1 = require("../helpers/addedAttributes");
var AddedAttributesContext_1 = require("../../contexts/AddedAttributesContext");
var emptyLineIds = [];
var useNewAttributesEditor = function (_a) {
    var change = _a.change;
    var addedId = (0, addedAttributes_1.getAddedId)(change);
    var hasAddedParent = (0, addedAttributes_1.hasParentAddedAttribute)(addedId);
    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 isLineRejected = (0, react_context_selector_1.useContextSelector)(ChangeRequestEditorContext_1.ChangeRequestEditorContext, function (context) {
        return context.getIsLineRejected(lineIds[0]);
    });
    var isParentLineRejected = (0, react_context_selector_1.useContextSelector)(ChangeRequestEditorContext_1.ChangeRequestEditorContext, function (context) {
        return context.getIsParentLineRejected(lineIds[0]);
    });
    var isRejected = isLineRejected || isParentLineRejected;
    var deleteLineData = (0, react_context_selector_1.useContextSelector)(ChangeRequestEditorContext_1.ChangeRequestEditorContext, function (ctx) { return ctx.deleteLineData; });
    var removeAddedAttribute = (0, react_context_selector_1.useContextSelector)(AddedAttributesContext_1.AddedAttributesContext, function (ctx) { return ctx.removeAddedAttribute; });
    var updateAddedAttributeRefEntity = (0, react_context_selector_1.useContextSelector)(AddedAttributesContext_1.AddedAttributesContext, function (ctx) { return ctx.updateAddedAttributeRefEntity; });
    var selectedEntity = (0, react_context_selector_1.useContextSelector)(AddedAttributesContext_1.AddedAttributesContext, function (ctx) { var _a; return addedId ? (_a = ctx.getAddedAttributes().find(function (a) { return a.id === addedId; })) === null || _a === void 0 ? void 0 : _a.refEntity : undefined; });
    var handleChange = (0, react_1.useCallback)(function (newValue) {
        updateChange(lineIds, (0, helpers_1.isSimpleAttributeValue)(newValue) ? newValue : { value: newValue });
    }, [lineIds, updateChange]);
    var handleDelete = (0, react_1.useCallback)(function () {
        if (!addedId)
            return;
        removeAddedAttribute(addedId);
        deleteLineData(lineIds);
    }, [addedId, deleteLineData, lineIds, removeAddedAttribute]);
    var handleEntityChange = (0, react_1.useCallback)(function (entity) {
        if (!addedId)
            return;
        updateAddedAttributeRefEntity(addedId, {
            entityUri: entity.uri,
            entityType: entity.type,
            entityLabel: entity.label
        });
    }, [addedId, updateAddedAttributeRefEntity]);
    var handleImageUpload = (0, react_1.useCallback)(function (value) {
        if (lineIds.length === 0)
            return;
        updateChange(lineIds, value);
    }, [lineIds, updateChange]);
    return {
        lineData: lineData,
        addedId: addedId,
        selectedEntity: selectedEntity,
        isRejected: isRejected,
        hasAddedParent: hasAddedParent,
        handleDelete: handleDelete,
        handleChange: handleChange,
        handleEntityChange: handleEntityChange,
        handleImageUpload: handleImageUpload
    };
};
exports.useNewAttributesEditor = useNewAttributesEditor;
