import tinycolor2 from "tinycolor2";
import { registerAll } from "../builder/register/all.js";
import { registerCustomTheme } from "../builder/register/theme/index.js";
import { darkTheme } from "./dark/index.js";
import { lightTheme } from "./light/index.js";
const raceChartTypes = [
    'raceBar',
    'raceColumn',
    'raceScatter',
    'raceLine',
    'racePie',
    'raceDonut'
];
let hasEnsuredRegisterAll = false;
const isRecord = (value)=>'object' == typeof value && null !== value && !Array.isArray(value);
const mergeThemeNode = (base, patch)=>{
    if (!isRecord(base) || !isRecord(patch)) return patch;
    const result = {
        ...base
    };
    for (const [key, patchValue] of Object.entries(patch)){
        if (void 0 === patchValue) continue;
        const currentValue = result[key];
        if (Array.isArray(patchValue)) {
            result[key] = [
                ...patchValue
            ];
            continue;
        }
        if (isRecord(currentValue) && isRecord(patchValue)) {
            result[key] = mergeThemeNode(currentValue, patchValue);
            continue;
        }
        result[key] = patchValue;
    }
    return result;
};
const withAlpha = (color, alpha)=>tinycolor2(color).setAlpha(alpha).toRgbString();
const ensureRegisterAll = (options)=>{
    if (options?.ensureRegisterAll === false || hasEnsuredRegisterAll) return;
    registerAll();
    hasEnsuredRegisterAll = true;
};
const getAccentColor = (tokens)=>tokens.accentColor || tokens.colorScheme[0];
const getAxisPatch = (tokens)=>({
        label: {
            labelColor: tokens.axisLabelColor || tokens.textSecondary,
            labelFontSize: tokens.axisFontSize
        },
        title: {
            titleColor: tokens.axisTitleColor || tokens.textSecondary,
            titleFontSize: tokens.axisFontSize
        },
        grid: {
            gridColor: tokens.axisGridColor || tokens.borderColor
        },
        tick: {
            tickColor: tokens.axisLineColor || tokens.borderColor
        },
        line: {
            lineColor: tokens.axisLineColor || tokens.borderColor
        }
    });
const getPivotGridPatch = (tokens)=>({
        borderColor: tokens.tableBorderColor || tokens.borderColor,
        bodyFontSize: tokens.tableBodyFontSize,
        bodyFontColor: tokens.tableBodyFontColor || tokens.textPrimary,
        headerFontSize: tokens.tableHeaderFontSize,
        headerFontColor: tokens.tableHeaderFontColor || tokens.textPrimary,
        headerBackgroundColor: tokens.tableHeaderBackgroundColor || tokens.surfaceColor || 'transparent',
        hoverHeaderBackgroundColor: tokens.tableHoverHeaderBackgroundColor || withAlpha(getAccentColor(tokens), 0.18),
        hoverHeaderInlineBackgroundColor: tokens.tableHoverHeaderInlineBackgroundColor || withAlpha(getAccentColor(tokens), 0.08),
        titleFontColor: tokens.textPrimary,
        titleFontSize: tokens.tableHeaderFontSize,
        chartGridColor: tokens.axisGridColor || tokens.borderColor,
        axisLabelColor: tokens.axisLabelColor || tokens.textSecondary,
        axisLabelFontSize: tokens.axisFontSize
    });
const getPlayerPatch = (tokens)=>{
    const accentColor = getAccentColor(tokens);
    return {
        fontFamily: tokens.fontFamily,
        fontSize: tokens.playerFontSize,
        railColor: tokens.playerRailColor || tokens.borderColor,
        trackColor: accentColor,
        sliderHandleColor: tokens.playerSliderHandleColor || tokens.surfaceColor || '#ffffff',
        sliderHandleBorderColor: tokens.playerSliderHandleBorderColor || accentColor,
        startButtonColor: accentColor,
        pauseButtonColor: accentColor,
        backwardButtonColor: accentColor,
        forwardButtonColor: accentColor
    };
};
const getTablePatch = (tokens)=>{
    const accentColor = getAccentColor(tokens);
    return {
        borderColor: tokens.tableBorderColor || tokens.borderColor,
        bodyFontSize: tokens.tableBodyFontSize,
        bodyFontFamily: tokens.fontFamily,
        bodyFontColor: tokens.tableBodyFontColor || tokens.textPrimary,
        headerFontSize: tokens.tableHeaderFontSize,
        headerFontFamily: tokens.fontFamily,
        headerFontColor: tokens.tableHeaderFontColor || tokens.textPrimary,
        headerBackgroundColor: tokens.tableHeaderBackgroundColor || tokens.surfaceColor || 'transparent',
        hoverBodyBackgroundColor: tokens.tableHoverBodyBackgroundColor || withAlpha(accentColor, 0.18),
        hoverBodyInlineBackgroundColor: tokens.tableHoverBodyInlineBackgroundColor || withAlpha(accentColor, 0.08),
        hoverHeaderBackgroundColor: tokens.tableHoverHeaderBackgroundColor || withAlpha(accentColor, 0.18),
        hoverHeaderInlineBackgroundColor: tokens.tableHoverHeaderInlineBackgroundColor || withAlpha(accentColor, 0.08),
        selectedBorderColor: tokens.tableSelectedBorderColor || accentColor,
        selectedBackgroundColor: tokens.tableSelectedBackgroundColor || withAlpha(accentColor, 0.12),
        backgroundColor: tokens.surfaceBackgroundColor || 'transparent',
        barAxisColor: tokens.axisLineColor || tokens.borderColor,
        backgroundColorScale: {
            minColor: tokens.linearColorScheme[0],
            maxColor: tokens.linearColorScheme[1]
        }
    };
};
const getChartPatch = (tokens)=>({
        backgroundColor: 'transparent',
        fontFamily: tokens.fontFamily,
        color: {
            colorScheme: [
                ...tokens.colorScheme
            ],
            linearColorScheme: [
                ...tokens.linearColorScheme
            ],
            positiveColor: tokens.positiveColor,
            negativeColor: tokens.negativeColor
        },
        label: {
            labelFontSize: tokens.labelFontSize,
            labelColor: tokens.labelColor || tokens.textPrimary,
            labelStroke: tokens.labelStroke
        },
        legend: {
            labelColor: tokens.legendLabelColor || tokens.textSecondary,
            labelFontSize: tokens.legendFontSize,
            pagerIconColor: tokens.legendPagerIconColor || tokens.textSecondary,
            pagerIconDisableColor: tokens.legendPagerIconDisableColor || tokens.borderColor
        },
        tooltip: {
            backgroundColor: tokens.tooltipBackgroundColor,
            borderColor: tokens.tooltipBorderColor || tokens.borderColor,
            fontSize: tokens.tooltipFontSize,
            keyColor: tokens.textSecondary,
            valueColor: tokens.textPrimary,
            titleColor: tokens.textPrimary
        }
    });
