"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, {
    D3_FLUENTVIZ_COLORWAY_MAPPING: function() {
        return D3_FLUENTVIZ_COLORWAY_MAPPING;
    },
    DEFAULT_D3_COLORWAY: function() {
        return DEFAULT_D3_COLORWAY;
    },
    createColorScale: function() {
        return createColorScale;
    },
    extractColor: function() {
        return extractColor;
    },
    getColor: function() {
        return getColor;
    },
    getOpacity: function() {
        return getOpacity;
    },
    getSchemaColors: function() {
        return getSchemaColors;
    },
    resolveColor: function() {
        return resolveColor;
    }
});
const _d3color = require("d3-color");
const _chartutilities = require("@fluentui/chart-utilities");
const _utilities = require("../../utilities/utilities");
const _colors = require("../../utilities/colors");
const _d3scale = require("d3-scale");
const DEFAULT_PLOTLY_COLORWAY = [
    '#636efa',
    '#ef553b',
    '#00cc96',
    '#ab63fa',
    '#ffa15a',
    '#19d3f3',
    '#ff6692',
    '#b6e880',
    '#ff97ff',
    '#fecb52'
];
const DEFAULT_D3_COLORWAY = [
    '#1f77b4',
    '#ff7f0e',
    '#2ca02c',
    '#d62728',
    '#9467bd',
    '#8c564b',
    '#e377c2',
    '#7f7f7f',
    '#bcbd22',
    '#17becf'
];
const PLOTLY_FLUENTVIZ_COLORWAY_MAPPING = [
    _colors.DataVizPalette.color1,
    _colors.DataVizPalette.warning,
    _colors.DataVizPalette.color8,
    _colors.DataVizPalette.color4,
    _colors.DataVizPalette.color7,
    _colors.DataVizPalette.color6,
    _colors.DataVizPalette.color2,
    _colors.DataVizPalette.color5,
    _colors.DataVizPalette.color9,
    _colors.DataVizPalette.color10
];
const D3_FLUENTVIZ_COLORWAY_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
];
function getPlotlyColorway(colorway, isDonut = false) {
    if (!colorway || !(0, _chartutilities.isArrayOrTypedArray)(colorway)) {
        return 'others';
    }
    const lower = colorway.map((c)=>c.toLowerCase());
    if (isDonut && (0, _utilities.areArraysEqual)(lower, D3_FLUENTVIZ_COLORWAY_MAPPING)) {
        return 'd3';
    }
    if ((0, _utilities.areArraysEqual)(lower, DEFAULT_PLOTLY_COLORWAY)) {
        return 'plotly';
    }
    return 'others';
}
function tryMapFluentDataViz(hexColor, templateColorway, isDarkTheme, isDonut) {
    if (templateColorway !== 'plotly') {
        return hexColor;
    }
    let defaultColorway = DEFAULT_PLOTLY_COLORWAY;
    let defaultMapping = PLOTLY_FLUENTVIZ_COLORWAY_MAPPING;
    if (isDonut) {
        defaultColorway = templateColorway === 'plotly' ? DEFAULT_PLOTLY_COLORWAY : DEFAULT_D3_COLORWAY;
        defaultMapping = templateColorway === 'plotly' ? PLOTLY_FLUENTVIZ_COLORWAY_MAPPING : D3_FLUENTVIZ_COLORWAY_MAPPING;
    }
    const idx = defaultColorway.indexOf(hexColor.toLowerCase());
    if (idx !== -1) {
        return (0, _colors.getColorFromToken)(defaultMapping[idx], !!isDarkTheme);
    }
    return hexColor;
}
const getColor = (legendLabel, colorMap, templateColorway, isDarkTheme, isDonut)=>{
    if (!colorMap.current.has(legendLabel)) {
        let nextColor;
        const defaultColorMapping = isDonut ? templateColorway === 'plotly' ? PLOTLY_FLUENTVIZ_COLORWAY_MAPPING : D3_FLUENTVIZ_COLORWAY_MAPPING : PLOTLY_FLUENTVIZ_COLORWAY_MAPPING;
        if (colorMap.current.size < defaultColorMapping.length) {
            // Get first 10 colors from plotly-fluentviz colorway mapping
            nextColor = (0, _colors.getColorFromToken)(defaultColorMapping[colorMap.current.size], isDarkTheme);
        } else {
            nextColor = (0, _colors.getNextColor)(colorMap.current.size, 0, isDarkTheme);
        }
        colorMap.current.set(legendLabel, nextColor);
        return nextColor;
    }
    return colorMap.current.get(legendLabel);
};
const getSchemaColors = (colorway, colors, colorMap, isDarkTheme, isDonut)=>{
    const hexColors = [];
    if (!colors) {
        return undefined;
    }
    const templateColorway = getPlotlyColorway(colorway, isDonut);
    if ((0, _chartutilities.isArrayOrTypedArray)(colors)) {
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        colors.forEach((element, index)=>{
            const colorString = element === null || element === void 0 ? void 0 : element.toString().trim();
            const nextFluentColor = getColor(`Label_${index}`, colorMap, templateColorway, isDarkTheme, isDonut);
            if (colorString) {
                const parsedColor = (0, _d3color.color)(colorString);
                hexColors.push(parsedColor ? tryMapFluentDataViz(parsedColor.formatHex(), templateColorway, isDarkTheme) : nextFluentColor);
            } else {
                hexColors.push(nextFluentColor);
            }
        });
    } else if (typeof colors === 'string') {
        const parsedColor = (0, _d3color.color)(colors);
        return parsedColor ? tryMapFluentDataViz(parsedColor.formatHex(), templateColorway, isDarkTheme) : getColor('Label_0', colorMap, templateColorway, isDarkTheme, isDonut);
    }
    return hexColors;
};
const extractColor = (colorway, colorwayType, colors, colorMap, isDarkTheme, isDonut)=>{
    return colorwayType === 'default' && colors ? getSchemaColors(colorway, colors, colorMap, isDarkTheme, isDonut) : undefined;
};
const resolveColor = (extractedColors, index, legend, colorMap, colorway, isDarkTheme, isDonut)=>{
    let color = '';
    const templateColorway = getPlotlyColorway(colorway, isDonut);
    if (extractedColors && (0, _chartutilities.isArrayOrTypedArray)(extractedColors) && extractedColors.length > 0) {
        color = extractedColors[index % extractedColors.length];
    } else if (typeof extractedColors === 'string') {
        color = extractedColors;
    } else {
        color = getColor(legend, colorMap, templateColorway, isDarkTheme, isDonut);
    }
    return color;
};
const getOpacity = (series, index)=>{
    var _series_marker, _series_marker1, _series_marker2, _series_marker3, _series_marker4;
    var _series_opacity;
    return ((_series_marker = series.marker) === null || _series_marker === void 0 ? void 0 : _series_marker.opacity) ? (0, _chartutilities.isArrayOrTypedArray)((_series_marker1 = series.marker) === null || _series_marker1 === void 0 ? void 0 : _series_marker1.opacity) ? ((_series_marker2 = series.marker) === null || _series_marker2 === void 0 ? void 0 : _series_marker2.opacity)[index % ((_series_marker3 = series.marker) === null || _series_marker3 === void 0 ? void 0 : _series_marker3.opacity).length] : (_series_marker4 = series.marker) === null || _series_marker4 === void 0 ? void 0 : _series_marker4.opacity : (_series_opacity = series.opacity) !== null && _series_opacity !== void 0 ? _series_opacity : 1;
};
const createColorScale = (layout, series, currentColorScale)=>{
    var _layout_coloraxis_colorscale, _layout_coloraxis, _series_marker, _series_marker1, _series_marker_color, _series_marker2;
    if ((layout === null || layout === void 0 ? void 0 : (_layout_coloraxis = layout.coloraxis) === null || _layout_coloraxis === void 0 ? void 0 : (_layout_coloraxis_colorscale = _layout_coloraxis.colorscale) === null || _layout_coloraxis_colorscale === void 0 ? void 0 : _layout_coloraxis_colorscale.length) && (0, _chartutilities.isArrayOrTypedArray)((_series_marker = series.marker) === null || _series_marker === void 0 ? void 0 : _series_marker.color) && ((_series_marker1 = series.marker) === null || _series_marker1 === void 0 ? void 0 : _series_marker1.color).length > 0 && typeof ((_series_marker2 = series.marker) === null || _series_marker2 === void 0 ? void 0 : (_series_marker_color = _series_marker2.color) === null || _series_marker_color === void 0 ? void 0 : _series_marker_color[0]) === 'number') {
        var _layout_coloraxis1, _series_marker3, _layout_coloraxis2, _layout_coloraxis3;
        const scale = layout === null || layout === void 0 ? void 0 : (_layout_coloraxis1 = layout.coloraxis) === null || _layout_coloraxis1 === void 0 ? void 0 : _layout_coloraxis1.colorscale;
        const colorValues = (_series_marker3 = series.marker) === null || _series_marker3 === void 0 ? void 0 : _series_marker3.color;
        var _layout_coloraxis_cmin, _layout_coloraxis_cmax;
        const [dMin, dMax] = [
            (_layout_coloraxis_cmin = layout === null || layout === void 0 ? void 0 : (_layout_coloraxis2 = layout.coloraxis) === null || _layout_coloraxis2 === void 0 ? void 0 : _layout_coloraxis2.cmin) !== null && _layout_coloraxis_cmin !== void 0 ? _layout_coloraxis_cmin : Math.min(...colorValues),
            (_layout_coloraxis_cmax = layout === null || layout === void 0 ? void 0 : (_layout_coloraxis3 = layout.coloraxis) === null || _layout_coloraxis3 === void 0 ? void 0 : _layout_coloraxis3.cmax) !== null && _layout_coloraxis_cmax !== void 0 ? _layout_coloraxis_cmax : Math.max(...colorValues)
        ];
        // Normalize colorscale domain to actual data domain
        const scaleDomain = scale.map(([pos])=>dMin + pos * (dMax - dMin));
        const scaleColors = scale.map((item)=>item[1]);
        return (0, _d3scale.scaleLinear)().domain(scaleDomain).range(scaleColors);
    }
    return currentColorScale;
};
