"use strict";
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.PieGraph = void 0;
const react_1 = __importStar(require("react"));
const varsUtilities_1 = require("../../varsUtilities");
const Text_1 = require("../Text");
const inlines_1 = require("inlines");
const pretty_number_1 = require("@based/pretty-number");
const PieLegend_1 = require("./PieLegend");
const PieGraph = ({ data, valueFormat = 'percentages', legend, style, }) => {
    const [featured, setFeatured] = (0, react_1.useState)();
    const totalValue = data?.map((item) => item.value).reduce((a, b) => a + b, 0);
    // percentages
    data = data?.map((item) => ({
        ...item,
        percentage: (item.value / totalValue) * 100,
        degrees: 0,
    }));
    // rotating degrees
    for (let i = 0; i < data.length; i++) {
        if (i === 0) {
            data[i].degrees = -90;
        }
        else {
            data[i].degrees = data[i - 1].percentage * 3.6 + data[i - 1].degrees;
        }
    }
    const objectWithLargestValue = data?.length > 0
        ? data?.reduce(function (prev, current) {
            return prev?.value > current?.value ? prev : current;
        })
        : { value: undefined };
    // if biggest strokeWidth = 32 and smallest 22
    const onePercentSmallestStrokeWidth = objectWithLargestValue.value / 100;
    data = data?.map((item) => ({
        ...item,
        strokeWidth: Math.round(item.value / onePercentSmallestStrokeWidth) / 10 + 22,
    }));
    return (react_1.default.createElement("div", null,
        react_1.default.createElement(inlines_1.styled.div, { style: {
                margin: '0 auto',
                textAlign: 'center',
                position: 'relative',
                width: 264,
                height: 264,
                marginTop: 32,
                ...style,
            } },
            react_1.default.createElement("svg", { width: "200", height: "200", viewBox: "0 0 120 120", style: {
                    overflow: 'visible',
                } }, data?.map((item, idx) => {
                return (react_1.default.createElement("circle", { onMouseEnter: () => {
                        setFeatured({
                            label: item.label,
                            percentage: item.percentage,
                            value: item.value,
                        });
                    }, onMouseLeave: () => setFeatured(undefined), key: idx, cx: "60", cy: "60", r: "64", fill: "none", stroke: (0, varsUtilities_1.color)('nonSemanticBackground', item?.color || (0, varsUtilities_1.colorHash)('nonSemanticBackground', item.label), 'strong'), strokeWidth: item.strokeWidth, strokeDasharray: 100, pathLength: "100", strokeDashoffset: 100 - item.percentage + 0.5, style: {
                        transform: `rotate(${item.degrees}deg)`,
                        transformOrigin: '60px 60px',
                        opacity: featured && item?.label !== featured?.label ? 0.4 : 1,
                    } }));
            })),
            react_1.default.createElement(inlines_1.styled.div, { style: {
                    alignItems: 'center',
                    display: 'flex',
                    flexDirection: 'column',
                    inset: '0px',
                    pointerEvents: 'none',
                    position: 'absolute',
                    top: '50%',
                    transform: 'translateY(-50%)',
                } },
                react_1.default.createElement(Text_1.Text, { weight: "strong", size: 32 }, featured
                    ? valueFormat !== 'percentages'
                        ? (0, pretty_number_1.prettyNumber)(featured.value, valueFormat)
                        : objectWithLargestValue?.percentage
                            ? featured?.percentage.toFixed(1) + '%'
                            : ''
                    : valueFormat !== 'percentages'
                        ? (0, pretty_number_1.prettyNumber)(objectWithLargestValue.value, valueFormat)
                        : objectWithLargestValue
                            ? objectWithLargestValue?.percentage
                                ? objectWithLargestValue?.percentage?.toFixed(1) + '%'
                                : ''
                            : ''),
                react_1.default.createElement(Text_1.Text, { weight: "strong", size: 12 }, featured ? featured?.label : objectWithLargestValue?.label))),
        legend && react_1.default.createElement(PieLegend_1.PieLegend, { data: data })));
};
exports.PieGraph = PieGraph;
//# sourceMappingURL=index.js.map