import { uniqueBy } from "remeda";
import { createFormatterByMeasure, findMeasureById } from "../../../../utils/index.js";
import { ORIGINAL_DATA } from "../../../../../dataReshape/index.js";
import { getTooltipStyle } from "./tooltipStyle.js";
import { updateTooltipElement } from "./tooltipElement.js";
const tooltipScatter = (spec, context)=>{
    const result = {
        ...spec
    };
    const { advancedVSeed, vseed } = context;
    const { datasetReshapeInfo, chartType, locale, dimensions = [], encoding } = advancedVSeed;
    const baseConfig = advancedVSeed.config[chartType];
    const { tooltip = {
        enable: true
    } } = baseConfig;
    const { enable } = tooltip;
    const { foldInfoList } = datasetReshapeInfo[0];
    result.tooltip = {
        style: getTooltipStyle(tooltip),
        visible: enable,
        mark: {
            title: {
                visible: false
            },
            content: createMarkContent(encoding.tooltip || [], dimensions, vseed.measures, locale, foldInfoList),
            updateContent: (prev = [])=>uniqueBy(prev, (entry)=>`${String(entry.key)}::${String(entry.value)}`)
        },
        dimension: {
            visible: false
        },
        updateElement: updateTooltipElement
    };
    return result;
};
const createMarkContent = (tooltip, dimensions, measures = [], locale, foldInfoList)=>{
    const dims = uniqueBy(dimensions.filter((item)=>tooltip.includes(item.id)), (item)=>item.id);
    const meas = uniqueBy(measures.filter((item)=>tooltip.includes(item.id)), (item)=>item.id);
    const dimContent = dims.map((item)=>({
            visible: true,
            hasShape: true,
            shapeType: 'rectRound',
            key: (v)=>{
                const datum = v;
                if (item.alias || item.id) return item.alias || item.id;
                return datum && datum[item.id];
            },
            value: (v)=>{
                const datum = v;
                return datum && datum[item.id];
            }
        }));
    const meaContent = meas.map((item)=>({
            visible: true,
            hasShape: true,
            shapeType: 'rectRound',
            key: item.alias || item.id,
            value: (v)=>{
                const datum = v;
                if (!datum) return '';
                const id = item.id;
                if (!datum || !datum[ORIGINAL_DATA] || !datum[ORIGINAL_DATA]) return '';
                const originalData = datum[ORIGINAL_DATA];
                const value = originalData[id];
                const measure = findMeasureById(measures, id);
                const formatter = createFormatterByMeasure(measure);
                return formatter(value);
            }
        }));
    const foldMeaContent = foldInfoList.map((foldInfo)=>({
            visible: true,
            hasShape: true,
            shapeType: 'rectRound',
            key: (v)=>{
                const { measureId, foldMap } = foldInfo;
                const datum = v;
                if (!datum) return '';
                const id = datum[measureId];
                return foldMap[id] ?? id;
            },
            value: (v)=>{
                const { measureId, measureValue } = foldInfo;
                const datum = v;
                if (!datum) return '';
                const value = datum[measureValue];
                const id = datum[measureId];
                const measure = findMeasureById(measures, id);
                const formatter = createFormatterByMeasure(measure);
                return formatter(value);
            }
        }));
    return [
        ...dimContent,
        ...foldMeaContent,
        ...meaContent
    ];
};
export { createMarkContent, tooltipScatter };

//# sourceMappingURL=tooltipScatter.js.map