"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.PieChart = void 0;
var react_1 = __importStar(require("react"));
var recharts_1 = require("recharts");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var withChartDataPercents_1 = require("../HOCs/withChartDataPercents");
var ChartLegend_1 = require("../ChartLegend");
var ActiveShape_1 = require("./components/ActiveShape");
var ChartTooltip_1 = require("../ChartTooltip");
var useClickableChartStyle_1 = require("../hooks/useClickableChartStyle");
var styles_1 = require("./styles");
var PieChart = function (_a) {
    var width = _a.width, height = _a.height, data = _a.data, _b = _a.options, options = _b === void 0 ? {} : _b, isDonut = _a.isDonut, onItemClick = _a.onItemClick;
    var _c = options.showLegend, showLegend = _c === void 0 ? true : _c, margin = options.margin, showActiveShape = options.showActiveShape;
    var styles = (0, styles_1.useStyles)();
    var _d = (0, react_1.useState)(-1), activeId = _d[0], setActiveId = _d[1];
    var onPieEnter = function (_, index) { return setActiveId(index); };
    var onPieLeave = function () { return setActiveId(-1); };
    var onPieClick = onItemClick && (function (_a) {
        var group = _a.group;
        return onItemClick(group);
    });
    var clickable = (0, useClickableChartStyle_1.useClickableChartStyle)(onPieClick);
    return (react_1.default.createElement(recharts_1.PieChart, { width: width, height: height, margin: margin },
        !showActiveShape && react_1.default.createElement(recharts_1.Tooltip, { content: react_1.default.createElement(ChartTooltip_1.ChartTooltip, null) }),
        showLegend && (react_1.default.createElement(recharts_1.Legend, __assign({}, (0, ChartLegend_1.getChartLegendProps)(width), { 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, "%)")
            }); }) }))),
        react_1.default.createElement(recharts_1.Pie, { dataKey: "value", data: data, activeIndex: activeId, activeShape: showActiveShape ? ActiveShape_1.ActiveShape : undefined, onMouseEnter: showActiveShape ? onPieEnter : undefined, onMouseLeave: showActiveShape ? onPieLeave : undefined, innerRadius: isDonut ? (showActiveShape ? '50%' : '60%') : 0, outerRadius: showActiveShape ? '70%' : '80%', onClick: onPieClick, className: styles.pie }, (data || []).map(function (entry, index) { return (react_1.default.createElement(recharts_1.Cell, { key: index, fill: (0, mdm_sdk_1.getChartItemColor)(entry.label), className: clickable })); }))));
};
exports.PieChart = PieChart;
exports.default = (0, withChartDataPercents_1.withChartDataPercents)(exports.PieChart);
