import { createFormatter, createFormatterByDimension, findMeasureById } from "../../../../utils/index.js";
import { isNumber, merge, uniqueBy } from "remeda";
import { MeasureId } from "../../../../../dataReshape/constant.js";
import { generateMeasurePercent, generateMeasureValue, label as external_label_js_label } from "./label.js";
const labelTreeMapLeaf = (spec, context)=>{
    const result = external_label_js_label(spec, context);
    const { advancedVSeed, vseed } = context;
    const { datasetReshapeInfo } = advancedVSeed;
    const { chartType, encoding } = advancedVSeed;
    const baseConfig = advancedVSeed.config[chartType];
    const foldInfoList = [
        datasetReshapeInfo[0].foldInfo
    ];
    const { label } = baseConfig;
    if (!label.enable) return result;
    const { showValue, showValuePercent, showDimension, autoFormat, numFormat = {}, wrap } = label;
    const hasDimLabelEncoding = vseed.dimensions?.some((item)=>encoding.label?.includes(item.id));
    const labelDims = uniqueBy(hasDimLabelEncoding ? vseed.dimensions?.filter((item)=>encoding.label?.includes(item.id)) || [] : showDimension ? advancedVSeed.dimensions.filter((d)=>d.id !== MeasureId && 'row' !== d.encoding && 'column' !== d.encoding) : [], (item)=>item.id);
    const labelMeas = uniqueBy(vseed.measures?.filter((item)=>encoding.label?.includes(item.id)) || [], (item)=>item.id);
    const percentFormat = merge(numFormat, {
        type: 'percent'
    });
    const percentFormatter = createFormatter(percentFormat);
    if (result.label) result.label.formatMethod = (_, datum)=>{
        const nodeName = datum.name;
        const dataArray = datum.datum;
        if (!dataArray || !Array.isArray(dataArray)) return '';
        const findDataNode = (nodes, name)=>{
            for (const node of nodes){
                if (node.name === name) return node;
                if (node.children) {
                    const found = findDataNode(node.children, name);
                    if (found) return found;
                }
            }
            return null;
        };
        const realDatum = findDataNode(dataArray, nodeName);
        if (!realDatum) return '';
        const resultText = [];
        const dimLabels = labelDims.map((item)=>{
            const id = item.id;
            const rawValue = realDatum[id] ?? realDatum.__OriginalData__?.[id];
            const formatter = createFormatterByDimension(item, advancedVSeed.locale);
            return formatter(rawValue);
        }).filter((v)=>null != v && '' !== v);
        resultText.push(...dimLabels);
        const meaLabels = labelMeas.map((item)=>generateMeasureValue(realDatum[item.id] ?? realDatum.__OriginalData__?.[item.id], item, autoFormat, numFormat));
        resultText.push(...meaLabels);
        foldInfoList.forEach((foldInfo)=>{
            const { measureId, measureValue, statistics } = foldInfo;
            const currentMeasureId = realDatum[measureId] ?? realDatum.__MeaId__;
            const measure = findMeasureById(advancedVSeed.measures, currentMeasureId);
            if (measure) {
                const val = realDatum[measureValue] ?? realDatum.__MeaValue__ ?? realDatum.value;
                const measureValueLabel = generateMeasureValue(val, measure, autoFormat, numFormat);
                if (showValue) resultText.push(measureValueLabel);
                if (showValuePercent) {
                    if (statistics && isNumber(statistics.sum)) resultText.push(generateMeasurePercent(val, statistics.sum, percentFormatter));
                }
            }
        });
        if (wrap) return resultText;
        return resultText.join(' ');
    };
    return result;
};
export { labelTreeMapLeaf };

//# sourceMappingURL=labelTreeMapLeaf.js.map