import { array } from "@visactor/vutils";
import { atmosphereColorToFill, atmosphereColorToStroke } from "./barColor.js";
const getGroupCountFromSpec = (spec)=>{
    const field = 'horizontal' === spec.direction ? array(spec.yField)[0] : array(spec.xField)[0];
    const groupCount = new Set((spec.data?.[0]?.values ?? []).map((row)=>row[field])).size;
    return {
        groupCount
    };
};
const getGroupInfo = (chartInstance, datum)=>{
    const series = chartInstance.getChart().getAllSeries()[0];
    const indexField = 'horizontal' === series.direction ? series.fieldY[0] : series.fieldX[0];
    const indexValue = datum?.[indexField];
    const scale = 'horizontal' === series.direction ? series.scaleY : series.scaleX;
    const index = (scale?.domain?.() ?? []).indexOf(indexValue) || 0;
    const count = (scale?.domain?.() ?? []).length ?? 1;
    return {
        index,
        count
    };
};
const getSliceDuration = (totalDuration)=>(_datum, _element, _ctx, context)=>{
        const { count } = getGroupInfo(context.vchart, _datum);
        return 0 === count ? 1000 : totalDuration / count / 2;
    };
const groupHighLightBar = (startTime, config, duration, interval, atmosphereDuration, isHorizontal, spec)=>{
    const totalDuration = 1000 * duration;
    const color = config.color ?? '#4A90E2';
    const isGradientChart = spec?.bar?.style?.fill?.gradient === 'linear';
    const fillColor = atmosphereColorToFill(color, isGradientChart, isHorizontal);
    const strokeColor = atmosphereColorToStroke(color, isGradientChart, isHorizontal);
    return [
        {
            loop: true,
            startTime,
            controlOptions: {
                immediatelyApply: false
            },
            timeSlices: [
                {
                    effects: {
                        channel: {
                            fill: {
                                to: fillColor
                            },
                            stroke: {
                                to: strokeColor
                            }
                        },
                        easing: config.ease
                    },
                    delay: (datum, _element, _ctx, context)=>{
                        const { count, index } = getGroupInfo(context.vchart, datum);
                        return 0 === count ? 0 : index * totalDuration / count;
                    },
                    duration: getSliceDuration(totalDuration)
                },
                {
                    effects: {
                        channel: {
                            fill: {
                                from: fillColor,
                                to: (...p)=>p[1].attribute.fill
                            },
                            stroke: {
                                from: strokeColor,
                                to: (...p)=>p[1].attribute.fill
                            }
                        },
                        easing: config.ease
                    },
                    delayAfter: (datum, _element, _ctx, context)=>{
                        const { count, index } = getGroupInfo(context.vchart, datum);
                        return 0 === count ? 0 : (interval + atmosphereDuration) * 1000 + (count - index - 1) * totalDuration / count;
                    },
                    duration: getSliceDuration(totalDuration)
                }
            ]
        }
    ];
};
export { getGroupCountFromSpec, groupHighLightBar };

//# sourceMappingURL=barGroup.js.map