"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.metricQueryVerticalBarViz = exports.verticalBarMetricVizConfigSchema = 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.verticalBarMetricVizConfigSchema = 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 dimension to be displayed on the x-axis.',
    }),
    yMetrics: zod_1.z
        .array((0, fieldId_1.getFieldIdSchema)({ additionalDescription: null }))
        .min(1)
        .describe('The field ids of the metrics to be displayed on the y-axis. The height of the bars'),
    sorts: zod_1.z
        .array(sortField_1.default)
        .describe('Sort configuration for the query, it can use a combination of metrics and dimensions.'),
    limit: zod_1.z
        .number()
        .max(constants_1.AI_DEFAULT_MAX_QUERY_LIMIT)
        .nullable()
        .describe(`The total number of data points / bars allowed on the chart.`),
    breakdownByDimension: (0, fieldId_1.getFieldIdSchema)({
        additionalDescription: 'The field id of the dimension used to split the metrics into groups along the x-axis. If stacking is false then this will create multiple bars around each x value, if stacking is true then this will create multiple bars for each metric stacked on top of each other',
    }).nullable(),
    stackBars: zod_1.z
        .boolean()
        .nullable()
        .describe('If using breakdownByDimension then this will stack the bars on top of each other instead of side by side.'),
    xAxisType: zod_1.z
        .union([zod_1.z.literal('category'), zod_1.z.literal('time')])
        .describe('The x-axis type can be categorical for string value or time if the dimension is a date or timestamp.'),
    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 metricQueryVerticalBarViz = ({ 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,
        // TODO: add tableCalculations
        tableCalculations,
    };
};
exports.metricQueryVerticalBarViz = metricQueryVerticalBarViz;
//# sourceMappingURL=verticalBarViz.js.map