'use strict';

var z = require('zod');
var validate$7 = require('../../_charts/chart/bar/validate.js');
var validate$4 = require('../../_charts/chart/doughnut/validate.js');
var validate$2 = require('../../_charts/chart/line/validate.js');
var validate$5 = require('../../_charts/chart/line-timeseries/validate.js');
var validate$6 = require('../../_charts/chart/bar-timeseries/validate.js');
var validate$3 = require('../../_charts/chart/heatmap/validate.js');
var validate$8 = require('../dashboard-list/validate.js');
var validate$1 = require('../scorecard/validate.js');
var validate = require('../scorecard-group/validate.js');
var AggregatedValidationError = require('../../../utils/ErrorHandler/AggregatedValidationError.js');
var types = require('./types.js');
var featureFlagsHelper = require('../../../utils/featureFlagsHelper.js');

const schemaMap = {
    list: validate$8.ListSchema,
    bar: validate$7.BarSchema,
    'bar-timeseries': validate$6.BarTimeseriesSchema,
    'line-timeseries': validate$5.LineTimeseriesSchema,
    doughnut: validate$4.DounutMeasureSchema,
    'matrix-timeseries': validate$3,
    line: validate$2.LineSchema,
    scorecard: validate$1.ScorecardSchema,
    'scorecard-group': validate.ScorecardGroupSchema,
};
async function validateDashboardVisualisations(dashboardDefinition) {
    const visualisationDefs = dashboardDefinition.sections.flatMap((section) => {
        return section.visualisations;
    });
    const results = await Promise.all(visualisationDefs.map(async (item, index) => {
        const { type, id } = item;
        const schema = schemaMap[type];
        if (!schema) {
            const error = new z.ZodError([{ code: 'custom', path: ['type'], message: `Unknown type: ${String(item.type)}` }]);
            return {
                index,
                success: false,
                type,
                id,
                error,
            };
        }
        const parsed = await schema.safeParseAsync(item);
        if (parsed.success) {
            return { index, success: true, data: parsed.data };
        }
        return {
            index,
            success: false,
            type,
            id,
            error: parsed.error,
        };
    }));
    const failures = results.filter((r) => r.success === false);
    if (failures.length > 0) {
        const indexedIssues = failures.flatMap((f) => f.error.issues.map((issue) => ({
            ...issue,
            path: [f.index, ...issue.path],
        })));
        const aggregatedZod = new z.ZodError(indexedIssues);
        const details = failures.map((f) => ({
            index: f.index,
            type: f.type,
            id: f.id,
            issues: f.error.issues,
        }));
        throw new AggregatedValidationError.AggregatedValidationError('Error: Schema validation: Dashboard Visualisation validation failed:', aggregatedZod, details);
    }
    return results.filter((r) => r.success).map((r) => r.data);
}
const getFeatureFlagVisTypeMap = (dashboardFeatureFlags) => {
    return {
        [types.DashboardVisualisationType.LIST]: true,
        [types.DashboardVisualisationType.BAR]: dashboardFeatureFlags[featureFlagsHelper.FEATURE_FLAG_KEYS.BAR_CHARTS],
        [types.DashboardVisualisationType.LINE]: dashboardFeatureFlags[featureFlagsHelper.FEATURE_FLAG_KEYS.LINE_CHARTS],
        [types.DashboardVisualisationType.DONUT]: dashboardFeatureFlags[featureFlagsHelper.FEATURE_FLAG_KEYS.DONUT_CHARTS],
        [types.DashboardVisualisationType.SCORECARD]: dashboardFeatureFlags[featureFlagsHelper.FEATURE_FLAG_KEYS.SCORECARD_CHARTS],
        [types.DashboardVisualisationType.SCORECARD_GROUP]: dashboardFeatureFlags[featureFlagsHelper.FEATURE_FLAG_KEYS.SCORECARD_GROUP_CHARTS],
        [types.DashboardVisualisationType.MATRIX_TIMESERIES]: dashboardFeatureFlags[featureFlagsHelper.FEATURE_FLAG_KEYS.MATRIX_TIMESERIES_CHARTS],
        [types.DashboardVisualisationType.BAR_TIMESERIES]: dashboardFeatureFlags[featureFlagsHelper.FEATURE_FLAG_KEYS.BAR_TIMESERIES_CHARTS],
        [types.DashboardVisualisationType.LINE_TIMESERIES]: dashboardFeatureFlags[featureFlagsHelper.FEATURE_FLAG_KEYS.LINE_TIMESERIES_CHARTS],
    };
};

exports.getFeatureFlagVisTypeMap = getFeatureFlagVisTypeMap;
exports.validateDashboardVisualisations = validateDashboardVisualisations;
//# sourceMappingURL=utils.js.map
