import { tooltip as external_tooltip_js_tooltip } from "./tooltip.js";
import { pipe, uniqueBy } from "remeda";
import { createFormatterByDimension, createFormatterByMeasure, findMeasureById } from "../../../../utils/index.js";
const tooltipTreeMap = (spec, context)=>{
    const result = external_tooltip_js_tooltip()(spec, context);
    const { advancedVSeed, vseed } = context;
    const { datasetReshapeInfo, dimensions = [], encoding } = advancedVSeed;
    const { foldInfo, unfoldInfo } = datasetReshapeInfo[0];
    if (result.tooltip) {
        if (!result.tooltip.mark) result.tooltip.mark = {};
        const formatterList = (encoding.hierarchy || []).map((id)=>{
            const dim = dimensions.find((item)=>item.id === id);
            return createFormatterByDimension(dim, advancedVSeed.locale);
        });
        result.tooltip.mark = {
            title: {
                visible: true,
                value: (val)=>val?.datum?.map((data, index)=>{
                        if (formatterList[index]) return formatterList[index](data.name);
                        return data.name;
                    }).join(' / ')
            },
            content: createMarkContent(encoding.tooltip || [], dimensions, vseed.measures, foldInfo, unfoldInfo, advancedVSeed.locale)
        };
    }
    return result;
};
const createMarkContent = (tooltip, dimensions = [], measures = [], foldInfo, unfoldInfo, locale)=>{
    const dims = pipe(dimensions.filter((item)=>tooltip.includes(item.id)), uniqueBy((item)=>item.id), uniqueBy((item)=>item.alias), 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: (v)=>{
                const { depth } = v;
                const datum = v?.datum[depth];
                return !!datum[item.id];
            },
            hasShape: true,
            shapeType: 'rectRound',
            key: (v)=>{
                const { depth } = v;
                const datum = v?.datum[depth];
                if (item.alias || item.id) return item.alias || item.id;
                return datum && datum[item.id];
            },
            value: (v)=>{
                const { depth } = v;
                const datum = v?.datum[depth];
                const formatter = createFormatterByDimension(item, locale);
                return datum ? formatter(datum[item.id]) : '';
            }
        }));
    const defaultContent = {
        visible: true,
        hasShape: true,
        shapeType: 'rectRound',
        key: (v)=>{
            const { depth } = v;
            const datum = v?.datum[depth];
            const { measureName } = foldInfo;
            const { encodingColor: colorName } = unfoldInfo;
            return datum && datum[measureName || colorName] || '';
        },
        value: (v)=>{
            const { depth } = v;
            const datum = v?.datum[depth];
            const { measureId, measureValue } = foldInfo;
            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);
        }
    };
    const meaContent = meas.map((item)=>({
            visible: true,
            hasShape: true,
            shapeType: 'rectRound',
            key: item.alias || item.id,
            value: (v)=>{
                const { depth } = v;
                const datum = v?.datum[depth];
                if (!datum) return '';
                const id = item.id;
                if (!datum) return '';
                const value = datum[id];
                const measure = findMeasureById(measures, id);
                const formatter = createFormatterByMeasure(measure);
                return formatter(value);
            }
        }));
    return [
        ...dimContent,
        defaultContent,
        ...meaContent
    ];
};
export { tooltipTreeMap };

//# sourceMappingURL=tooltipTreeMap.js.map