"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HistoryCircleWithTooltip = void 0;
var react_1 = __importStar(require("react"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var HistoryLink_1 = require("./components/HistoryLink");
var HistoryCircle_1 = require("./components/HistoryCircle");
var types_1 = require("../types");
var helpers_1 = require("./helpers");
var withTooltip_1 = require("../../../HOCs/withTooltip");
var COL_WIDTH = 16;
var COL_MARGIN = 24;
var ROW_MARGIN = 16;
var ROW_HEIGHT = 16;
exports.HistoryCircleWithTooltip = (0, withTooltip_1.withTooltip)(HistoryCircle_1.HistoryCircle);
var HistoryGraph = function (_a) {
    var graphData = _a.graphData, onHistoryCircleMouseOver = _a.onHistoryCircleMouseOver;
    var historyLanes = graphData.historyLanes, rows = graphData.rows;
    var rowPositions = [ROW_MARGIN];
    var _b = rows.reduce(function (acc, _a, i) {
        var uri = _a.uri, height = _a.height, uris = _a.uris, type = _a.type, fakeType = _a.fakeType, index = _a.index, timestamp = _a.timestamp, label = _a.label;
        var historyLane = historyLanes[uri];
        var x = historyLane.index * COL_WIDTH + COL_MARGIN;
        var y = rowPositions[i];
        rowPositions.push(y + height);
        var hasFakeType = (0, helpers_1.isFakeHistoryType)(fakeType);
        var circleUri = hasFakeType ? uris[0] : uri;
        var isCollapsed = !(uris === null || uris === void 0 ? void 0 : uris.some(function (uri) { return historyLanes[uri]; }));
        var shouldShowLine = fakeType === types_1.HistoryChangeTypes.FAKE_NAVIGATE_PROFILE && historyLane.index !== 0;
        acc.circles.push(react_1.default.createElement(exports.HistoryCircleWithTooltip, { tooltipTitle: label, tooltipPlacement: "top", onMouseOver: onHistoryCircleMouseOver ? function () { return onHistoryCircleMouseOver(uri, timestamp, index); } : undefined, key: "circle-".concat(circleUri, "-").concat(i), x: x, y: y, color: (0, mdm_sdk_1.getContributorColor)(circleUri), isDashed: hasFakeType, isCollapsed: isCollapsed, showCollapseIcon: !!uris, showLine: shouldShowLine }));
        acc.rowLines.push(react_1.default.createElement("rect", { key: "".concat(circleUri, "-").concat(index), x: x, y: y - ROW_HEIGHT / 2, width: '100%', height: ROW_HEIGHT, fill: (0, mdm_sdk_1.getContributorColor)(circleUri), opacity: 0.06 }));
        var isUpRight = type === types_1.HistoryChangeTypes.ENTITIES_SPLITTED;
        uris === null || uris === void 0 ? void 0 : uris.forEach(function (contributorUri) {
            var contributorLane = historyLanes[contributorUri];
            if (!contributorLane)
                return;
            var rowHeight = rows[i].height;
            var toX = contributorLane.index * COL_WIDTH + COL_MARGIN;
            var toY = isUpRight ? y - rowHeight : y + rowHeight;
            if (x !== toX)
                acc.links.push(react_1.default.createElement(HistoryLink_1.HistoryLink, { key: "link-".concat(contributorUri, "-").concat(index), x: x, y: y, toX: toX, toY: toY, isDashed: hasFakeType, color: (0, mdm_sdk_1.getContributorColor)(contributorUri) }));
        });
        return acc;
    }, { links: [], circles: [], rowLines: [] }), links = _b.links, rowLines = _b.rowLines, circles = _b.circles;
    var width = Object.keys(historyLanes).length * COL_WIDTH + COL_MARGIN;
    var height = rowPositions[rowPositions.length - 1] - ROW_MARGIN;
    Object.entries(historyLanes).forEach(function (_a) {
        var contributorUri = _a[0], contributorLane = _a[1];
        var x = contributorLane.index * COL_WIDTH + COL_MARGIN;
        contributorLane.lifeTime.forEach(function (_a, i) {
            var _b;
            var start = _a.start, end = _a.end;
            if (((_b = rows[end === null || end === void 0 ? void 0 : end.index]) === null || _b === void 0 ? void 0 : _b.type) === types_1.HistoryChangeTypes.ENTITIES_SPLITTED) {
                if ((end === null || end === void 0 ? void 0 : end.index) && contributorLane.index === 0) {
                    links.unshift(react_1.default.createElement("path", { key: "line-".concat(contributorUri, "-").concat(i), d: "M".concat(x, " ").concat(rowPositions[0], " L").concat(x, " ").concat(rowPositions[end.index - 1]), stroke: (0, mdm_sdk_1.getContributorColor)(contributorUri), strokeWidth: 2 }));
                }
                return;
            }
            var startIndex = (start === null || start === void 0 ? void 0 : start.type) === types_1.HistoryChangeTypes.ENTITIES_SPLITTED ? (start === null || start === void 0 ? void 0 : start.index) - 1 : start === null || start === void 0 ? void 0 : start.index;
            var endIndex = (0, helpers_1.findEndIndex)(end);
            var startY = rowPositions[startIndex] || rowPositions[rowPositions.length - 2];
            var endY = rowPositions[endIndex] || ROW_MARGIN;
            if (startY <= endY)
                return;
            var color = (0, mdm_sdk_1.getContributorColor)(contributorUri);
            if (!end && endIndex === undefined && contributorLane.index > 0) {
                circles.push(react_1.default.createElement(HistoryCircle_1.HistoryCircle, { key: "initCircle-".concat(contributorUri, "-").concat(i), x: x, y: endY, color: color, showLine: true }));
            }
            links.unshift(react_1.default.createElement("path", { key: "line-".concat(contributorUri, "-").concat(i), d: "M".concat(x, " ").concat(startY, " L").concat(x, " ").concat(endY), stroke: color, strokeWidth: 2 }));
        });
    });
    return (react_1.default.createElement("svg", { "data-reltio-id": "reltio-history-graph", className: "chart", width: width, height: height },
        react_1.default.createElement("g", { id: "rowLines" }, rowLines),
        react_1.default.createElement("g", { id: "links" }, links),
        react_1.default.createElement("g", { id: "circles" }, circles)));
};
exports.default = (0, react_1.memo)(HistoryGraph);
