"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.overrideSeriesInUpdate = exports.configureSeries = exports.resolveShowInGraph = void 0;
const defaults = __importStar(require("../defaults"));
const common_1 = require("./common");
const colors_1 = require("./colors");
const markers_1 = require("../plugins/markers");
const paths_1 = require("./paths");
function getCommonProperty(series, yagr, key, defaultValue) {
    if (series[key] !== undefined) {
        return series[key];
    }
    const seriesOptions = yagr.config.chart.series;
    if (seriesOptions && key in seriesOptions) {
        return seriesOptions[key];
    }
    return defaultValue;
}
function resolveShowInGraph(rawSeries) {
    var _a;
    if (rawSeries.showInGraph !== undefined) {
        return rawSeries.showInGraph;
    }
    return (_a = rawSeries.show) !== null && _a !== void 0 ? _a : true;
}
exports.resolveShowInGraph = resolveShowInGraph;
// eslint-disable-next-line complexity
function configureSeries(yagr, rawSeries, idx) {
    var _a, _b;
    const type = getCommonProperty(rawSeries, yagr, 'type', 'line');
    const serie = {
        ...rawSeries,
        type,
        show: (_a = rawSeries.show) !== null && _a !== void 0 ? _a : true,
        showInGraph: resolveShowInGraph(rawSeries),
        name: rawSeries.name || `${yagr.utils.i18n('series')} ${idx + 1}`,
        color: rawSeries.color
            ? yagr.utils.colors.parse(rawSeries.color)
            : yagr.utils.theme.DEFAULT_LINE_COLOR,
        id: (rawSeries.id === undefined ? rawSeries.name : String(rawSeries.id)) || (0, common_1.genId)(),
        $c: rawSeries.data,
        scale: rawSeries.scale || defaults.DEFAULT_Y_SCALE,
        count: 0,
        sum: 0,
        avg: 0,
        getFocusedColor: colors_1.getFocusedColor,
    };
    serie.points = serie.points || {};
    const colorFn = (0, colors_1.getSerieFocusColors)(yagr, 'color');
    if (serie.type === 'area') {
        serie.lineColor = yagr.utils.colors.parse(getCommonProperty(rawSeries, yagr, 'lineColor', defaults.SERIE_AREA_BORDER_COLOR));
        serie.lineWidth = getCommonProperty(rawSeries, yagr, 'lineWidth', defaults.SERIE_AREA_BORDER_WIDTH);
        serie.fill = colorFn;
        serie.stroke = (0, colors_1.getSerieFocusColors)(yagr, 'lineColor');
        serie.width = serie.lineWidth;
        serie.points.show = markers_1.drawMarkersIfRequired;
        serie.interpolation = getCommonProperty(rawSeries, yagr, 'interpolation', 'linear');
        serie.spanGaps = getCommonProperty(rawSeries, yagr, 'spanGaps', false);
    }
    if (serie.type === 'line') {
        serie.width = getCommonProperty(rawSeries, yagr, 'width', defaults.SERIE_LINE_WIDTH);
        serie.width = serie.width || defaults.SERIE_LINE_WIDTH;
        serie.stroke = colorFn;
        serie.points.show = markers_1.drawMarkersIfRequired;
        serie.interpolation = getCommonProperty(rawSeries, yagr, 'interpolation', 'linear');
        serie.spanGaps = getCommonProperty(rawSeries, yagr, 'spanGaps', false);
    }
    if (serie.type === 'column') {
        serie.stroke = colorFn;
        serie.fill = colorFn;
        serie.points.show = false;
        serie.width = (_b = serie.width) !== null && _b !== void 0 ? _b : 0;
        serie.renderOptions = getCommonProperty(serie, yagr, 'renderOptions');
    }
    if (serie.type === 'dots') {
        serie.stroke = () => serie.color;
        serie.fill = colorFn;
        serie.width = 2;
        serie.pointsSize = getCommonProperty(serie, yagr, 'pointsSize', defaults.DEFAULT_POINT_SIZE);
    }
    serie.paths = paths_1.pathsRenderer;
    return serie;
}
exports.configureSeries = configureSeries;
const overrideSeriesInUpdate = (dest, source) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
    dest.$c = (_a = source.$c) !== null && _a !== void 0 ? _a : dest.$c;
    dest.show = (_b = source.show) !== null && _b !== void 0 ? _b : dest.show;
    dest.data = (_c = source.data) !== null && _c !== void 0 ? _c : dest.data;
    dest.width = (_d = source.width) !== null && _d !== void 0 ? _d : dest.width;
    dest.pointsSize = (_e = source.pointsSize) !== null && _e !== void 0 ? _e : dest.pointsSize;
    dest.color = (_f = source.color) !== null && _f !== void 0 ? _f : dest.color;
    dest.lineColor = (_g = source.lineColor) !== null && _g !== void 0 ? _g : dest.lineColor;
    dest.lineWidth = (_h = source.lineWidth) !== null && _h !== void 0 ? _h : dest.lineWidth;
    dest.stroke = (_j = source.stroke) !== null && _j !== void 0 ? _j : dest.stroke;
    dest.getFocusedColor = (_k = source.getFocusedColor) !== null && _k !== void 0 ? _k : dest.getFocusedColor;
    dest.formatter = (_l = source.formatter) !== null && _l !== void 0 ? _l : dest.formatter;
    dest.paths = (_m = source.paths) !== null && _m !== void 0 ? _m : dest.paths;
};
exports.overrideSeriesInUpdate = overrideSeriesInUpdate;
