import React, { useCallback } from 'react';
import i18n from 'ui-i18n';
import { useContextSelector } from '@fluentui/react-context-selector';
import AddIcon from '@mui/icons-material/Add';
import { AddedAttributesContext } from '../../../contexts/AddedAttributesContext';
import { SmallIconButton } from '../../../../../SmallIconButton';
import { extractLineIds } from '../../../helpers/merge';
import { getAddedId, getObjectIdFromLineId, getParentAddedId, getParentChangeOverrides, getParentLineId, isRootAddedAttribute } from '../../helpers/addedAttributes';
export var AddSameAttributeButton = function (_a) {
    var _b, _c;
    var change = _a.change;
    var attributeType = change.attributeType, lineIds = change.lineIds;
    var lineId = (_b = lineIds === null || lineIds === void 0 ? void 0 : lineIds[0]) !== null && _b !== void 0 ? _b : (_c = extractLineIds(change === null || change === void 0 ? void 0 : change.newValue)) === null || _c === void 0 ? void 0 : _c[0];
    var addedId = getAddedId(change);
    var addAddedAttributes = useContextSelector(AddedAttributesContext, function (ctx) { return ctx.addAddedAttributes; });
    var handleAdd = useCallback(function () {
        if (!attributeType || !lineId)
            return;
        var changeOverrides = getParentChangeOverrides(change);
        if (isRootAddedAttribute(lineId)) {
            addAddedAttributes({
                attributeTypes: [attributeType],
                objectUri: getObjectIdFromLineId(lineId),
                changeOverrides: changeOverrides
            });
        }
        else {
            addAddedAttributes({
                attributeTypes: [attributeType],
                parentLineId: getParentLineId(lineId),
                parentId: addedId ? getParentAddedId(addedId) : undefined,
                changeOverrides: changeOverrides
            });
        }
    }, [attributeType, lineId, addedId, change, addAddedAttributes]);
    return React.createElement(SmallIconButton, { icon: AddIcon, onClick: handleAdd, title: i18n.text('Add'), size: "S" });
};
