"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getHiddenTableFields = exports.getEChartsChartTypeFromChartKind = exports.getChartKind = exports.getChartType = exports.isSeriesWithMixedChartTypes = exports.getDefaultResolvedColorPalette = exports.getDefaultSeriesColor = exports.ECHARTS_DEFAULT_COLORS = exports.removePivotedSeriesValuesFromChartConfig = exports.getSeriesId = exports.hashFieldReference = exports.getConditionalFormattingsFromChartConfig = exports.getCustomLabelsFromTableConfig = exports.getCustomLabelsFromColumnProperties = exports.isPieChartConfig = exports.isTableChartConfig = exports.isBigNumberConfig = exports.isDataAppVizChart = exports.isCartesianChartConfig = exports.isCompleteEchartsConfig = exports.isCompleteLayout = exports.XAxisSort = exports.XAxisSortType = exports.TooltipSortByOptions = exports.isPivotReferenceWithValues = exports.CartesianSeriesType = exports.FunnelChartLegendPosition = exports.FunnelChartLabelPosition = exports.FunnelChartDataInput = exports.MapTileBackground = exports.MapHexbinAggregation = exports.MapHexbinValueBasis = exports.MapHexbinSizingMode = exports.MapChartType = exports.MapChartLocation = exports.PieChartTooltipLabelMaxLength = exports.PieChartLegendLabelMaxLengthDefault = exports.PieChartLegendPositionDefault = exports.PieChartLegendPositions = exports.PieChartValueLabels = exports.ComparisonDiffTypes = exports.ComparisonFormatTypes = exports.ChartType = exports.ChartKind = void 0;
exports.getXAxisSort = getXAxisSort;
const tslib_1 = require("tslib");
const assertUnreachable_1 = tslib_1.__importDefault(require("../utils/assertUnreachable"));
var ChartKind;
(function (ChartKind) {
    ChartKind["LINE"] = "line";
    ChartKind["HORIZONTAL_BAR"] = "horizontal_bar";
    ChartKind["VERTICAL_BAR"] = "vertical_bar";
    ChartKind["SCATTER"] = "scatter";
    ChartKind["AREA"] = "area";
    ChartKind["MIXED"] = "mixed";
    ChartKind["PIE"] = "pie";
    ChartKind["TABLE"] = "table";
    ChartKind["BIG_NUMBER"] = "big_number";
    ChartKind["FUNNEL"] = "funnel";
    ChartKind["CUSTOM"] = "custom";
    ChartKind["TREEMAP"] = "treemap";
    ChartKind["GAUGE"] = "gauge";
    ChartKind["MAP"] = "map";
    ChartKind["SANKEY"] = "sankey";
    ChartKind["DATA_APP_VIZ"] = "data_app_viz";
})(ChartKind || (exports.ChartKind = ChartKind = {}));
var ChartType;
(function (ChartType) {
    ChartType["CARTESIAN"] = "cartesian";
    ChartType["TABLE"] = "table";
    ChartType["BIG_NUMBER"] = "big_number";
    ChartType["PIE"] = "pie";
    ChartType["FUNNEL"] = "funnel";
    ChartType["TREEMAP"] = "treemap";
    ChartType["GAUGE"] = "gauge";
    ChartType["CUSTOM"] = "custom";
    ChartType["MAP"] = "map";
    ChartType["SANKEY"] = "sankey";
    ChartType["DATA_APP_VIZ"] = "data_app_viz";
})(ChartType || (exports.ChartType = ChartType = {}));
var ComparisonFormatTypes;
(function (ComparisonFormatTypes) {
    ComparisonFormatTypes["RAW"] = "raw";
    ComparisonFormatTypes["PERCENTAGE"] = "percentage";
})(ComparisonFormatTypes || (exports.ComparisonFormatTypes = ComparisonFormatTypes = {}));
var ComparisonDiffTypes;
(function (ComparisonDiffTypes) {
    ComparisonDiffTypes["POSITIVE"] = "positive";
    ComparisonDiffTypes["NEGATIVE"] = "negative";
    ComparisonDiffTypes["NONE"] = "none";
    ComparisonDiffTypes["NAN"] = "NaN";
    ComparisonDiffTypes["UNDEFINED"] = "undefined";
})(ComparisonDiffTypes || (exports.ComparisonDiffTypes = ComparisonDiffTypes = {}));
exports.PieChartValueLabels = {
    hidden: 'Hidden',
    inside: 'Inside',
    outside: 'Outside',
};
exports.PieChartLegendPositions = {
    horizontal: 'Horizontal',
    vertical: 'Vertical',
};
exports.PieChartLegendPositionDefault = Object.keys(exports.PieChartLegendPositions)[0];
exports.PieChartLegendLabelMaxLengthDefault = 30;
exports.PieChartTooltipLabelMaxLength = 40;
var MapChartLocation;
(function (MapChartLocation) {
    MapChartLocation["USA"] = "USA";
    MapChartLocation["WORLD"] = "world";
    MapChartLocation["EUROPE"] = "europe";
    MapChartLocation["CUSTOM"] = "custom";
})(MapChartLocation || (exports.MapChartLocation = MapChartLocation = {}));
var MapChartType;
(function (MapChartType) {
    MapChartType["SCATTER"] = "scatter";
    MapChartType["AREA"] = "area";
    MapChartType["HEATMAP"] = "heatmap";
    MapChartType["HEXBIN"] = "hexbin";
})(MapChartType || (exports.MapChartType = MapChartType = {}));
var MapHexbinSizingMode;
(function (MapHexbinSizingMode) {
    /** Resolution is derived from current map zoom level. */
    MapHexbinSizingMode["DYNAMIC"] = "dynamic";
    /** Resolution is fixed by the user, independent of zoom. */
    MapHexbinSizingMode["FIXED"] = "fixed";
})(MapHexbinSizingMode || (exports.MapHexbinSizingMode = MapHexbinSizingMode = {}));
var MapHexbinValueBasis;
(function (MapHexbinValueBasis) {
    /** Color cells by the count of points they contain. */
    MapHexbinValueBasis["COUNT"] = "count";
    /** Color cells by an aggregation over a chosen value field. */
    MapHexbinValueBasis["FIELD"] = "field";
})(MapHexbinValueBasis || (exports.MapHexbinValueBasis = MapHexbinValueBasis = {}));
var MapHexbinAggregation;
(function (MapHexbinAggregation) {
    MapHexbinAggregation["SUM"] = "sum";
    MapHexbinAggregation["AVG"] = "avg";
    MapHexbinAggregation["MIN"] = "min";
    MapHexbinAggregation["MAX"] = "max";
})(MapHexbinAggregation || (exports.MapHexbinAggregation = MapHexbinAggregation = {}));
var MapTileBackground;
(function (MapTileBackground) {
    MapTileBackground["NONE"] = "none";
    MapTileBackground["OPENSTREETMAP"] = "openstreetmap";
    MapTileBackground["LIGHT"] = "light";
    MapTileBackground["DARK"] = "dark";
    MapTileBackground["SATELLITE"] = "satellite";
    MapTileBackground["VOYAGER"] = "voyager";
})(MapTileBackground || (exports.MapTileBackground = MapTileBackground = {}));
var FunnelChartDataInput;
(function (FunnelChartDataInput) {
    FunnelChartDataInput["ROW"] = "row";
    FunnelChartDataInput["COLUMN"] = "column";
})(FunnelChartDataInput || (exports.FunnelChartDataInput = FunnelChartDataInput = {}));
var FunnelChartLabelPosition;
(function (FunnelChartLabelPosition) {
    FunnelChartLabelPosition["INSIDE"] = "inside";
    FunnelChartLabelPosition["LEFT"] = "left";
    FunnelChartLabelPosition["RIGHT"] = "right";
    FunnelChartLabelPosition["HIDDEN"] = "hidden";
})(FunnelChartLabelPosition || (exports.FunnelChartLabelPosition = FunnelChartLabelPosition = {}));
var FunnelChartLegendPosition;
(function (FunnelChartLegendPosition) {
    FunnelChartLegendPosition["HORIZONTAL"] = "horizontal";
    FunnelChartLegendPosition["VERTICAL"] = "vertical";
})(FunnelChartLegendPosition || (exports.FunnelChartLegendPosition = FunnelChartLegendPosition = {}));
var CartesianSeriesType;
(function (CartesianSeriesType) {
    CartesianSeriesType["LINE"] = "line";
    CartesianSeriesType["BAR"] = "bar";
    CartesianSeriesType["SCATTER"] = "scatter";
    CartesianSeriesType["AREA"] = "area";
})(CartesianSeriesType || (exports.CartesianSeriesType = CartesianSeriesType = {}));
const isPivotReferenceWithValues = (value) => !!value.pivotValues && value.pivotValues.length > 0;
exports.isPivotReferenceWithValues = isPivotReferenceWithValues;
exports.TooltipSortByOptions = {
    DEFAULT: 'default',
    ALPHABETICAL: 'alphabetical',
    VALUE_ASCENDING: 'value_ascending',
    VALUE_DESCENDING: 'value_descending',
};
var XAxisSortType;
(function (XAxisSortType) {
    XAxisSortType["DEFAULT"] = "default";
    XAxisSortType["CATEGORY"] = "category";
    XAxisSortType["BAR_TOTALS"] = "bar_totals";
})(XAxisSortType || (exports.XAxisSortType = XAxisSortType = {}));
var XAxisSort;
(function (XAxisSort) {
    XAxisSort["DEFAULT"] = "default";
    XAxisSort["DEFAULT_REVERSED"] = "default_reversed";
    XAxisSort["ASCENDING"] = "ascending";
    XAxisSort["DESCENDING"] = "descending";
    XAxisSort["BAR_TOTALS_ASCENDING"] = "bar_totals_ascending";
    XAxisSort["BAR_TOTALS_DESCENDING"] = "bar_totals_descending";
})(XAxisSort || (exports.XAxisSort = XAxisSort = {}));
function getXAxisSort(xAxis) {
    if (!xAxis)
        return XAxisSort.DEFAULT;
    switch (xAxis.sortType) {
        case XAxisSortType.CATEGORY:
            return xAxis.inverse ? XAxisSort.DESCENDING : XAxisSort.ASCENDING;
        case XAxisSortType.BAR_TOTALS:
            return xAxis.inverse
                ? XAxisSort.BAR_TOTALS_DESCENDING
                : XAxisSort.BAR_TOTALS_ASCENDING;
        case XAxisSortType.DEFAULT:
        default:
            return xAxis.inverse
                ? XAxisSort.DEFAULT_REVERSED
                : XAxisSort.DEFAULT;
    }
}
const isCompleteLayout = (value) => !!value && !!value.xField && !!value.yField && value.yField.length > 0;
exports.isCompleteLayout = isCompleteLayout;
const isCompleteEchartsConfig = (value) => !!value && !!value.series && value.series.length > 0;
exports.isCompleteEchartsConfig = isCompleteEchartsConfig;
const isCartesianChartConfig = (value) => !!value && 'layout' in value && 'eChartsConfig' in value;
exports.isCartesianChartConfig = isCartesianChartConfig;
const isDataAppVizChart = (value) => !!value && 'dataAppVizUuid' in value && 'fieldMapping' in value;
exports.isDataAppVizChart = isDataAppVizChart;
const isBigNumberConfig = (value) => !!value && !(0, exports.isCartesianChartConfig)(value) && !(0, exports.isDataAppVizChart)(value);
exports.isBigNumberConfig = isBigNumberConfig;
const isTableChartConfig = (value) => !!value && !(0, exports.isCartesianChartConfig)(value) && !(0, exports.isDataAppVizChart)(value);
exports.isTableChartConfig = isTableChartConfig;
const isPieChartConfig = (value) => !!value && 'isDonut' in value;
exports.isPieChartConfig = isPieChartConfig;
const getCustomLabelsFromColumnProperties = (columns) => columns
    ? Object.entries(columns).reduce((acc, [key, value]) => value.name
        ? {
            ...acc,
            [key]: value.name,
        }
        : acc, {})
    : undefined;
