import { defaultTitleText } from "./title/defaultTitleText.js";
import { MeasureId } from "../../../../../dataReshape/index.js";
import { isArray, isNil } from "@visactor/vutils";
import { bandAxisStyle } from "./bandAxisStyle.js";
import { createFormatterByDimension } from "../../../../utils/index.js";
const yBand = (spec, context)=>{
    const result = {
        ...spec
    };
    const { advancedVSeed, vseed } = context;
    const { chartType } = vseed;
    const { measures = [], dimensions = [], encoding, datasetReshapeInfo, pivotAllDatasetReshapeInfo } = advancedVSeed;
    const config = advancedVSeed.config?.[chartType]?.yAxis ?? {};
    if (!result.axes) result.axes = [];
    const { labelAutoLimitLength = 80 } = config;
    const onlyMeasureId = 0 === (encoding.y || []).filter((v)=>v !== MeasureId).length;
    const bandAxis = bandAxisStyle(config);
    bandAxis.type = 'band';
    bandAxis.orient = 'left';
    bandAxis.maxWidth = labelAutoLimitLength + 60;
    if (bandAxis.label) bandAxis.label.containerAlign = 'right';
    bandAxis.paddingInner = [
        0.15,
        0.1
    ];
    bandAxis.paddingOuter = [
        0.075,
        0.1
    ];
    if (isNil(bandAxis.title?.text)) bandAxis.title.text = defaultTitleText(measures, dimensions, encoding.y);
    if (bandAxis.label) {
        const allDatasetReshapeInfo = pivotAllDatasetReshapeInfo || datasetReshapeInfo;
        const colorIdMap = allDatasetReshapeInfo.reduce((prev, cur)=>({
                ...prev,
                ...cur.unfoldInfo.colorIdMap
            }), {});
        const dimensionMap = new Map(dimensions.map((item)=>[
                item.id,
                item
            ]));
        const dimIds = (encoding.y || []).filter((v)=>v !== MeasureId);
        const dimFormatter = dimIds.length ? createFormatterByDimension(dimensionMap.get(dimIds[0]), advancedVSeed.locale) : null;
        bandAxis.label.formatMethod = (text)=>{
            if (isArray(text)) return text;
            if (onlyMeasureId) return colorIdMap[String(text)]?.alias ?? text;
            const rawText = String(text ?? '');
            if (!dimFormatter) return rawText;
            return dimFormatter(rawText);
        };
    }
    result.axes = [
        ...result.axes,
        bandAxis
    ];
    return result;
};
export { yBand };

//# sourceMappingURL=yBand.js.map