"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseVizConfig = void 0;
const constants_1 = require("./constants");
const schemas_1 = require("./schemas");
const types_1 = require("./types");
const validators_1 = require("./validators");
const parseVizConfig = (vizConfigUnknown, maxLimit) => {
    if (!vizConfigUnknown) {
        return null;
    }
    const toolVerticalBarArgsParsed = schemas_1.toolVerticalBarArgsSchemaTransformed.safeParse(vizConfigUnknown);
    if (toolVerticalBarArgsParsed.success) {
        const vizTool = toolVerticalBarArgsParsed.data;
        const metricQuery = (0, schemas_1.metricQueryVerticalBarViz)({
            vizConfig: vizTool.vizConfig,
            filters: vizTool.filters,
            maxLimit: maxLimit ?? constants_1.AI_DEFAULT_MAX_QUERY_LIMIT,
            customMetrics: vizTool.customMetrics ?? null,
            tableCalculations: (0, schemas_1.convertAiTableCalcsSchemaToTableCalcs)(vizTool.tableCalculations),
        });
        return {
            type: types_1.AiResultType.VERTICAL_BAR_RESULT,
            vizTool,
            metricQuery,
        };
    }
    const toolTimeSeriesArgsParsed = schemas_1.toolTimeSeriesArgsSchemaTransformed.safeParse(vizConfigUnknown);
    if (toolTimeSeriesArgsParsed.success) {
        const vizTool = toolTimeSeriesArgsParsed.data;
        const metricQuery = (0, schemas_1.metricQueryTimeSeriesViz)({
            vizConfig: vizTool.vizConfig,
            filters: vizTool.filters,
            maxLimit: maxLimit ?? constants_1.AI_DEFAULT_MAX_QUERY_LIMIT,
            customMetrics: vizTool.customMetrics ?? null,
            tableCalculations: (0, schemas_1.convertAiTableCalcsSchemaToTableCalcs)(vizTool.tableCalculations),
        });
        return {
            type: types_1.AiResultType.TIME_SERIES_RESULT,
            vizTool,
            metricQuery,
        };
    }
    const toolTableVizArgsParsed = schemas_1.toolTableVizArgsSchemaTransformed.safeParse(vizConfigUnknown);
    if (toolTableVizArgsParsed.success) {
        const vizTool = toolTableVizArgsParsed.data;
        const metricQuery = (0, schemas_1.metricQueryTableViz)({
            vizConfig: vizTool.vizConfig,
            filters: vizTool.filters,
            maxLimit: maxLimit ?? constants_1.AI_DEFAULT_MAX_QUERY_LIMIT,
            customMetrics: vizTool.customMetrics ?? null,
            tableCalculations: (0, schemas_1.convertAiTableCalcsSchemaToTableCalcs)(vizTool.tableCalculations),
        });
        return {
            type: types_1.AiResultType.TABLE_RESULT,
            vizTool,
            metricQuery,
        };
    }
    // Parse runQuery tool
    const toolRunQueryArgsParsed = schemas_1.toolRunQueryArgsSchemaTransformed.safeParse(vizConfigUnknown);
    if (toolRunQueryArgsParsed.success) {
        const vizTool = toolRunQueryArgsParsed.data;
        const metricQuery = {
            exploreName: vizTool.queryConfig.exploreName,
            dimensions: vizTool.queryConfig.dimensions,
            metrics: vizTool.queryConfig.metrics,
            sorts: vizTool.queryConfig.sorts.map((sort) => ({
                ...sort,
                nullsFirst: sort.nullsFirst ?? undefined,
            })),
            limit: (0, validators_1.getValidAiQueryLimit)(vizTool.queryConfig.limit, maxLimit ?? constants_1.AI_DEFAULT_MAX_QUERY_LIMIT),
            filters: vizTool.filters,
            additionalMetrics: vizTool.customMetrics ?? [],
            tableCalculations: (0, schemas_1.convertAiTableCalcsSchemaToTableCalcs)(vizTool.tableCalculations),
        };
        return {
            type: types_1.AiResultType.QUERY_RESULT,
            vizTool,
            metricQuery,
        };
    }
    return null;
};
exports.parseVizConfig = parseVizConfig;
//# sourceMappingURL=utils.js.map