"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasModifiedSqlAuthoredFields = exports.getModifiedSqlAuthoredFields = exports.hasSqlAuthoredFields = void 0;
const field_1 = require("../types/field");
const getSqlCustomDimensions = (metricQuery) => (metricQuery?.customDimensions ?? []).filter(field_1.isCustomSqlDimension);
const getSqlTableCalculations = (metricQuery) => (metricQuery?.tableCalculations ?? []).filter(field_1.isSqlTableCalculation);
const hasSqlAuthoredFields = (metricQuery) => getSqlCustomDimensions(metricQuery).length > 0 ||
    getSqlTableCalculations(metricQuery).length > 0;
exports.hasSqlAuthoredFields = hasSqlAuthoredFields;
const getModifiedSqlAuthoredFields = (incoming, saved) => {
    const incomingDims = getSqlCustomDimensions(incoming);
    const savedDimsById = new Map(getSqlCustomDimensions(saved).map((dim) => [dim.id, dim]));
    const modifiedCustomDimensions = incomingDims.filter((dim) => {
        const savedDim = savedDimsById.get(dim.id);
        return !savedDim || savedDim.sql !== dim.sql;
    });
    const incomingCalcs = getSqlTableCalculations(incoming);
    const savedCalcsByName = new Map(getSqlTableCalculations(saved).map((calc) => [calc.name, calc]));
    const modifiedTableCalculations = incomingCalcs.filter((calc) => {
        const savedCalc = savedCalcsByName.get(calc.name);
        return !savedCalc || savedCalc.sql !== calc.sql;
    });
    return {
        customDimensions: modifiedCustomDimensions,
        tableCalculations: modifiedTableCalculations,
    };
};
exports.getModifiedSqlAuthoredFields = getModifiedSqlAuthoredFields;
const hasModifiedSqlAuthoredFields = (incoming, saved) => {
    const modified = (0, exports.getModifiedSqlAuthoredFields)(incoming, saved);
    return (modified.customDimensions.length > 0 ||
        modified.tableCalculations.length > 0);
};
exports.hasModifiedSqlAuthoredFields = hasModifiedSqlAuthoredFields;
//# sourceMappingURL=sqlAuthoredFields.js.map