exports.getCustomLabelsFromColumnProperties = getCustomLabelsFromColumnProperties;
const getCustomLabelsFromTableConfig = (config) => config && (0, exports.isTableChartConfig)(config)
    ? (0, exports.getCustomLabelsFromColumnProperties)(config.columns)
    : undefined;
exports.getCustomLabelsFromTableConfig = getCustomLabelsFromTableConfig;
const getConditionalFormattingsFromChartConfig = (config) => config && (0, exports.isTableChartConfig)(config)
    ? config.conditionalFormattings
    : undefined;
exports.getConditionalFormattingsFromChartConfig = getConditionalFormattingsFromChartConfig;
const hashFieldReference = (reference) => reference.pivotValues
    ? `${reference.field}.${reference.pivotValues
        .map(({ field, value }) => `${field}.${value}`)
        .join('.')}`
    : reference.field;
exports.hashFieldReference = hashFieldReference;
const getSeriesId = (series) => `${(0, exports.hashFieldReference)(series.encode.xRef)}|${(0, exports.hashFieldReference)(series.encode.yRef)}`;
exports.getSeriesId = getSeriesId;
const stripPivotValuesFromSeries = (series) => {
    const { encode, name, color, ...rest } = series;
    return {
        ...rest,
        encode: {
            xRef: { field: encode.xRef.field },
            yRef: { field: encode.yRef.field },
        },
    };
};
const removePivotedSeriesValuesFromChartConfig = (chartConfig) => {
    if (chartConfig.type !== ChartType.CARTESIAN || !chartConfig.config) {
        return chartConfig;
    }
    const { series } = chartConfig.config.eChartsConfig;
    if (!series || series.length === 0) {
        return chartConfig;
    }
    const seenSeriesIds = new Set();
    const portableSeries = series.reduce((acc, currentSeries) => {
        const hasPivotValues = (0, exports.isPivotReferenceWithValues)(currentSeries.encode.yRef) ||
            (0, exports.isPivotReferenceWithValues)(currentSeries.encode.xRef);
        const portableSerie = hasPivotValues
            ? stripPivotValuesFromSeries(currentSeries)
            : currentSeries;
        const seriesId = (0, exports.getSeriesId)(portableSerie);
        if (seenSeriesIds.has(seriesId)) {
            return acc;
        }
        seenSeriesIds.add(seriesId);
        return [...acc, portableSerie];
    }, []);
    return {
        ...chartConfig,
        config: {
            ...chartConfig.config,
            eChartsConfig: {
                ...chartConfig.config.eChartsConfig,
                series: portableSeries,
            },
        },
    };
};
exports.removePivotedSeriesValuesFromChartConfig = removePivotedSeriesValuesFromChartConfig;
exports.ECHARTS_DEFAULT_COLORS = [
    '#5470c6',
    '#fc8452',
    '#91cc75',
    '#fac858',
    '#ee6666',
    '#73c0de',
    '#3ba272',
    '#9a60b4',
    '#ea7ccc',
];
const getDefaultSeriesColor = (index) => exports.ECHARTS_DEFAULT_COLORS[index % exports.ECHARTS_DEFAULT_COLORS.length];
exports.getDefaultSeriesColor = getDefaultSeriesColor;
const getDefaultResolvedColorPalette = () => ({
    colors: exports.ECHARTS_DEFAULT_COLORS,
    darkColors: null,
    paletteUuid: null,
    paletteName: null,
    source: { type: 'default' },
});
exports.getDefaultResolvedColorPalette = getDefaultResolvedColorPalette;
const isSeriesWithMixedChartTypes = (series) => new Set(series?.map(({ type, areaStyle }) => `${type}${type === CartesianSeriesType.LINE ? areaStyle : undefined}`)).size >= 2;
exports.isSeriesWithMixedChartTypes = isSeriesWithMixedChartTypes;
const getChartType = (chartKind) => {
    if (chartKind === undefined)
        return ChartType.CARTESIAN;
    switch (chartKind) {
        case ChartKind.PIE:
            return ChartType.PIE;
        case ChartKind.FUNNEL:
            return ChartType.FUNNEL;
        case ChartKind.BIG_NUMBER:
            return ChartType.BIG_NUMBER;
        case ChartKind.TABLE:
            return ChartType.TABLE;
        case ChartKind.TREEMAP:
            return ChartType.TREEMAP;
        case ChartKind.GAUGE:
            return ChartType.GAUGE;
        case ChartKind.SANKEY:
            return ChartType.SANKEY;
        case ChartKind.DATA_APP_VIZ:
            return ChartType.DATA_APP_VIZ;
        default:
            return ChartType.CARTESIAN;
    }
};
exports.getChartType = getChartType;
const getChartKind = (chartType, value) => {
    switch (chartType) {
        case ChartType.PIE:
            return ChartKind.PIE;
        case ChartType.FUNNEL:
            return ChartKind.FUNNEL;
        case ChartType.BIG_NUMBER:
            return ChartKind.BIG_NUMBER;
        case ChartType.TABLE:
            return ChartKind.TABLE;
        case ChartType.CUSTOM:
            return ChartKind.CUSTOM;
        case ChartType.CARTESIAN:
            if ((0, exports.isCartesianChartConfig)(value)) {
                const { series } = value.eChartsConfig;
                if ((0, exports.isSeriesWithMixedChartTypes)(series)) {
                    return ChartKind.MIXED;
                }
                const type = series?.[0]?.type;
                if (!type)
                    return undefined;
                switch (type) {
                    case CartesianSeriesType.AREA:
                        return ChartKind.AREA;
                    case CartesianSeriesType.BAR:
                        return value.layout.flipAxes
                            ? ChartKind.HORIZONTAL_BAR
                            : ChartKind.VERTICAL_BAR;
                    case CartesianSeriesType.LINE:
                        return series?.[0]?.areaStyle
                            ? ChartKind.AREA
                            : ChartKind.LINE;
                    case CartesianSeriesType.SCATTER:
                        return ChartKind.SCATTER;
                    default:
                        return (0, assertUnreachable_1.default)(type, `Unknown cartesian series type: ${type}`);
                }
            }
            return undefined;
        case ChartType.TREEMAP:
            return ChartKind.TREEMAP;
        case ChartType.GAUGE:
            return ChartKind.GAUGE;
        case ChartType.MAP:
            return ChartKind.MAP;
        case ChartType.SANKEY:
            return ChartKind.SANKEY;
        case ChartType.DATA_APP_VIZ:
            return ChartKind.DATA_APP_VIZ;
        default:
            return (0, assertUnreachable_1.default)(chartType, `Unknown chart type: ${chartType}`);
    }
};
exports.getChartKind = getChartKind;
const getEChartsChartTypeFromChartKind = (chartKind) => {
    switch (chartKind) {
        case ChartKind.VERTICAL_BAR:
            return CartesianSeriesType.BAR;
        case ChartKind.LINE:
            return CartesianSeriesType.LINE;
        case ChartKind.AREA:
            return CartesianSeriesType.AREA;
        case ChartKind.SCATTER:
            return CartesianSeriesType.SCATTER;
        default:
            return CartesianSeriesType.BAR;
    }
};
exports.getEChartsChartTypeFromChartKind = getEChartsChartTypeFromChartKind;
const getHiddenTableFields = (config) => {
    // get hidden fields from chart config
    if (config.type === 'table' && config.config?.columns) {
        return Object.entries(config.config?.columns).reduce((acc, [col, props]) => {
            if (props.visible === false)
                return [...acc, col];
            return acc;
        }, []);
    }
    return [];
};
exports.getHiddenTableFields = getHiddenTableFields;
//# sourceMappingURL=savedCharts.js.map