"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    getSequentialSchemeColors: function() {
        return getSequentialSchemeColors;
    },
    getVegaColor: function() {
        return getVegaColor;
    },
    getVegaColorFromMap: function() {
        return getVegaColorFromMap;
    },
    isStandardVegaScheme: function() {
        return isStandardVegaScheme;
    }
});
const _colors = require("../../utilities/colors");
const _utilities = require("../../utilities/utilities");
/**
 * Vega-Lite Color Scheme to Fluent DataViz Palette Adapter
 *
 * Maps standard Vega-Lite color schemes to Fluent UI DataViz colors
 * Similar to PlotlyColorAdapter but for Vega-Lite specifications
 */ // Vega's category10 scheme (D3 Category10)
// https://vega.github.io/vega/docs/schemes/#categorical
const VEGA_CATEGORY10 = [
    '#1f77b4',
    '#ff7f0e',
    '#2ca02c',
    '#d62728',
    '#9467bd',
    '#8c564b',
    '#e377c2',
    '#7f7f7f',
    '#bcbd22',
    '#17becf'
];
// Vega's category20 scheme
const VEGA_CATEGORY20 = [
    '#1f77b4',
    '#aec7e8',
    '#ff7f0e',
    '#ffbb78',
    '#2ca02c',
    '#98df8a',
    '#d62728',
    '#ff9896',
    '#9467bd',
    '#c5b0d5',
    '#8c564b',
    '#c49c94',
    '#e377c2',
    '#f7b6d2',
    '#7f7f7f',
    '#c7c7c7',
    '#bcbd22',
    '#dbdb8d',
    '#17becf',
    '#9edae5'
];
// Tableau10 scheme (commonly used in Vega-Lite)
const VEGA_TABLEAU10 = [
    '#4e79a7',
    '#f28e2c',
    '#e15759',
    '#76b7b2',
    '#59a14f',
    '#edc949',
    '#af7aa1',
    '#ff9da7',
    '#9c755f',
    '#bab0ab'
];
// Tableau20 scheme
const VEGA_TABLEAU20 = [
    '#4e79a7',
    '#a0cbe8',
    '#f28e2c',
    '#ffbe7d',
    '#59a14f',
    '#8cd17d',
    '#b6992d',
    '#f1ce63',
    '#499894',
    '#86bcb6',
    '#e15759',
    '#ff9d9a',
    '#79706e',
    '#bab0ab',
    '#d37295',
    '#fabfd2',
    '#b07aa1',
    '#d4a6c8',
    '#9d7660',
    '#d7b5a6'
];
// Mapping from Vega category10 to Fluent DataViz tokens
const CATEGORY10_FLUENT_MAPPING = [
    _colors.DataVizPalette.color26,
    _colors.DataVizPalette.warning,
    _colors.DataVizPalette.color5,
    _colors.DataVizPalette.error,
    _colors.DataVizPalette.color4,
    _colors.DataVizPalette.color17,
    _colors.DataVizPalette.color22,
    _colors.DataVizPalette.disabled,
    _colors.DataVizPalette.color10,
    _colors.DataVizPalette.color3
];
// Mapping from Vega category20 to Fluent DataViz tokens
const CATEGORY20_FLUENT_MAPPING = [
    _colors.DataVizPalette.color26,
    _colors.DataVizPalette.color36,
    _colors.DataVizPalette.warning,
    _colors.DataVizPalette.color27,
    _colors.DataVizPalette.color5,
    _colors.DataVizPalette.color15,
    _colors.DataVizPalette.error,
    _colors.DataVizPalette.color32,
    _colors.DataVizPalette.color4,
    _colors.DataVizPalette.color24,
    _colors.DataVizPalette.color17,
    _colors.DataVizPalette.color37,
    _colors.DataVizPalette.color22,
    _colors.DataVizPalette.color12,
    _colors.DataVizPalette.disabled,
    _colors.DataVizPalette.color31,
    _colors.DataVizPalette.color10,
    _colors.DataVizPalette.color30,
    _colors.DataVizPalette.color3,
    _colors.DataVizPalette.color13
];
// Mapping from Tableau10 to Fluent DataViz tokens
const TABLEAU10_FLUENT_MAPPING = [
    _colors.DataVizPalette.color1,
    _colors.DataVizPalette.color7,
    _colors.DataVizPalette.error,
    _colors.DataVizPalette.color3,
    _colors.DataVizPalette.color5,
    _colors.DataVizPalette.color10,
    _colors.DataVizPalette.color4,
    _colors.DataVizPalette.color2,
    _colors.DataVizPalette.color17,
    _colors.DataVizPalette.disabled
];
// Mapping from Tableau20 to Fluent DataViz tokens
const TABLEAU20_FLUENT_MAPPING = [
    _colors.DataVizPalette.color1,
    _colors.DataVizPalette.color11,
    _colors.DataVizPalette.color7,
    _colors.DataVizPalette.color27,
    _colors.DataVizPalette.color5,
    _colors.DataVizPalette.color15,
    _colors.DataVizPalette.color10,
    _colors.DataVizPalette.color30,
    _colors.DataVizPalette.color3,
    _colors.DataVizPalette.color13,
    _colors.DataVizPalette.error,
    _colors.DataVizPalette.color32,
    _colors.DataVizPalette.disabled,
    _colors.DataVizPalette.color31,
    _colors.DataVizPalette.color2,
    _colors.DataVizPalette.color12,
    _colors.DataVizPalette.color4,
    _colors.DataVizPalette.color24,
    _colors.DataVizPalette.color17,
    _colors.DataVizPalette.color37
];
/**
 * Gets the Fluent color mapping for a given Vega-Lite color scheme
 */ function getSchemeMapping(scheme) {
    if (!scheme) {
        return undefined;
    }
    const schemeLower = scheme.toLowerCase();
    switch(schemeLower){
        case 'category10':
            return CATEGORY10_FLUENT_MAPPING;
        case 'category20':
        case 'category20b':
        case 'category20c':
            return CATEGORY20_FLUENT_MAPPING;
        case 'tableau10':
            return TABLEAU10_FLUENT_MAPPING;
        case 'tableau20':
            return TABLEAU20_FLUENT_MAPPING;
        // For unsupported schemes, fall back to default Fluent palette
        case 'accent':
        case 'dark2':
        case 'paired':
        case 'pastel1':
        case 'pastel2':
        case 'set1':
        case 'set2':
        case 'set3':
            // Color schemes not yet mapped to Fluent colors. Using default palette.
            return undefined;
        default:
            return undefined;
    }
}
function getVegaColor(index, scheme, range, isDarkTheme = false) {
    // Priority 1: Custom range (highest priority)
    if (range && range.length > 0) {
        return range[index % range.length];
    }
    // Priority 2: Named color scheme mapped to Fluent
    const schemeMapping = getSchemeMapping(scheme);
    if (schemeMapping) {
        const token = schemeMapping[index % schemeMapping.length];
        return (0, _colors.getColorFromToken)(token, isDarkTheme);
    }
    // Priority 3: Default Fluent qualitative palette
    return (0, _colors.getNextColor)(index, 0, isDarkTheme);
}
function getVegaColorFromMap(legendLabel, colorMap, scheme, range, isDarkTheme = false) {
    var _colorMap_current, _colorMap_current1, _colorMap_current2;
    // Check if color is already assigned
    if ((_colorMap_current = colorMap.current) === null || _colorMap_current === void 0 ? void 0 : _colorMap_current.has(legendLabel)) {
        return colorMap.current.get(legendLabel);
    }
    var _colorMap_current_size;
    // Assign new color based on current map size
    const index = (_colorMap_current_size = (_colorMap_current1 = colorMap.current) === null || _colorMap_current1 === void 0 ? void 0 : _colorMap_current1.size) !== null && _colorMap_current_size !== void 0 ? _colorMap_current_size : 0;
    const color = getVegaColor(index, scheme, range, isDarkTheme);
    (_colorMap_current2 = colorMap.current) === null || _colorMap_current2 === void 0 ? void 0 : _colorMap_current2.set(legendLabel, color);
    return color;
}
/**
 * Sequential and diverging color scheme ramps for heatmaps and continuous color scales.
 * Each ramp is a 5-point gradient matching D3/Vega defaults.
 */ const SEQUENTIAL_SCHEMES = {
    blues: [
        '#deebf7',
        '#9ecae1',
        '#4292c6',
        '#2171b5',
        '#084594'
    ],
    greens: [
        '#e5f5e0',
        '#a1d99b',
        '#41ab5d',
        '#238b45',
        '#005a32'
    ],
    reds: [
        '#fee0d2',
        '#fc9272',
        '#ef3b2c',
        '#cb181d',
        '#99000d'
    ],
    oranges: [
        '#feedde',
        '#fdbe85',
        '#fd8d3c',
        '#e6550d',
        '#a63603'
    ],
    purples: [
        '#efedf5',
        '#bcbddc',
        '#807dba',
        '#6a51a3',
        '#4a1486'
    ],
    greys: [
        '#f0f0f0',
        '#bdbdbd',
        '#969696',
        '#636363',
        '#252525'
    ],
    viridis: [
        '#440154',
        '#3b528b',
        '#21918c',
        '#5ec962',
        '#fde725'
    ],
    inferno: [
        '#000004',
        '#57106e',
        '#bc3754',
        '#f98c0a',
        '#fcffa4'
    ],
    magma: [
        '#000004',
        '#51127c',
        '#b73779',
        '#fc8961',
        '#fcfdbf'
    ],
    plasma: [
        '#0d0887',
        '#7e03a8',
        '#cc4778',
        '#f89540',
        '#f0f921'
    ],
    greenblue: [
        '#e0f3db',
        '#a8ddb5',
        '#4eb3d3',
        '#2b8cbe',
        '#08589e'
    ],
    yellowgreen: [
        '#ffffcc',
        '#c2e699',
        '#78c679',
        '#31a354',
        '#006837'
    ],
    yellowgreenblue: [
        '#ffffcc',
        '#a1dab4',
        '#41b6c4',
        '#2c7fb8',
        '#253494'
    ],
    redyellowgreen: [
        '#d73027',
        '#fc8d59',
        '#fee08b',
        '#91cf60',
        '#1a9850'
    ],
    blueorange: [
        '#2166ac',
        '#67a9cf',
        '#f7f7f7',
        '#f4a582',
        '#b2182b'
    ],
    redblue: [
        '#ca0020',
        '#f4a582',
        '#f7f7f7',
        '#92c5de',
        '#0571b0'
    ]
};
function getSequentialSchemeColors(scheme, steps = 5) {
    const ramp = SEQUENTIAL_SCHEMES[scheme.toLowerCase()];
    if (!ramp) {
        return undefined;
    }
    if (steps === ramp.length) {
        return [
            ...ramp
        ];
    }
    // Interpolate to requested number of steps
    const result = [];
    for(let i = 0; i < steps; i++){
        const t = steps === 1 ? 0.5 : i / (steps - 1);
        const pos = t * (ramp.length - 1);
        const lo = Math.floor(pos);
        const hi = Math.min(lo + 1, ramp.length - 1);
        const frac = pos - lo;
        if (frac === 0) {
            result.push(ramp[lo]);
        } else {
            result.push(interpolateHexColor(ramp[lo], ramp[hi], frac));
        }
    }
    return result;
}
/**
 * Linearly interpolates between two hex colors
 */ function interpolateHexColor(c1, c2, t) {
    const r1 = parseInt(c1.slice(1, 3), 16);
    const g1 = parseInt(c1.slice(3, 5), 16);
    const b1 = parseInt(c1.slice(5, 7), 16);
    const r2 = parseInt(c2.slice(1, 3), 16);
    const g2 = parseInt(c2.slice(3, 5), 16);
    const b2 = parseInt(c2.slice(5, 7), 16);
    const r = Math.round(r1 + (r2 - r1) * t);
    const g = Math.round(g1 + (g2 - g1) * t);
    const b = Math.round(b1 + (b2 - b1) * t);
    return `#${r.toString(16).padStart(2, '0')}${g.toString(16).padStart(2, '0')}${b.toString(16).padStart(2, '0')}`;
}
function isStandardVegaScheme(range) {
    if (!range || range.length === 0) {
        return undefined;
    }
    const rangeLower = range.map((c)=>c.toLowerCase());
    if ((0, _utilities.areArraysEqual)(rangeLower, VEGA_CATEGORY10)) {
        return 'category10';
    }
    if ((0, _utilities.areArraysEqual)(rangeLower, VEGA_CATEGORY20)) {
        return 'category20';
    }
    if ((0, _utilities.areArraysEqual)(rangeLower, VEGA_TABLEAU10)) {
        return 'tableau10';
    }
    if ((0, _utilities.areArraysEqual)(rangeLower, VEGA_TABLEAU20)) {
        return 'tableau20';
    }
    return undefined;
}
