var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import { ADDED_ID_SEPARATOR } from '../../contexts/AddedAttributesContext';
export var isAddedAttribute = function (change) {
    var _a;
    return !!((_a = change.newValue) === null || _a === void 0 ? void 0 : _a.addedId) || !!change.addedId;
};
export var getAddedId = function (change) {
    var _a, _b;
    return (_b = (_a = change.newValue) === null || _a === void 0 ? void 0 : _a.addedId) !== null && _b !== void 0 ? _b : change.addedId;
};
export var getParentAddedId = function (addedId) {
    var parts = addedId.split(ADDED_ID_SEPARATOR);
    return parts.length > 2 ? parts.slice(0, -1).join(ADDED_ID_SEPARATOR) : undefined;
};
export var hasParentAddedAttribute = function (addedId) {
    return !!addedId && !!getParentAddedId(addedId);
};
export var getParentLineId = function (lineId) {
    var parts = lineId.split('/');
    return parts.length > 3 ? parts.slice(0, -3).join('/') : undefined;
};
export var isRootAddedAttribute = function (lineId) {
    return lineId.split('/').length === 3;
};
export var getObjectIdFromLineId = function (lineId) {
    return lineId.split('/')[0];
};
export var extractChangeOverrides = function (change) {
    if (!change.attributePath && !change.refAttributeType && !change.refObjectURI)
        return undefined;
    return __assign(__assign(__assign({}, (change.attributePath && { attributePath: change.attributePath })), (change.refAttributeType && { refAttributeType: change.refAttributeType })), (change.refObjectURI && { refObjectURI: change.refObjectURI }));
};
export var getParentChangeOverrides = function (change) {
    var result = {};
    if (change.attributePath) {
        var parts = change.attributePath.split('/');
        var dropCount = parts.length % 2 === 0 ? 2 : 1;
        if (parts.length > dropCount) {
            result.attributePath = parts.slice(0, -dropCount).join('/');
        }
    }
    if (change.refAttributeType) {
        var parts = change.refAttributeType.split('/');
        if (parts.length > 2) {
            var parentRefAttributeType = parts.slice(0, -2).join('/');
            if (parentRefAttributeType.includes('/attributes/')) {
                result.refAttributeType = parentRefAttributeType;
            }
        }
    }
    if (result.refAttributeType && change.refObjectURI) {
        result.refObjectURI = change.refObjectURI;
    }
    return Object.keys(result).length > 0 ? result : undefined;
};
