import { pipe, unique, uniqueBy } from "remeda";
import { createFormatterByMeasure, findMeasureById } from "../../../../utils/index.js";
import { MeasureId, MeasureName, ORIGINAL_DATA } from "../../../../../dataReshape/index.js";
import { getTooltipStyle } from "./tooltipStyle.js";
import { updateTooltipElement } from "./tooltipElement.js";
const tooltip_tooltip = (spec, context)=>{
    const result = {
        ...spec
    };
    const { advancedVSeed, vseed } = context;
    const { measures = [], datasetReshapeInfo, chartType, dimensions = [], encoding, reshapeMeasures = [] } = advancedVSeed;
    const baseConfig = advancedVSeed.config[chartType];
    const { tooltip = {
        enable: true
    } } = baseConfig;
    const { enable = true } = tooltip;
    const { foldInfo, unfoldInfo } = datasetReshapeInfo[0];
    result.tooltip = {
        style: getTooltipStyle(tooltip),
        visible: !!enable,
        mark: {
            title: {
                visible: false
            },
            content: createMarkContent(encoding.tooltip || [], dimensions, vseed.measures, foldInfo, unfoldInfo)
        },
        dimension: {
            title: {
                visible: true
            },
            content: createDimensionContent(encoding.tooltip || [], encoding.color || [], measures, foldInfo, unfoldInfo, reshapeMeasures.length > 1)
        },
        updateElement: updateTooltipElement
    };
    return result;
};
const createDimensionContent = (tooltips, colors, measures = [], foldInfo, unfoldInfo, hasMultiMeasureGroup)=>{
    const { measureId, measureValue, foldMap } = foldInfo;
    const { encodingColor } = unfoldInfo;
    const hasMeasureTooltip = tooltips.some((d)=>measures.find((item)=>item.id === d));
    const hasMeasureIdInColor = colors.some((d)=>measures.find((item)=>item.id === d || d === MeasureId || d === MeasureName));
    return [
        {
            visible: true,
            shapeType: 'rectRound',
            hasShape: true,
            key: hasMeasureTooltip ? (v)=>{
                const datum = v;
                const key = datum && datum[measureId] || '';
                return foldMap[key] ?? key;
            } : (v)=>{
                const datum = v;
                const colorId = datum?.[encodingColor];
                const colorAlias = unfoldInfo.colorIdMap?.[colorId]?.alias ?? colorId;
                if (hasMultiMeasureGroup) {
                    if (hasMeasureIdInColor) return colorAlias;
                    const meaAlias = datum && datum[MeasureName] || '';
                    return unique([
                        colorAlias,
                        meaAlias
                    ]).join('-');
                }
                return colorAlias;
            },
            value: (v)=>{
                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);
            }
        }
    ];
};
const createMarkContent = (tooltip, dimensions = [], measures = [], foldInfo, unfoldInfo)=>{
    const dims = pipe(dimensions.filter((item)=>tooltip.includes(item.id)), uniqueBy((item)=>item.id), uniqueBy((item)=>item.alias));
    const meas = pipe(measures.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: (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 defaultContent = {
        visible: true,
        hasShape: true,
        shapeType: 'rectRound',
        key: (v)=>{
            const { measureName } = foldInfo;
            const { encodingColor: colorName } = unfoldInfo;
            const datum = v;
            return datum && datum[measureName || colorName] || '';
        },
        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);
            if (!measure) return String(value);
            const formatter = createFormatterByMeasure(measure);
            return formatter(value);
        }
    };
    return [
        ...dimContent,
        defaultContent,
        ...meaContent
    ];
};
export { createDimensionContent, createMarkContent, tooltip_tooltip as tooltip };

//# sourceMappingURL=tooltip.js.map