"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getOpacity = exports.resolveColor = exports.extractColor = exports.getSchemaColors = exports.getColor = void 0;
var d3_color_1 = require("d3-color");
var chart_utilities_1 = require("@fluentui/chart-utilities");
var utilities_1 = require("../../utilities/utilities");
var colors_1 = require("../../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 = [
    colors_1.DataVizPalette.color1, //1
    colors_1.DataVizPalette.warning, //2
    colors_1.DataVizPalette.color8, //3
    colors_1.DataVizPalette.color4, //4
    colors_1.DataVizPalette.color7, //5
    colors_1.DataVizPalette.color6, //6
    colors_1.DataVizPalette.color2, //7
    colors_1.DataVizPalette.color5, //8
    colors_1.DataVizPalette.color9, //9
    colors_1.DataVizPalette.color10, //10
];
function getPlotlyColorway(colorway) {
    var isPlotlyColorway = (0, chart_utilities_1.isArrayOrTypedArray)(colorway) &&
        (0, utilities_1.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 (0, colors_1.getColorFromToken)(PLOTLY_FLUENTVIZ_COLORWAY_MAPPING[index], isDarkTheme);
    }
    return hexColor;
}
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 = (0, colors_1.getColorFromToken)(PLOTLY_FLUENTVIZ_COLORWAY_MAPPING[colorMap.current.size], isDarkTheme);
        }
        else {
            nextColor = (0, colors_1.getNextColor)(colorMap.current.size, 0, isDarkTheme);
        }
        colorMap.current.set(legendLabel, nextColor);
        return nextColor;
    }
    return colorMap.current.get(legendLabel);
};
exports.getColor = getColor;
var getSchemaColors = function (colorway, colors, colorMap, isDarkTheme) {
    var hexColors = [];
    if (!colors) {
        return undefined;
    }
    var templateColorway = getPlotlyColorway(colorway);
    if ((0, chart_utilities_1.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 = (0, exports.getColor)("Label_".concat(index), colorMap, isDarkTheme);
            if (colorString) {
                var parsedColor = (0, d3_color_1.color)(colorString);
                hexColors.push(parsedColor ? tryMapFluentDataViz(parsedColor.formatHex(), templateColorway, isDarkTheme) : nextFluentColor);
            }
            else {
                hexColors.push(nextFluentColor);
            }
        });
    }
    else if (typeof colors === 'string') {
        var parsedColor = (0, d3_color_1.color)(colors);
        return parsedColor
            ? tryMapFluentDataViz(parsedColor.formatHex(), templateColorway, isDarkTheme)
            : (0, exports.getColor)('Label_0', colorMap, isDarkTheme);
    }
    return hexColors;
};
exports.getSchemaColors = getSchemaColors;
var extractColor = function (colorway, colorwayType, colors, colorMap, isDarkTheme) {
    return colorwayType === 'default' && colors ? (0, exports.getSchemaColors)(colorway, colors, colorMap, isDarkTheme) : undefined;
};
exports.extractColor = extractColor;
var resolveColor = function (extractedColors, index, legend, colorMap, isDarkTheme) {
    var color = '';
    if (extractedColors && (0, chart_utilities_1.isArrayOrTypedArray)(extractedColors) && extractedColors.length > 0) {
        color = extractedColors[index % extractedColors.length];
    }
    else if (typeof extractedColors === 'string') {
        color = extractedColors;
    }
    else {
        color = (0, exports.getColor)(legend, colorMap, isDarkTheme);
    }
    return color;
};
exports.resolveColor = resolveColor;
var getOpacity = function (series, index) {
    var _a, _b, _c, _d, _e, _f;
    return ((_a = series.marker) === null || _a === void 0 ? void 0 : _a.opacity)
        ? (0, chart_utilities_1.isArrayOrTypedArray)((_b = series.marker) === null || _b === void 0 ? void 0 : _b.opacity)
            ? ((_c = series.marker) === null || _c === void 0 ? void 0 : _c.opacity)[index % ((_d = series.marker) === null || _d === void 0 ? void 0 : _d.opacity).length]
            : (_e = series.marker) === null || _e === void 0 ? void 0 : _e.opacity
        : (_f = series.opacity) !== null && _f !== void 0 ? _f : 1;
};
exports.getOpacity = getOpacity;
//# sourceMappingURL=PlotlyColorAdapter.js.map