import React, { memo } from 'react';
import { getContributorColor } from '@reltio/mdm-sdk';
import { HistoryLink } from './components/HistoryLink';
import { HistoryCircle } from './components/HistoryCircle';
import { HistoryChangeTypes } from '../types';
import { isFakeHistoryType, findEndIndex } from './helpers';
import { withTooltip } from '../../../HOCs/withTooltip';
var COL_WIDTH = 16;
var COL_MARGIN = 24;
var ROW_MARGIN = 16;
var ROW_HEIGHT = 16;
export var HistoryCircleWithTooltip = withTooltip(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 = 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 === HistoryChangeTypes.FAKE_NAVIGATE_PROFILE && historyLane.index !== 0;
        acc.circles.push(React.createElement(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: getContributorColor(circleUri), isDashed: hasFakeType, isCollapsed: isCollapsed, showCollapseIcon: !!uris, showLine: shouldShowLine }));
        acc.rowLines.push(React.createElement("rect", { key: "".concat(circleUri, "-").concat(index), x: x, y: y - ROW_HEIGHT / 2, width: '100%', height: ROW_HEIGHT, fill: getContributorColor(circleUri), opacity: 0.06 }));
        var isUpRight = type === 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.createElement(HistoryLink, { key: "link-".concat(contributorUri, "-").concat(index), x: x, y: y, toX: toX, toY: toY, isDashed: hasFakeType, color: 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) === HistoryChangeTypes.ENTITIES_SPLITTED) {
                if ((end === null || end === void 0 ? void 0 : end.index) && contributorLane.index === 0) {
                    links.unshift(React.createElement("path", { key: "line-".concat(contributorUri, "-").concat(i), d: "M".concat(x, " ").concat(rowPositions[0], " L").concat(x, " ").concat(rowPositions[end.index - 1]), stroke: getContributorColor(contributorUri), strokeWidth: 2 }));
                }
                return;
            }
            var startIndex = (start === null || start === void 0 ? void 0 : start.type) === HistoryChangeTypes.ENTITIES_SPLITTED ? (start === null || start === void 0 ? void 0 : start.index) - 1 : start === null || start === void 0 ? void 0 : start.index;
            var endIndex = findEndIndex(end);
            var startY = rowPositions[startIndex] || rowPositions[rowPositions.length - 2];
            var endY = rowPositions[endIndex] || ROW_MARGIN;
            if (startY <= endY)
                return;
            var color = getContributorColor(contributorUri);
            if (!end && endIndex === undefined && contributorLane.index > 0) {
                circles.push(React.createElement(HistoryCircle, { key: "initCircle-".concat(contributorUri, "-").concat(i), x: x, y: endY, color: color, showLine: true }));
            }
            links.unshift(React.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.createElement("svg", { "data-reltio-id": "reltio-history-graph", className: "chart", width: width, height: height },
        React.createElement("g", { id: "rowLines" }, rowLines),
        React.createElement("g", { id: "links" }, links),
        React.createElement("g", { id: "circles" }, circles)));
};
export default memo(HistoryGraph);
