"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCustomMetricType = exports.convertAdditionalMetric = void 0;
const dbt_1 = require("../types/dbt");
const field_1 = require("../types/field");
const metricQuery_1 = require("../types/metricQuery");
const convertAdditionalMetric = ({ additionalMetric, table, }) => {
    const metric = (0, dbt_1.convertColumnMetric)({
        modelName: table.name,
        dimensionSql: additionalMetric.sql,
        name: additionalMetric.name,
        metric: { ...additionalMetric, filters: undefined },
        tableLabel: table.label,
    });
    const popMetadata = (0, metricQuery_1.isPeriodOverPeriodAdditionalMetric)(additionalMetric)
        ? additionalMetric
        : undefined;
    // A MIN/MAX custom metric carries its base dimension's temporal type,
    // resolved from baseDimensionName (the UI builds `sql` from that dimension).
    const baseDimension = additionalMetric.baseDimensionName
        ? table.dimensions[additionalMetric.baseDimensionName]
        : undefined;
    const baseDimensionMetadata = (0, field_1.getMinMaxBaseDimensionMetadata)(additionalMetric.type, baseDimension && {
        type: baseDimension.type,
        timeInterval: baseDimension.timeInterval,
    });
    return {
        ...metric,
        ...baseDimensionMetadata,
        ...(additionalMetric.filters && {
            filters: additionalMetric.filters,
        }),
        ...(additionalMetric.formatOptions && {
            formatOptions: additionalMetric.formatOptions,
        }),
        ...(popMetadata ?? {}),
    };
};
exports.convertAdditionalMetric = convertAdditionalMetric;
/**
 * Get the custom metric types for a given dimension type
 * @param type
 * @returns
 */
const getCustomMetricType = (type) => {
    switch (type) {
        case field_1.DimensionType.STRING:
        case field_1.DimensionType.TIMESTAMP:
        case field_1.DimensionType.DATE:
            return [
                field_1.MetricType.COUNT_DISTINCT,
                field_1.MetricType.COUNT,
                field_1.MetricType.MIN,
                field_1.MetricType.MAX,
            ];
        case field_1.DimensionType.NUMBER:
            return [
                field_1.MetricType.MIN,
                field_1.MetricType.MAX,
                field_1.MetricType.SUM,
                field_1.MetricType.PERCENTILE,
                field_1.MetricType.MEDIAN,
                field_1.MetricType.AVERAGE,
                field_1.MetricType.COUNT_DISTINCT,
                field_1.MetricType.COUNT,
            ];
        case field_1.DimensionType.BOOLEAN:
            return [field_1.MetricType.COUNT_DISTINCT, field_1.MetricType.COUNT];
        default:
            return [];
    }
};
exports.getCustomMetricType = getCustomMetricType;
//# sourceMappingURL=additionalMetrics.js.map