import { isCustomSqlDimension, isSqlTableCalculation, } from '../types/field';
const getSqlCustomDimensions = (metricQuery) => (metricQuery?.customDimensions ?? []).filter(isCustomSqlDimension);
const getSqlTableCalculations = (metricQuery) => (metricQuery?.tableCalculations ?? []).filter(isSqlTableCalculation);
export const hasSqlAuthoredFields = (metricQuery) => getSqlCustomDimensions(metricQuery).length > 0 ||
    getSqlTableCalculations(metricQuery).length > 0;
export 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,
    };
};
export const hasModifiedSqlAuthoredFields = (incoming, saved) => {
    const modified = getModifiedSqlAuthoredFields(incoming, saved);
    return (modified.customDimensions.length > 0 ||
        modified.tableCalculations.length > 0);
};
//# sourceMappingURL=sqlAuthoredFields.js.map