"use strict";
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);
};
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.BubbleChart = void 0;
var react_1 = __importStar(require("react"));
var ramda_1 = require("ramda");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var recharts_1 = require("recharts");
var ChartLegend_1 = require("../ChartLegend");
var ChartTooltip_1 = require("../ChartTooltip");
var helpers_1 = require("./helpers");
var withChartDataPercents_1 = require("../HOCs/withChartDataPercents");
var useChartTooltipState_1 = require("../hooks/useChartTooltipState");
var useClickableChartStyle_1 = require("../hooks/useClickableChartStyle");
var useBubbles_1 = require("./useBubbles");
var styles_1 = require("./styles");
var BubbleChart = function (_a) {
    var width = _a.width, height = _a.height, data = _a.data, _b = _a.options, options = _b === void 0 ? {} : _b, onItemClick = _a.onItemClick;
    var styles = (0, styles_1.useStyles)();
    var _c = options.showLegend, showLegend = _c === void 0 ? true : _c, margin = options.margin;
    var initialRect = {
        x: (0, ramda_1.propOr)(0, 'left', margin),
        y: (0, ramda_1.propOr)(0, 'top', margin),
        width: width - (0, ramda_1.propOr)(0, 'left', margin) - (0, ramda_1.propOr)(0, 'right', margin),
        height: height - (0, ramda_1.propOr)(0, 'top', margin) - (0, ramda_1.propOr)(0, 'bottom', margin)
    };
    var initialLegendProps = (0, ChartLegend_1.getChartLegendProps)(width);
    var _d = (0, react_1.useState)({ width: 0, height: 0 }), legendBox = _d[0], setLegendBox = _d[1];
    var _e = (0, helpers_1.adjustRectWithLegend)(initialRect, initialLegendProps, legendBox), rect = _e.rect, legendProps = _e.legendProps;
    var bubbles = (0, useBubbles_1.useBubbles)({ data: data, width: rect.width, height: rect.height });
    var _f = (0, useChartTooltipState_1.useChartTooltipState)(), tooltipState = _f.tooltipState, showTooltip = _f.showTooltip, hideTooltip = _f.hideTooltip;
    var clickable = (0, useClickableChartStyle_1.useClickableChartStyle)(onItemClick);
    var circles = (0, react_1.useMemo)(function () {
        return bubbles.map(function (bubble) {
            var cx = rect.x + bubble.x;
            var cy = rect.y + bubble.y;
            return (react_1.default.createElement("circle", { key: bubble.data.label, cx: cx, cy: cy, r: bubble.r, onMouseEnter: function () { return showTooltip(bubble.data, { x: cx, y: cy }); }, onMouseLeave: function () { return hideTooltip(); }, onClick: onItemClick && (function () { return onItemClick(bubble.data.group); }), fill: (0, mdm_sdk_1.getChartItemColor)(bubble.data.label), className: clickable },
                react_1.default.createElement("animate", { attributeName: 'r', from: 0, to: bubble.r, dur: "0.5s" })));
        });
    }, [bubbles, hideTooltip, rect.x, rect.y, showTooltip, onItemClick, clickable]);
    return (react_1.default.createElement("div", { className: styles.bubbleChart, style: { width: width, height: height } },
        react_1.default.createElement("svg", { width: width, height: height }, circles),
        showLegend && (react_1.default.createElement(recharts_1.Legend, __assign({}, legendProps, { payload: (data || []).map(function (item) { return ({
                id: item.label,
                type: 'square',
                color: (0, mdm_sdk_1.getChartItemColor)(item.label),
                value: "".concat(item.label, " (").concat(item.percent, "%)")
            }); }), chartWidth: width, chartHeight: height, margin: margin, onBBoxUpdate: setLegendBox }))),
        react_1.default.createElement(recharts_1.Tooltip, __assign({ viewBox: rect, content: react_1.default.createElement(ChartTooltip_1.ChartTooltip, null) }, tooltipState))));
};
exports.BubbleChart = BubbleChart;
exports.default = (0, withChartDataPercents_1.withChartDataPercents)(exports.BubbleChart);
