import { color as d3Color } from 'd3-color';
import { isArrayOrTypedArray } from '@fluentui/chart-utilities';
import { areArraysEqual } from '../../utilities/utilities';
import { DataVizPalette, getColorFromToken, getNextColor } from '../../utilities/colors';
var DEFAULT_PLOTLY_COLORWAY = [
    '#636efa', //1
    '#ef553b', //2
    '#00cc96', //3
    '#ab63fa', //4
    '#ffa15a', //5
    '#19d3f3', //6
    '#ff6692', //7
    '#b6e880', //8
    '#ff97ff', //9
    '#fecb52', //10
];
var PLOTLY_FLUENTVIZ_COLORWAY_MAPPING = [
    DataVizPalette.color1, //1
    DataVizPalette.warning, //2
    DataVizPalette.color8, //3
    DataVizPalette.color4, //4
    DataVizPalette.color7, //5
    DataVizPalette.color6, //6
    DataVizPalette.color2, //7
    DataVizPalette.color5, //8
    DataVizPalette.color9, //9
    DataVizPalette.color10, //10
];
function getPlotlyColorway(colorway) {
    var isPlotlyColorway = isArrayOrTypedArray(colorway) &&
        areArraysEqual(colorway === null || colorway === void 0 ? void 0 : colorway.map(function (c) { return c.toLowerCase(); }), DEFAULT_PLOTLY_COLORWAY);
    return isPlotlyColorway ? 'plotly' : 'others';
}
function tryMapFluentDataViz(hexColor, templateColorway, isDarkTheme) {
    if (templateColorway !== 'plotly') {
        return hexColor;
    }
    var index = DEFAULT_PLOTLY_COLORWAY.indexOf(hexColor.toLowerCase());
    if (index !== -1) {
        return getColorFromToken(PLOTLY_FLUENTVIZ_COLORWAY_MAPPING[index], isDarkTheme);
    }
    return hexColor;
}
export var getColor = function (legendLabel, colorMap, isDarkTheme) {
    if (!colorMap.current.has(legendLabel)) {
        var nextColor = void 0;
        if (colorMap.current.size < PLOTLY_FLUENTVIZ_COLORWAY_MAPPING.length) {
            // Get first 10 colors from plotly-fluentviz colorway mapping
            nextColor = getColorFromToken(PLOTLY_FLUENTVIZ_COLORWAY_MAPPING[colorMap.current.size], isDarkTheme);
        }
        else {
            nextColor = getNextColor(colorMap.current.size, 0, isDarkTheme);
        }
        colorMap.current.set(legendLabel, nextColor);
        return nextColor;
    }
    return colorMap.current.get(legendLabel);
};
export var getSchemaColors = function (colorway, colors, colorMap, isDarkTheme) {
    var hexColors = [];
    if (!colors) {
        return undefined;
    }
    var templateColorway = getPlotlyColorway(colorway);
    if (isArrayOrTypedArray(colors)) {
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        colors.forEach(function (element, index) {
            var colorString = element === null || element === void 0 ? void 0 : element.toString().trim();
            var nextFluentColor = getColor("Label_".concat(index), colorMap, isDarkTheme);
            if (colorString) {
                var parsedColor = d3Color(colorString);
                hexColors.push(parsedColor ? tryMapFluentDataViz(parsedColor.formatHex(), templateColorway, isDarkTheme) : nextFluentColor);
            }
            else {
                hexColors.push(nextFluentColor);
            }
        });
    }
    else if (typeof colors === 'string') {
        var parsedColor = d3Color(colors);
        return parsedColor
            ? tryMapFluentDataViz(parsedColor.formatHex(), templateColorway, isDarkTheme)
            : getColor('Label_0', colorMap, isDarkTheme);
    }
    return hexColors;
};
export var extractColor = function (colorway, colorwayType, colors, colorMap, isDarkTheme) {
    return colorwayType === 'default' && colors ? getSchemaColors(colorway, colors, colorMap, isDarkTheme) : undefined;
};
export var resolveColor = function (extractedColors, index, legend, colorMap, isDarkTheme) {
    var color = '';
    if (extractedColors && isArrayOrTypedArray(extractedColors) && extractedColors[index]) {
        color = extractedColors[index % extractedColors.length];
    }
    else if (typeof extractedColors === 'string') {
        color = extractedColors;
    }
    else {
        color = getColor(legend, colorMap, isDarkTheme);
    }
    return color;
};
//# sourceMappingURL=PlotlyColorAdapter.js.map