import { isNullish, pipe, uniqueBy } from "remeda";
import { BinEndMeasureId, BinStartMeasureId, ColorEncoding, FoldMeasureValue, XEncoding } from "../../../../../dataReshape/index.js";
import { getTooltipStyle } from "./tooltipStyle.js";
import { getDefaultValueFormatterOfHistogram, getDefaultXFormatterOfHistogram } from "../../utils/histogram.js";
import { updateMarkTooltipElement } from "./tooltipElement.js";
const VCHART_OUTLIER_KEY = '__VCHART_BOX_PLOT_OUTLIER_VALUE';
const tooltipHistogram = (spec, context)=>{
    const result = {
        ...spec
    };
    const { advancedVSeed } = context;
    const { chartType, dimensions = [], encoding } = advancedVSeed;
    const baseConfig = advancedVSeed.config[chartType];
    const { binValueType, tooltip = {
        enable: true
    } } = baseConfig;
    const { enable } = tooltip;
    const defaultXFormatter = getDefaultXFormatterOfHistogram(advancedVSeed);
    const defaultValueFormatter = getDefaultValueFormatterOfHistogram(binValueType);
    result.tooltip = {
        style: getTooltipStyle(tooltip),
        visible: enable,
        mark: {
            title: {
                visible: false
            },
            content: createMarkContent(encoding.tooltip || [], dimensions, encoding, defaultXFormatter, defaultValueFormatter)
        },
        dimension: {
            title: {
                visible: false
            },
            content: createMarkContent(encoding.tooltip || [], dimensions, encoding, defaultXFormatter, defaultValueFormatter)
        },
        updateElement: updateMarkTooltipElement
    };
    return result;
};
const createMarkContent = (tooltip, dimensions, encoding, dimFormatter, defaultValueFormatter)=>{
    const dims = pipe(dimensions.filter((item)=>tooltip.includes(item.id)), uniqueBy((item)=>item.id), uniqueBy((item)=>item.alias));
    const dimContent = dims.map((item)=>({
            visible: true,
            hasShape: true,
            shapeType: 'rectRound',
            key: item.alias ?? item.id,
            value: (datum)=>{
                if (!isNullish(datum?.[VCHART_OUTLIER_KEY])) {
                    if (encoding.color?.includes(item.id)) return datum?.[ColorEncoding];
                    if (encoding.x?.includes(item.id)) return datum?.[XEncoding];
                }
                return defaultValueFormatter(datum?.[item.id]);
            }
        }));
    const defaultContent = [
        {
            visible: true,
            hasShape: true,
            shapeType: 'rectRound',
            key: (datum)=>{
                if (!datum) return '';
                return `[${dimFormatter(+datum[BinStartMeasureId])}, ${dimFormatter(+datum[BinEndMeasureId])})`;
            },
            value: (datum)=>{
                if (!datum) return '';
                return defaultValueFormatter(datum[FoldMeasureValue]);
            }
        }
    ];
    return [
        ...dimContent,
        defaultContent
    ];
};
export { tooltipHistogram };

//# sourceMappingURL=tooltipHistogram.js.map