"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.metricQueryTimeSeriesViz = exports.timeSeriesMetricVizConfigSchema = void 0;
const tslib_1 = require("tslib");
const zod_1 = require("zod");
const constants_1 = require("../../constants");
const validators_1 = require("../../validators");
const customMetrics_1 = require("../customMetrics");
const fieldId_1 = require("../fieldId");
const sortField_1 = tslib_1.__importDefault(require("../sortField"));
exports.timeSeriesMetricVizConfigSchema = zod_1.z.object({
    exploreName: zod_1.z
        .string()
        .describe('The name of the explore containing the metrics and dimensions used for the chart.'),
    xDimension: (0, fieldId_1.getFieldIdSchema)({
        additionalDescription: 'The field id of the time dimension to be displayed on the x-axis.',
    }),
    yMetrics: zod_1.z
        .array((0, fieldId_1.getFieldIdSchema)({ additionalDescription: null }))
        .min(1)
        .describe('At least one metric is required. The field ids of the metrics to be displayed on the y-axis. If there are multiple metrics there will be one line per metric'),
    sorts: zod_1.z
        .array(sortField_1.default)
        .describe('Sort configuration for the query, it can use a combination of metrics and dimensions.'),
    breakdownByDimension: (0, fieldId_1.getFieldIdSchema)({
        additionalDescription: 'The field id of the dimension used to split the metrics into series for each dimension value. For example if you wanted to split a metric into multiple series based on City you would use the City dimension field id here. If this is not provided then the metric will be displayed as a single series.',
    }).nullable(),
    lineType: zod_1.z
        .union([zod_1.z.literal('line'), zod_1.z.literal('area')])
        .describe('default line. The type of line to display. If area then the area under the line will be filled in.'),
    limit: zod_1.z
        .number()
        .max(constants_1.AI_DEFAULT_MAX_QUERY_LIMIT)
        .nullable()
        .describe(`The total number of data points allowed on the chart.`),
    xAxisLabel: zod_1.z
        .string()
        .nullable()
        .describe('A helpful label to explain the x-axis'),
    yAxisLabel: zod_1.z
        .string()
        .nullable()
        .describe('A helpful label to explain the y-axis'),
});
const metricQueryTimeSeriesViz = ({ vizConfig, filters, maxLimit, customMetrics, tableCalculations, }) => {
    const metrics = vizConfig.yMetrics;
    const dimensions = [
        vizConfig.xDimension,
        ...(vizConfig.breakdownByDimension
            ? [vizConfig.breakdownByDimension]
            : []),
    ];
    const { limit, sorts } = vizConfig;
    return {
        metrics,
        dimensions,
        limit: (0, validators_1.getValidAiQueryLimit)(limit, maxLimit),
        sorts: sorts.map((sort) => ({
            ...sort,
            nullsFirst: sort.nullsFirst ?? undefined,
        })),
        exploreName: vizConfig.exploreName,
        filters,
        additionalMetrics: (0, customMetrics_1.filterAggregationCustomMetrics)(customMetrics),
        customMetrics: customMetrics ?? null,
        tableCalculations,
    };
};
exports.metricQueryTimeSeriesViz = metricQueryTimeSeriesViz;
//# sourceMappingURL=timeSeriesViz.js.map