import { createNumFormatter, flatReshapeMeasures } from "../../../../utils/index.js";
import { createLinearFormat } from "./format/linearFormat.js";
import { linearAxisStyle } from "./linearAxisStyle.js";
import { FoldPrimaryMeasureValue } from "../../../../../dataReshape/index.js";
const yLinearPrimary = (spec, context)=>{
    const result = {
        ...spec
    };
    const { advancedVSeed, vseed } = context;
    const { chartType } = vseed;
    const { datasetReshapeInfo, reshapeMeasures = [] } = advancedVSeed;
    const { index, id: reshapeInfoId, foldInfoList } = datasetReshapeInfo[0];
    const primaryYAxis = advancedVSeed.config?.[chartType]?.primaryYAxis;
    const yAxisConfig = Array.isArray(primaryYAxis) ? primaryYAxis[index] || primaryYAxis[0] : primaryYAxis;
    const alignTicks = advancedVSeed.config?.[chartType]?.alignTicks;
    const alignTicksConfig = Array.isArray(alignTicks) ? alignTicks[index] || alignTicks[0] : alignTicks;
    if (!foldInfoList) return result;
    const primaryFoldInfoList = foldInfoList.filter((f)=>f.measureValue.startsWith(FoldPrimaryMeasureValue));
    const isEmpty = !primaryFoldInfoList.length || primaryFoldInfoList.every((foldInfo)=>!Object.keys(foldInfo.foldMap).length);
    const id = `${reshapeInfoId}-primary-axis`;
    const seriesId = alignTicksConfig ? spec.series.map((s)=>s.id) : spec.series.filter((s)=>s.yField.startsWith(FoldPrimaryMeasureValue)).map((s)=>s.id);
    if (!result.axes) result.axes = [];
    const { autoFormat, numFormat = {} } = yAxisConfig ?? {};
    const formatter = createNumFormatter(numFormat);
    const formatMethod = (value)=>createLinearFormat(value, autoFormat, numFormat, formatter);
    const measures = flatReshapeMeasures(reshapeMeasures);
    const measureIds = primaryFoldInfoList.reduce((res, foldInfo)=>{
        Object.keys(foldInfo.foldMap).forEach((k)=>{
            if (!res.includes(k)) res.push(k);
        });
        return res;
    }, []);
    const titleText = yAxisConfig?.title?.titleText || measures.filter((m)=>'primaryYAxis' === m.encoding && measureIds.includes(m.id)).map((m)=>m.alias ?? m.id).join(' & ');
    const linearAxis = {
        ...linearAxisStyle({
            ...yAxisConfig,
            orient: 'left',
            formatMethod,
            titleText,
            id,
            seriesId
        }),
        visible: isEmpty ? false : yAxisConfig?.visible ?? true
    };
    result.axes = [
        ...result.axes,
        linearAxis
    ];
    return result;
};
export { yLinearPrimary };

//# sourceMappingURL=yLinearPrimary.js.map