"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.CloudChart = void 0;
var react_1 = __importStar(require("react"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var recharts_1 = require("recharts");
var ChartTooltip_1 = require("../ChartTooltip");
var useChartTooltipState_1 = require("../hooks/useChartTooltipState");
var useClickableChartStyle_1 = require("../hooks/useClickableChartStyle");
var useCloud_1 = require("./useCloud");
var withChartDataPercents_1 = require("../HOCs/withChartDataPercents");
var styles_1 = require("./styles");
var CloudChart = function (_a) {
    var width = _a.width, height = _a.height, data = _a.data, _b = _a.options, options = _b === void 0 ? {} : _b, onItemClick = _a.onItemClick;
    var _c = (0, useChartTooltipState_1.useChartTooltipState)(), tooltipState = _c.tooltipState, showTooltip = _c.showTooltip, hideTooltip = _c.hideTooltip;
    var maxFontSize = options.maxFontSize;
    var styles = (0, styles_1.useStyles)();
    var clickable = (0, useClickableChartStyle_1.useClickableChartStyle)(onItemClick);
    var rect = (0, react_1.useMemo)(function () { return ({
        x: 0,
        y: 0,
        width: width || 0,
        height: height || 0
    }); }, [width, height]);
    var words = (0, useCloud_1.useCloud)({ data: data, width: width, height: height, maxFontSize: maxFontSize });
    var cloudWords = (0, react_1.useMemo)(function () {
        var cx = rect.width / 2;
        var cy = rect.height / 2;
        return (react_1.default.createElement("g", { transform: "translate(".concat(cx, ",").concat(cy, ")") }, words.map(function (_a) {
            var text = _a.text, data = _a.data, x = _a.x, y = _a.y, size = _a.size;
            return (react_1.default.createElement("text", { key: text, onMouseEnter: function () { return showTooltip(data, { x: cx + x, y: cy + y }); }, onMouseLeave: function () { return hideTooltip(); }, fill: (0, mdm_sdk_1.getChartItemColor)(text), textAnchor: "middle", transform: "translate(".concat(x, ",").concat(y, ")"), fontSize: size, className: clickable, onClick: onItemClick && (function () { return onItemClick(data.group); }) }, text));
        })));
    }, [rect.width, rect.height, words, clickable, onItemClick, showTooltip, hideTooltip]);
    return (react_1.default.createElement("div", { className: styles.container },
        react_1.default.createElement("svg", { width: rect.width, height: rect.height }, cloudWords),
        react_1.default.createElement(recharts_1.Tooltip, __assign({ offset: 5, viewBox: rect, content: react_1.default.createElement(ChartTooltip_1.ChartTooltip, null) }, tooltipState))));
};
exports.CloudChart = CloudChart;
exports.default = (0, withChartDataPercents_1.withChartDataPercents)(exports.CloudChart);
