import * as _ from 'lodash';
/**
 * @internal
 */
var DEFAULT_COLORS = [
    [236, 64, 122],
    [92, 107, 192],
    [38, 198, 218],
    [255, 238, 88],
    [156, 204, 101],
    [0, 102, 204],
    [102, 153, 204],
    [102, 0, 204],
    [204, 102, 204],
    [204, 255, 204],
    [255, 0, 102],
    [255, 99, 132],
    [54, 162, 235],
    [255, 206, 86],
    [151, 187, 205],
    [220, 220, 220],
    [247, 70, 74],
    [70, 191, 189],
    [253, 180, 92],
    [148, 159, 177],
    [77, 83, 96],
    [230, 25, 75],
    [60, 180, 75],
    [255, 225, 25],
    [0, 130, 200],
    [245, 130, 48],
    [145, 30, 180],
    [70, 240, 240],
    [240, 50, 230],
    [210, 245, 60],
    [250, 190, 190],
    [0, 128, 128],
    [230, 190, 255],
    [170, 110, 40],
    [255, 250, 200],
    [128, 0, 0],
    [170, 255, 195],
    [128, 128, 0],
    [255, 215, 180],
    [0, 0, 128],
    [128, 128, 128]
];
/**
 * @internal
 */
export function replaceDefaultColors(colors) {
    _.merge(DEFAULT_COLORS, colors);
}
/**
 * @internal
 */
function generateColors(definedColors, totalNum) {
    var defaultIdx = 0;
    var colors = [];
    if (definedColors.length < totalNum) {
        definedColors = _.concat(definedColors, new Array(totalNum - definedColors.length));
    }
    _.each(definedColors, function (val, i) {
        var col = parseColor(val);
        if (!col) {
            col = DEFAULT_COLORS[defaultIdx++] || getRandomColor();
        }
        colors.push(col);
    });
    return colors;
}
/**
 * @internal
 */
function parseColor(input) {
    if (!input) {
        return undefined;
    }
    var m = input.match(/^#([0-9a-f]{6})$/i);
    if (m) {
        return [parseInt(m[1].substr(0, 2), 16), parseInt(m[1].substr(2, 2), 16), parseInt(m[1].substr(4, 2), 16)];
    }
    else {
        m = input.match(/^#([0-9a-f]{3})$/i);
        if (m) {
            return [
                parseInt(m[1].charAt(0), 16) * 0x11,
                parseInt(m[1].charAt(1), 16) * 0x11,
                parseInt(m[1].charAt(2), 16) * 0x11
            ];
        }
        else {
            m = input.match(/^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i);
            if (m) {
                return [parseInt(m[1], 10), parseInt(m[2], 10), parseInt(m[3], 10)];
            }
            else {
                console.error('Color ' + input + ' could not be parsed.');
                return undefined;
            }
        }
    }
}
/**
 * @internal
 */
export function generateColorsAsStrings(definedColors, totalNum) {
    return generateColors(definedColors, totalNum).map(function (c) { return "rgba(" + c[0] + ", " + c[1] + ", " + c[2] + ", 0.8)"; });
}
/**
 * @internal
 */
function rgba(colour, alpha) {
    return 'rgba(' + colour.concat(alpha).join(',') + ')';
}
/**
 * @internal
 */
function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
/**
 * @internal
 */
function getRandomColor() {
    return [getRandomInt(0, 255), getRandomInt(0, 255), getRandomInt(0, 255)];
}
/**
 * @internal
 */
function formatChartColorsForSeries(colors, type) {
    return colors.map(function (color) { return ({
        backgroundColor: rgba(color, 0.6),
        borderColor: type === 'line' ? rgba(color, 0.6) : '#fff',
        pointBackgroundColor: rgba(color, 0.6),
        pointBorderColor: '#fff',
        pointHoverBackgroundColor: rgba(color, 1),
        pointHoverBorderColor: rgba(color, 0.8)
    }); });
}
/**
 * @internal
 */
export function generateColorsBySeries(definedColors, totalNum, type) {
    return formatChartColorsForSeries(generateColors(definedColors || [], totalNum), type);
}
/**
 * @internal
 */
function formatChartColorsForData(colors, type) {
    return {
        backgroundColor: colors.map(function (color) { return rgba(color, 0.6); }),
        borderColor: colors.map(function (color) { return (type === 'line' ? rgba(color, 0.6) : '#fff'); }),
        pointBackgroundColor: colors.map(function (color) { return rgba(color, 1); }),
        pointBorderColor: colors.map(function (color) { return '#fff'; }),
        pointHoverBackgroundColor: colors.map(function (color) { return rgba(color, 1); }),
        pointHoverBorderColor: colors.map(function (color) { return rgba(color, 0.8); })
    };
}
/**
 * @internal
 */
export function generateColorsByDataPoints(definedColors, totalNum, type) {
    return formatChartColorsForData(generateColors(definedColors || [], totalNum), type);
}
//# sourceMappingURL=color.helpers.js.map