import { jsxs, jsx } from 'react/jsx-runtime';
import { useState } from 'react';
import Typography from '../../Typography/Typography.js';
import AutoTruncateText from '../../AutoTruncateText/AutoTruncateText.js';

const StackedBarChart = ({ data, barWidth = 24, height = 250, barGap = 40, barBorderRadius = 0, xAxisLabel, yAxisLabel, yAxisDivisions = 5, padding = 20, legend = true, legendPosition = 'bottom', showXAxisLabels = true, isTruncateText = true, isOnclick = false, onSelectedBar, onSegmentClick, }) => {
    const [selectedBar, setSelectedBar] = useState(null);
    const topPadding = 40;
    const leftPadding = 50;
    const maxTotal = Math.max(...data?.map((item) => item?.segments?.reduce((number, segment) => number + segment?.value, 0)), yAxisDivisions);
    const allKeys = Array.from(new Set(data.flatMap((block) => block?.segments?.map((segment) => segment?.key))));
    const keyColorMap = {};
    data.forEach((item) => item?.segments.forEach((segment) => {
        if (!keyColorMap[segment.key])
            keyColorMap[segment.key] = segment.color;
    }));
    const totalBarWidth = data.length * barWidth + (data.length - 1) * barGap;
    const chartWidth = totalBarWidth + leftPadding + padding + 20;
    const xAxisLabelWidth = barWidth + barGap;
    const handleClick = (colData, index) => {
        if (!isOnclick)
            return;
        setSelectedBar(colData);
        onSelectedBar?.(colData, index);
    };
    const handleSegmentClick = (label, segment, segmentIndex) => {
        onSegmentClick?.(label, segment, segmentIndex);
    };
    const renderLegend = () => (jsx("div", { className: "ff-stacked-bar-legend", children: allKeys.map((key) => (jsxs("div", { className: "ff-stacked-bar-legend-item", children: [jsx("span", { className: "ff-stacked-bar-legend-dot", style: { backgroundColor: keyColorMap[key] } }), isTruncateText ? (jsx(AutoTruncateText, { className: "ff-stacked-bar-legend-label", placement: legendPosition === 'top' ? 'bottom' : 'top', children: key })) : (jsx(Typography, { as: "span", className: "ff-stacked-bar-legend-label", fontSize: 10, fontWeight: "regular", children: key }))] }, key))) }));
    return (jsxs("div", { className: "ff-stacked-bar-container", children: [legend && legendPosition === 'top' && renderLegend(), jsxs("svg", { width: chartWidth, height: height +
                    topPadding +
                    5 +
                    (showXAxisLabels ? 20 : 0) +
                    (xAxisLabel ? 20 : 0), children: [Array.from({ length: yAxisDivisions + 1 }).map((_, i) => {
                        const y = height - (i / yAxisDivisions) * height + topPadding + 4;
                        const val = ((i * maxTotal) / yAxisDivisions)
                            .toFixed(0)
                            .padStart(2, '0');
                        return (jsx("text", { x: leftPadding - 5, y: y, textAnchor: "end", className: "ff-stacked-bar-label", children: val }, i));
                    }), jsx("line", { x1: leftPadding, y1: topPadding, x2: leftPadding, y2: height + topPadding, stroke: "var(--ff-comment-border-color)", strokeWidth: "1" }), data?.map((item, barIndex) => {
                        const total = item?.segments?.reduce((idx, segment) => idx + segment.value, 0);
                        const barX = barIndex * (barWidth + barGap) + leftPadding + padding;
                        const isSelected = selectedBar?.label === item?.label;
                        let stackY = height + topPadding;
                        return (jsxs("g", { onClick: () => handleClick(item, barIndex), style: { cursor: isOnclick ? 'pointer' : 'default' }, children: [item?.segments?.map((segment, segIndex) => {
                                    const segHeight = total > 0 ? (segment?.value / maxTotal) * height : 0;
                                    stackY -= segHeight;
                                    const isLastSeg = segIndex === item?.segments?.length - 1;
                                    const rx = isLastSeg ? barBorderRadius : 0;
                                    const segY = stackY;
                                    const tooltipId = `stacked-bar-tooltip-${barIndex}-${segIndex}`;
                                    return (jsxs("g", { children: [jsx("rect", { x: barX, y: segY, width: barWidth, height: segHeight, fill: segment.color, rx: isLastSeg ? rx : 0, ry: isLastSeg ? rx : 0, style: {
                                                    opacity: isSelected ? 0.85 : 1,
                                                    transition: 'opacity 0.3s ease',
                                                } }), jsxs("foreignObject", { x: barX, y: segY, width: barWidth, height: segHeight, style: { overflow: 'visible', pointerEvents: 'none' }, children: [jsx("div", { "data-tooltip-id": tooltipId, "data-tooltip-placement": "right", style: {
                                                            width: barWidth,
                                                            height: segHeight,
                                                            pointerEvents: 'all',
                                                        }, onClick: () => handleSegmentClick(item?.label, segment, segIndex) }), jsxs("div", { id: tooltipId, className: "ff-stacked-bar-tooltip-content", style: { display: 'none' }, children: [jsx(Typography, { as: "div", fontWeight: "semi-bold", children: item?.label }), jsxs("div", { className: "ff-stacked-bar-tooltip-row", children: [jsx(Typography, { fontWeight: "semi-bold", children: segment.key }), jsxs(Typography, { fontWeight: "semi-bold", children: [": ", segment.value] })] })] })] })] }, segment?.key));
                                }), showXAxisLabels && (jsx("foreignObject", { x: barX + barWidth / 2 - xAxisLabelWidth / 2, y: height + topPadding + 5, width: xAxisLabelWidth, height: 20, className: "ff-stacked-bar-x-label-wrapper", style: {
                                        color: isSelected ? 'var(--brand-color)' : 'black',
                                        fontWeight: isSelected ? 'bold' : 'normal',
                                    }, children: jsx(AutoTruncateText, { className: "ff-stacked-bar-x-label", children: item?.label }) }))] }, barIndex));
                    }), jsx("line", { x1: leftPadding, y1: height + topPadding, x2: chartWidth, y2: height + topPadding, stroke: "var(--ff-comment-border-color)", strokeWidth: "1" }), xAxisLabel && (jsx("text", { x: chartWidth / 2, y: height + topPadding + (showXAxisLabels ? 40 : 20), textAnchor: "middle", className: "ff-stacked-bar-label", children: xAxisLabel })), yAxisLabel && (jsx("text", { x: -(height / 2 + topPadding), y: 15, transform: "rotate(-90)", textAnchor: "middle", className: "ff-stacked-bar-label", children: yAxisLabel }))] }), legend && legendPosition === 'bottom' && renderLegend()] }));
};

export { StackedBarChart as default };
//# sourceMappingURL=StackedBarChart.js.map