const getAnnotationPatch = (tokens)=>({
        annotationPoint: {
            textFontSize: tokens.labelFontSize
        },
        annotationHorizontalLine: {
            textFontSize: tokens.labelFontSize
        },
        annotationVerticalLine: {
            textFontSize: tokens.labelFontSize
        },
        annotationDifferenceLine: {
            textFontSize: tokens.labelFontSize
        },
        annotationArea: {
            textFontSize: tokens.labelFontSize
        }
    });
const getRegressionLinePatch = (tokens)=>({
        kdeRegressionLine: {
            textFontSize: tokens.labelFontSize
        },
        ecdfRegressionLine: {
            textFontSize: tokens.labelFontSize
        },
        linearRegressionLine: {
            textFontSize: tokens.labelFontSize
        },
        lowessRegressionLine: {
            textFontSize: tokens.labelFontSize
        },
        polynomialRegressionLine: {
            textFontSize: tokens.labelFontSize
        },
        logisticRegressionLine: {
            textFontSize: tokens.labelFontSize
        }
    });
const withAxesAndExtras = (chartType, chartConfig, tokens)=>{
    let nextChartConfig = mergeThemeNode(chartConfig, getChartPatch(tokens));
    const chartRecord = nextChartConfig;
    if (Object.prototype.hasOwnProperty.call(chartRecord, 'xAxis')) nextChartConfig = mergeThemeNode(nextChartConfig, {
        xAxis: getAxisPatch(tokens)
    });
    if (Object.prototype.hasOwnProperty.call(chartRecord, 'yAxis')) nextChartConfig = mergeThemeNode(nextChartConfig, {
        yAxis: getAxisPatch(tokens)
    });
    if (Object.prototype.hasOwnProperty.call(chartRecord, 'primaryYAxis')) nextChartConfig = mergeThemeNode(nextChartConfig, {
        primaryYAxis: getAxisPatch(tokens)
    });
    if (Object.prototype.hasOwnProperty.call(chartRecord, 'secondaryYAxis')) nextChartConfig = mergeThemeNode(nextChartConfig, {
        secondaryYAxis: getAxisPatch(tokens)
    });
    if (Object.prototype.hasOwnProperty.call(chartRecord, 'pivotGrid')) nextChartConfig = mergeThemeNode(nextChartConfig, {
        pivotGrid: getPivotGridPatch(tokens)
    });
    if (Object.prototype.hasOwnProperty.call(chartRecord, 'annotation')) nextChartConfig = mergeThemeNode(nextChartConfig, {
        annotation: getAnnotationPatch(tokens)
    });
    if (Object.prototype.hasOwnProperty.call(chartRecord, 'regressionLine')) nextChartConfig = mergeThemeNode(nextChartConfig, {
        regressionLine: getRegressionLinePatch(tokens)
    });
    if (raceChartTypes.includes(chartType) && Object.prototype.hasOwnProperty.call(chartRecord, 'player')) nextChartConfig = mergeThemeNode(nextChartConfig, {
        player: getPlayerPatch(tokens)
    });
    return nextChartConfig;
};
const createTokenThemeConfig = (tokens)=>{
    const baseTheme = 'dark' === tokens.baseTheme ? darkTheme() : lightTheme();
    const baseConfig = baseTheme.config || {};
    const nextConfig = {};
    for (const chartType of Object.keys(baseConfig)){
        const chartConfig = baseConfig[chartType];
        if (chartConfig) {
            if ('table' === chartType || 'pivotTable' === chartType) {
                nextConfig[chartType] = mergeThemeNode(chartConfig, getTablePatch(tokens));
                continue;
            }
            nextConfig[chartType] = withAxesAndExtras(chartType, chartConfig, tokens);
        }
    }
    return {
        config: nextConfig
    };
};
const registerTokenTheme = (themeName, tokens, options)=>{
    ensureRegisterAll(options);
    registerCustomTheme(themeName, createTokenThemeConfig(tokens));
};
const registerTokenThemes = (themes, options)=>{
    ensureRegisterAll(options);
    for (const [themeName, tokens] of Object.entries(themes))registerCustomTheme(themeName, createTokenThemeConfig(tokens));
};
export { createTokenThemeConfig, registerTokenTheme, registerTokenThemes };

//# sourceMappingURL=tokenTheme.js.map