"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useCloud = void 0;
var react_1 = require("react");
var d3_cloud_1 = __importDefault(require("d3-cloud"));
var useCloud = function (_a) {
    var data = _a.data, width = _a.width, height = _a.height, _b = _a.maxFontSize, maxFontSize = _b === void 0 ? 40 : _b;
    return (0, react_1.useMemo)(function () {
        if (!width || !height)
            return [];
        var dataValues = data.map(function (item) { return item.value; });
        var minDataValue = Math.min.apply(Math, dataValues);
        var maxDataValue = Math.max.apply(Math, dataValues);
        var minFont = Math.log(12);
        var maxFont = Math.log(maxFontSize);
        var valDiff = maxDataValue - minDataValue;
        var scale = (maxFont - minFont) / (valDiff || 1);
        var layout = (0, d3_cloud_1.default)()
            .size([width, height])
            .words(data.map(function (item) { return ({
            text: item.label,
            size: Math.floor(Math.exp(minFont + scale * (item.value - minDataValue))),
            font: 'Roboto',
            data: item
        }); }))
            .random(function () { return 0.5; })
            .fontSize(function (word) { return word.size; })
            .rotate(0)
            .padding(3)
            .start();
        return layout.words();
    }, [data, width, height, maxFontSize]);
};
exports.useCloud = useCloud;
