import { createNumFormatter, flatReshapeMeasures } from "../../../../utils/index.js";
import { createLinearFormat } from "./format/linearFormat.js";
import { linearAxisStyle } from "./linearAxisStyle.js";
import { FoldSecondaryMeasureValue } from "../../../../../dataReshape/index.js";
const yLinearSecondary = (spec, context)=>{
    const result = {
        ...spec
    };
    const { advancedVSeed, vseed } = context;
    const { chartType } = vseed;
    const { datasetReshapeInfo, reshapeMeasures = [] } = advancedVSeed;
    const { index, id: reshapeInfoId, foldInfoList } = datasetReshapeInfo[0];
    const secondaryYAxis = advancedVSeed.config?.[chartType]?.secondaryYAxis;
    const yAxisConfig = Array.isArray(secondaryYAxis) ? secondaryYAxis[index] || secondaryYAxis[0] : secondaryYAxis;
    const alignTicks = advancedVSeed.config?.[chartType]?.alignTicks;
    const alignTicksConfig = Array.isArray(alignTicks) ? alignTicks[index] || alignTicks[0] : alignTicks;
    if (!foldInfoList) return result;
    const secondaryFoldInfoList = foldInfoList.filter((f)=>f.measureValue.startsWith(FoldSecondaryMeasureValue));
    const isEmptySecondary = !secondaryFoldInfoList.length || secondaryFoldInfoList.every((foldInfo)=>!Object.keys(foldInfo.foldMap).length);
    const onlySecondary = !isEmptySecondary && foldInfoList.length && foldInfoList.every((f)=>f.measureValue.startsWith(FoldSecondaryMeasureValue) || 0 === Object.keys(f.foldMap).length);
    const sync = {
        axisId: `${reshapeInfoId}-primary-axis`,
        zeroAlign: true
    };
    const id = `${reshapeInfoId}-secondary-axis`;
    const seriesId = alignTicksConfig ? spec.series.map((s)=>s.id) : spec.series.filter((s)=>s.yField.startsWith(FoldSecondaryMeasureValue)).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 titleText = yAxisConfig?.title?.titleText || measures.filter((m)=>'secondaryYAxis' === m.encoding).map((m)=>m.alias ?? m.id).join(' & ');
    const baseStyle = linearAxisStyle({
        ...yAxisConfig,
        orient: 'right',
        formatMethod,
        titleText,
        id,
        seriesId,
        sync
    });
    const linearAxis = {
        ...baseStyle,
        visible: isEmptySecondary ? false : yAxisConfig?.visible ?? true,
        grid: {
            ...baseStyle.grid,
            visible: onlySecondary ? true : yAxisConfig?.grid?.visible
        }
    };
    result.axes = [
        ...result.axes,
        linearAxis
    ];
    return result;
};
export { yLinearSecondary };

//# sourceMappingURL=yLinearSecondary.js.map