"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createColorScale = 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 d3_scale_1 = require("d3-scale");
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;
var createColorScale = function (layout, series, currentColorScale) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
    if (((_b = (_a = layout === null || layout === void 0 ? void 0 : layout.coloraxis) === null || _a === void 0 ? void 0 : _a.colorscale) === null || _b === void 0 ? void 0 : _b.length) &&
        (0, chart_utilities_1.isArrayOrTypedArray)((_c = series.marker) === null || _c === void 0 ? void 0 : _c.color) &&
        ((_d = series.marker) === null || _d === void 0 ? void 0 : _d.color).length > 0 &&
        typeof ((_f = (_e = series.marker) === null || _e === void 0 ? void 0 : _e.color) === null || _f === void 0 ? void 0 : _f[0]) === 'number') {
        var scale = (_g = layout === null || layout === void 0 ? void 0 : layout.coloraxis) === null || _g === void 0 ? void 0 : _g.colorscale;
        var colorValues = (_h = series.marker) === null || _h === void 0 ? void 0 : _h.color;
        var _o = [
            (_k = (_j = layout === null || layout === void 0 ? void 0 : layout.coloraxis) === null || _j === void 0 ? void 0 : _j.cmin) !== null && _k !== void 0 ? _k : Math.min.apply(Math, colorValues),
            (_m = (_l = layout === null || layout === void 0 ? void 0 : layout.coloraxis) === null || _l === void 0 ? void 0 : _l.cmax) !== null && _m !== void 0 ? _m : Math.max.apply(Math, colorValues),
        ], dMin_1 = _o[0], dMax_1 = _o[1];
        // Normalize colorscale domain to actual data domain
        var scaleDomain = scale.map(function (_a) {
            var pos = _a[0];
            return dMin_1 + pos * (dMax_1 - dMin_1);
        });
        var scaleColors = scale.map(function (item) { return item[1]; });
        return (0, d3_scale_1.scaleLinear)().domain(scaleDomain).range(scaleColors);
    }
    return currentColorScale;
};
exports.createColorScale = createColorScale;
//# sourceMappingURL=PlotlyColorAdapter.js.map