"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.previewConvertCustomDimensionToDbt = exports.convertCustomBinDimensionToDbt = exports.convertCustomSqlDimensionToDbt = void 0;
const tslib_1 = require("tslib");
const errors_1 = require("../types/errors");
const field_1 = require("../types/field");
const assertUnreachable_1 = tslib_1.__importDefault(require("./assertUnreachable"));
const customDimensions_1 = require("./customDimensions");
const warehouse_1 = require("./warehouse");
const convertCustomSqlDimensionToDbt = (field) => ({
    label: (0, field_1.friendlyName)(field.name),
    type: field.dimensionType,
    sql: field.sql,
});
exports.convertCustomSqlDimensionToDbt = convertCustomSqlDimensionToDbt;
const convertCustomBinDimensionToDbt = ({ customDimension, baseDimensionSql, warehouseSqlBuilder, }) => {
    switch (customDimension.binType) {
        case field_1.BinType.CUSTOM_RANGE:
            return {
                label: (0, field_1.friendlyName)(customDimension.name),
                type: field_1.DimensionType.STRING,
                sql: (0, customDimensions_1.getCustomRangeSelectSql)({
                    binRanges: customDimension.customRange,
                    baseDimensionSql,
                    warehouseSqlBuilder,
                }),
            };
        case field_1.BinType.FIXED_WIDTH:
            return {
                label: (0, field_1.friendlyName)(customDimension.name),
                type: field_1.DimensionType.STRING,
                sql: (0, customDimensions_1.getFixedWidthBinSelectSql)({
                    binWidth: customDimension.binWidth,
                    baseDimensionSql,
                    warehouseSqlBuilder,
                }),
            };
        case field_1.BinType.FIXED_NUMBER:
            throw new errors_1.NotImplementedError('Bin with fixed number of bins can not be converted to dbt as it requires a CTE');
        case field_1.BinType.CUSTOM_GROUP:
            return {
                label: (0, field_1.friendlyName)(customDimension.name),
                type: field_1.DimensionType.STRING,
                sql: (0, customDimensions_1.getCustomGroupSelectSql)({
                    binGroups: customDimension.customGroups,
                    baseDimensionSql,
                    warehouseSqlBuilder,
                }),
            };
        default:
            throw new Error(`Unknown bin type ${(0, assertUnreachable_1.default)(customDimension, 'Unknown bin type')}`);
    }
};
exports.convertCustomBinDimensionToDbt = convertCustomBinDimensionToDbt;
// Mock Bigquery warehouse client for preview
const warehouseClientMock = {
    credentials: undefined,
    getCatalog() {
        throw new errors_1.NotImplementedError('getCatalog not implemented');
    },
    getAdapterType() {
        throw new errors_1.NotImplementedError('getCatalog not implemented');
    },
    supportsCteMaterialization: () => true,
    getAllTables() {
        throw new errors_1.NotImplementedError('getCatalog not implemented');
    },
    getFields() {
        throw new errors_1.NotImplementedError('getCatalog not implemented');
    },
    getMetricSql() {
        throw new errors_1.NotImplementedError('getMetricSql not implemented');
    },
    getStartOfWeek() {
        throw new errors_1.NotImplementedError('getCatalog not implemented');
    },
    parseError() {
        throw new errors_1.NotImplementedError('getCatalog not implemented');
    },
    parseWarehouseCatalog() {
        throw new errors_1.NotImplementedError('getCatalog not implemented');
    },
    runQuery() {
        throw new errors_1.NotImplementedError('getCatalog not implemented');
    },
    streamQuery() {
        throw new errors_1.NotImplementedError('getCatalog not implemented');
    },
    executeAsyncQuery() {
        throw new errors_1.NotImplementedError('getCatalog not implemented');
    },
    test() {
        throw new errors_1.NotImplementedError('getCatalog not implemented');
    },
    concatString(...args) {
        return `CONCAT(${args.join(', ')})`;
    },
    getStringQuoteChar() {
        return "'";
    },
    getEscapeStringQuoteChar() {
        return '\\';
    },
    getFieldQuoteChar() {
        return '"';
    },
    getFloatingType() {
        return 'FLOAT';
    },
    getNullSafeEqualSql: warehouse_1.defaultNullSafeEqualSql,
    getNullSafeEqualJoinSql: warehouse_1.defaultNullSafeEqualSql,
    escapeString(value) {
        return value;
    },
    castToTimestamp(date) {
        return `CAST('${date.toISOString()}' AS TIMESTAMP)`;
    },
    getIntervalSql(value, unit) {
        return `INTERVAL '${value} ${unit}'`;
    },
    getTimestampDiffSeconds(startTimestampSql, endTimestampSql) {
        return `EXTRACT(EPOCH FROM (${endTimestampSql} - ${startTimestampSql}))`;
    },
    getMedianSql(valueSql) {
        return `PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY ${valueSql})`;
    },
    buildArray(elements) {
        return `ARRAY[${elements.join(', ')}]`;
    },
    buildArrayAgg(expression, orderBy) {
        return orderBy
            ? `ARRAY_AGG(${expression} ORDER BY ${orderBy})`
            : `ARRAY_AGG(${expression})`;
    },
};
const previewConvertCustomDimensionToDbt = (field) => {
    if ((0, field_1.isCustomBinDimension)(field)) {
        // Mock base dimension SQL and warehouse client for preview
        const preview = (0, exports.convertCustomBinDimensionToDbt)({
            customDimension: field,
            baseDimensionSql: '${reference_column}',
            warehouseSqlBuilder: warehouseClientMock,
        });
        return {
            ...preview,
            // Add a comment at the top of the multiline string to indicate that this is a preview
            sql: `/* This is a preview! Replace column references and confirm SQL before using in production. */\n${preview.sql}`,
        };
    }
    return (0, exports.convertCustomSqlDimensionToDbt)(field);
};
exports.previewConvertCustomDimensionToDbt = previewConvertCustomDimensionToDbt;
//# sourceMappingURL=convertCustomDimensionsToYaml.js.map