import { groupBy, uniqueBy } from "remeda";
import { datasetYX } from "../dataset/index.js";
const playerBar = (spec, context)=>{
    const { advancedVSeed } = context;
    const { dimensions = [], datasetReshapeInfo, chartType, encoding } = advancedVSeed;
    const baseConfig = advancedVSeed.config[chartType];
    const result = datasetYX(spec, context);
    if (!baseConfig || !baseConfig.player) return result;
    const { player } = baseConfig;
    const fontFamily = player.fontFamily || baseConfig.fontFamily;
    const textSize = player.fontSize ?? 36;
    const id = datasetReshapeInfo[0].id;
    const { unfoldInfo, foldInfo } = datasetReshapeInfo[0];
    const { encodingPlayer, encodingY } = unfoldInfo;
    const { measureValue } = foldInfo;
    const { maxCount, autoPlay = true, interval = 1000, loop = false, position, railColor, trackColor, sliderHandleColor, sliderHandleBorderColor, startButtonColor, pauseButtonColor, backwardButtonColor, forwardButtonColor } = player;
    const duration = interval;
    const exchangeDuration = 0.6 * interval;
    const dataGroups = groupBy(advancedVSeed.dataset, (item)=>item[encodingPlayer]);
    if (result.data && 'values' in result.data) result.data.values = [];
    const yValues = uniqueBy(advancedVSeed.dataset.map((d)=>d[encodingY]), (item)=>item);
    const specs = Object.values(dataGroups).map((items)=>{
        const filledItems = items.map((item)=>({
                ...item,
                [encodingY]: yValues.find((yValue)=>yValue === item[encodingY]) || 0
            }));
        const sortedItems = filledItems.sort((a, b)=>b[measureValue] - a[measureValue]);
        return {
            data: {
                id: id,
                values: false === maxCount ? sortedItems : sortedItems.slice(0, maxCount)
            }
        };
    });
    const dataKey = dimensions.filter((d)=>!encoding.player?.includes(d.id)).map((d)=>d.id);
    const padding = 12;
    return {
        ...result,
        dataKey,
        stackCornerRadius: void 0,
        animationUpdate: {
            bar: [
                {
                    type: 'update',
                    options: {
                        excludeChannels: [
                            'y'
                        ]
                    },
                    easing: 'linear',
                    duration
                },
                {
                    channel: [
                        'y'
                    ],
                    easing: 'circInOut',
                    duration: exchangeDuration
                }
            ]
        },
        animationEnter: {
            bar: [
                {
                    type: 'moveIn',
                    duration: exchangeDuration,
                    easing: 'circInOut',
                    options: {
                        direction: 'y',
                        orient: 'negative'
                    }
                }
            ]
        },
        animationExit: {
            bar: [
                {
                    type: 'moveOut',
                    options: {
                        direction: 'x'
                    },
                    duration: exchangeDuration
                },
                {
                    type: 'moveOut',
                    options: {
                        direction: 'y',
                        orient: 'negative'
                    },
                    duration: exchangeDuration
                },
                {
                    type: 'fadeOut',
                    duration: exchangeDuration
                }
            ]
        },
        customMark: [
            {
                type: 'text',
                dataId: 'year',
                style: {
                    textBaseline: 'bottom',
                    fontSize: textSize,
                    textAlign: 'right',
                    fontFamily,
                    fontWeight: 600,
                    text: (datum)=>datum[encodingPlayer],
                    x: (_datum, ctx)=>ctx.vchart.getChart().getCanvasRect()?.width - padding,
                    y: (_datum, ctx)=>ctx.vchart.getChart().getCanvasRect()?.height - padding - textSize,
                    fill: 'rgb(100, 100, 100)',
                    fillOpacity: 0.25
                }
            }
        ],
        player: {
            visible: true,
            auto: autoPlay,
            interval: interval,
            loop: loop,
            alternate: false,
            position: 'middle',
            orient: position,
            type: 'discrete',
            specs,
            controller: {
                visible: true,
                start: {
                    visible: true,
                    order: 0,
                    style: {
                        fill: startButtonColor
                    }
                },
                pause: {
                    visible: true,
                    order: 1,
                    style: {
                        fill: pauseButtonColor
                    }
                },
                backward: {
                    visible: true,
                    order: 2,
                    style: {
                        fill: backwardButtonColor
                    }
                },
                forward: {
                    visible: true,
                    order: 3,
                    position: 'end',
                    style: {
                        fill: forwardButtonColor
                    }
                }
            },
            slider: {
                visible: true,
                railStyle: {
                    visible: true,
                    fill: railColor,
                    ['top' === position || 'bottom' === position ? 'height' : 'width']: 2
                },
                trackStyle: {
                    visible: true,
                    fill: trackColor,
                    ['top' === position || 'bottom' === position ? 'height' : 'width']: 2
                },
                handlerStyle: {
                    visible: true,
                    lineWidth: 2,
                    stroke: sliderHandleBorderColor,
                    fill: sliderHandleColor,
                    size: 9
                }
            }
        }
    };
};
export { playerBar };

//# sourceMappingURL=playerBar.js.map