var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
import React, { useMemo } from 'react';
import { scaleLinear } from 'd3-scale';
import { IntersectionsChartAxis } from '../IntersectionsChartAxis';
import { GAP_BETWEEN_TABLE_AND_CHART } from '../../constants';
import { useStyles } from './styles';
export var IntersectionsChart = function (_a) {
    var xScale = _a.xScale, width = _a.width, height = _a.height, transform = _a.transform, matrixHeight = _a.matrixHeight, intersections = _a.intersections, highlightedElements = _a.highlightedElements, onIntersectionHover = _a.onIntersectionHover;
    var styles = useStyles();
    var yScale = useMemo(function () {
        var intersectionsSizes = intersections.map(function (intersection) { return intersection.size; });
        var domain = [0, Math.max.apply(Math, __spreadArray([0], intersectionsSizes, false))];
        return scaleLinear([height, 0]).domain(domain);
    }, [intersections, height]);
    return (React.createElement("g", { transform: transform },
        React.createElement(IntersectionsChartAxis, { xScale: xScale, yScale: yScale, width: width, height: height }),
        intersections.map(function (intersection, i) {
            var elements = intersection.elements, size = intersection.size;
            var x = xScale(String(i));
            var y = yScale(size);
            var width = xScale.bandwidth();
            var barY = highlightedElements
                ? yScale(elements.filter(function (el) { return highlightedElements.includes(el); }).length)
                : y;
            return (React.createElement("g", { key: "intersection-group-".concat(i) },
                React.createElement("rect", { className: styles.barBackground, x: x, y: y, width: width, height: height - y }),
                React.createElement("text", { className: styles.barLabel, x: x + width / 2, y: y - 4 }, size),
                React.createElement("rect", { className: styles.bar, x: x, y: barY, width: width, height: height - barY }),
                React.createElement("rect", { className: styles.hoverBar, "data-reltio-id": "intersection-hover-area", x: x, y: y, width: width, height: height - y + matrixHeight + GAP_BETWEEN_TABLE_AND_CHART - 1, onMouseEnter: function () { return onIntersectionHover(intersection); }, onMouseLeave: function () { return onIntersectionHover(null); } })));
        })));
};
