"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatSql = void 0;
const tslib_1 = require("tslib");
const sql_formatter_1 = require("sql-formatter");
const naming_1 = require("../ee/preAggregates/naming");
const projects_1 = require("../types/projects");
const assertUnreachable_1 = tslib_1.__importDefault(require("./assertUnreachable"));
const MATERIALIZED_TABLE_SAFE_TOKEN = '__duckdb_materialized_table';
const getSqlFormatterDialect = (warehouseType) => {
    switch (warehouseType) {
        case projects_1.WarehouseTypes.BIGQUERY:
            return 'bigquery';
        case projects_1.WarehouseTypes.SNOWFLAKE:
            return 'snowflake';
        case projects_1.WarehouseTypes.ATHENA:
        case projects_1.WarehouseTypes.TRINO:
            return 'trino';
        case projects_1.WarehouseTypes.DATABRICKS:
            return 'spark';
        case projects_1.WarehouseTypes.CLICKHOUSE:
            return 'clickhouse';
        case projects_1.WarehouseTypes.POSTGRES:
            return 'postgresql';
        case projects_1.WarehouseTypes.REDSHIFT:
            return 'redshift';
        case projects_1.WarehouseTypes.DUCKDB:
            return 'duckdb';
        case undefined:
            return 'sql';
        default:
            return (0, assertUnreachable_1.default)(warehouseType, 'Unknown warehouse type');
    }
};
/**
 * Formats SQL using the appropriate dialect for the given warehouse type.
 * Returns the original SQL on formatting failure.
 */
const formatSql = (sql, warehouseType) => {
    try {
        const sanitized = sql.replaceAll(naming_1.PRE_AGGREGATE_MATERIALIZED_TABLE_PLACEHOLDER, MATERIALIZED_TABLE_SAFE_TOKEN);
        const formatted = (0, sql_formatter_1.format)(sanitized, {
            language: getSqlFormatterDialect(warehouseType),
        });
        return formatted.replaceAll(MATERIALIZED_TABLE_SAFE_TOKEN, naming_1.PRE_AGGREGATE_MATERIALIZED_TABLE_PLACEHOLDER);
    }
    catch (e) {
        return sql;
    }
};
exports.formatSql = formatSql;
//# sourceMappingURL=sqlDialect.js.map