"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.BarGraph = void 0;
const react_1 = __importStar(require("react"));
const inlines_1 = require("inlines");
const HorizontalBar_1 = require("./HorizontalBar");
const VerticalBar_1 = require("./VerticalBar");
const StackedBars_1 = require("./StackedBars");
const NestedBars_1 = require("./NestedBars");
const Xaxis_1 = require("./Xaxis");
const Yaxis_1 = require("./Yaxis");
const BarLegend_1 = require("./BarLegend");
const BarGraph = ({ data, direction, style, valueFormat, color, barWidth, stacked, nested, spacing, showAxis, legend, }) => {
    const barWrapperRef = (0, react_1.useRef)();
    // if an object in data has more then 1 value it is nested or stacked
    for (let i = 0; i < data.length; i++) {
        if (typeof data[i].value !== 'number') {
            if (!nested && !stacked) {
                nested = true;
            }
        }
    }
    const totalValue = data.map((item) => item.value).reduce((a, b) => a + b, 0);
    // percentages
    data = data.map((item) => ({
        ...item,
        percentage: (item.value / totalValue) * 100,
    }));
    let totalValuesArr = [];
    if (stacked || nested) {
        for (let i = 0; i < data.length; i++) {
            totalValuesArr.push(Object.values(data[i].value)
                .map((item) => item)
                .reduce((a, b) => a + b, 0));
        }
    }
    let allValuesArr = [];
    if (nested || stacked) {
        for (let i = 0; i < data.length; i++) {
            let arr = Object.values(data[i].value);
            allValuesArr.push(arr);
        }
    }
    let theLargestValue = Math.max(...allValuesArr.flat());
    let axisValues = [];
    if (showAxis) {
        let divideByNo = 4;
        let largest = stacked
            ? theLargestValue
            : nested
                ? Math.max(...totalValuesArr)
                : totalValue;
        let step = largest / divideByNo;
        for (let i = 0; i < divideByNo + 1; i++) {
            axisValues.push(i * step);
        }
    }
    return (react_1.default.createElement(inlines_1.styled.div, null,
        nested ? (react_1.default.createElement(inlines_1.styled.div, { style: {
                width: '100%',
                display: 'flex',
                flexDirection: direction === 'vertical' ? 'row' : 'column',
                ...style,
            } },
            showAxis && direction === 'vertical' && (react_1.default.createElement(Yaxis_1.Yaxis, { axisValues: axisValues, spacing: spacing, valueFormat: valueFormat })),
            data.map((item, idx) => (react_1.default.createElement(NestedBars_1.NestedBars, { value: item.value, key: idx, label: item.label, largestValue: Math.max(...totalValuesArr), barWidth: barWidth, spacing: spacing, valueFormat: valueFormat, direction: direction }))),
            showAxis && direction !== 'vertical' && (react_1.default.createElement(Xaxis_1.Xaxis, { axisValues: axisValues, spacing: spacing, valueFormat: valueFormat })))) : stacked ? (react_1.default.createElement(inlines_1.styled.div, { style: {
                width: '100%',
                display: 'flex',
                flexDirection: direction === 'vertical' ? 'row' : 'column',
                ...style,
            } },
            showAxis && direction === 'vertical' && (react_1.default.createElement(Yaxis_1.Yaxis, { axisValues: axisValues, spacing: spacing, valueFormat: valueFormat })),
            data.map((item, idx) => (react_1.default.createElement("div", { key: idx },
                react_1.default.createElement(StackedBars_1.StackedBars, { value: item.value, key: idx, label: item.label, totalValue: theLargestValue, valueFormat: valueFormat, spacing: spacing, direction: direction, barWidth: barWidth })))),
            showAxis && direction !== 'vertical' && (react_1.default.createElement(Xaxis_1.Xaxis, { axisValues: axisValues, spacing: spacing, valueFormat: valueFormat })))) : direction === 'vertical' ? (react_1.default.createElement(inlines_1.styled.div, { style: {
                width: '100%',
                display: 'flex',
                flexDirection: 'row',
                ...style,
            } },
            showAxis && (react_1.default.createElement(Yaxis_1.Yaxis, { axisValues: axisValues, spacing: spacing, valueFormat: valueFormat })),
            data.map((item, idx) => (react_1.default.createElement(VerticalBar_1.VerticalBar, { key: idx, valueFormat: valueFormat, label: item.label, value: item.value, percentage: item.percentage, color: item.color ? item.color : color, spacing: spacing, legend: legend, barWidth: item.barWidth ? item.barWidth : barWidth }))))) : !direction || direction === 'horizontal' ? (react_1.default.createElement(inlines_1.styled.div, { style: {
                width: '100%',
                display: 'flex',
                flexDirection: 'column',
                ...style,
            } },
            data.map((item, idx) => (react_1.default.createElement(HorizontalBar_1.HorizontalBar, { key: idx, valueFormat: valueFormat, label: item.label, legend: legend, value: item.value, percentage: item.percentage, color: item.color ? item.color : color, barWidth: item.barWidth ? item.barWidth : barWidth, spacing: spacing }))),
            showAxis && (react_1.default.createElement(Xaxis_1.Xaxis, { axisValues: axisValues, spacing: spacing, valueFormat: valueFormat })))) : (react_1.default.createElement(react_1.default.Fragment, null)),
        legend && (react_1.default.createElement(BarLegend_1.BarLegend, { data: data, direction: direction, valueFormat: valueFormat, nested: nested, stacked: stacked }))));
};
exports.BarGraph = BarGraph;
//# sourceMappingURL=index.js.map