import { ZodError } from 'zod';
import BarChartSchemas from '../../_charts/chart/bar/validate.js';
import DoughnutChartSchemas from '../../_charts/chart/doughnut/validate.js';
import LineChartSchemas from '../../_charts/chart/line/validate.js';
import LineTimeseriesChartSchemas from '../../_charts/chart/line-timeseries/validate.js';
import BarTimeseriesChartSchemas from '../../_charts/chart/bar-timeseries/validate.js';
import MatrixTimeseriesSchema from '../../_charts/chart/heatmap/validate.js';
import ListSchemas from '../dashboard-list/validate.js';
import ScorecardSchemas from '../scorecard/validate.js';
import ScorecardGroupSchemas from '../scorecard-group/validate.js';
import { AggregatedValidationError } from '../../../utils/ErrorHandler/AggregatedValidationError.js';
import { DashboardVisualisationType } from './types.js';
import { FEATURE_FLAG_KEYS } from '../../../utils/featureFlagsHelper.js';

const schemaMap = {
    list: ListSchemas.ListSchema,
    bar: BarChartSchemas.BarSchema,
    'bar-timeseries': BarTimeseriesChartSchemas.BarTimeseriesSchema,
    'line-timeseries': LineTimeseriesChartSchemas.LineTimeseriesSchema,
    doughnut: DoughnutChartSchemas.DounutMeasureSchema,
    'matrix-timeseries': MatrixTimeseriesSchema,
    line: LineChartSchemas.LineSchema,
    scorecard: ScorecardSchemas.ScorecardSchema,
    'scorecard-group': ScorecardGroupSchemas.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 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 ZodError(indexedIssues);
        const details = failures.map((f) => ({
            index: f.index,
            type: f.type,
            id: f.id,
            issues: f.error.issues,
        }));
        throw new AggregatedValidationError('Error: Schema validation: Dashboard Visualisation validation failed:', aggregatedZod, details);
    }
    return results.filter((r) => r.success).map((r) => r.data);
}
const getFeatureFlagVisTypeMap = (dashboardFeatureFlags) => {
    return {
        [DashboardVisualisationType.LIST]: true,
        [DashboardVisualisationType.BAR]: dashboardFeatureFlags[FEATURE_FLAG_KEYS.BAR_CHARTS],
        [DashboardVisualisationType.LINE]: dashboardFeatureFlags[FEATURE_FLAG_KEYS.LINE_CHARTS],
        [DashboardVisualisationType.DONUT]: dashboardFeatureFlags[FEATURE_FLAG_KEYS.DONUT_CHARTS],
        [DashboardVisualisationType.SCORECARD]: dashboardFeatureFlags[FEATURE_FLAG_KEYS.SCORECARD_CHARTS],
        [DashboardVisualisationType.SCORECARD_GROUP]: dashboardFeatureFlags[FEATURE_FLAG_KEYS.SCORECARD_GROUP_CHARTS],
        [DashboardVisualisationType.MATRIX_TIMESERIES]: dashboardFeatureFlags[FEATURE_FLAG_KEYS.MATRIX_TIMESERIES_CHARTS],
        [DashboardVisualisationType.BAR_TIMESERIES]: dashboardFeatureFlags[FEATURE_FLAG_KEYS.BAR_TIMESERIES_CHARTS],
        [DashboardVisualisationType.LINE_TIMESERIES]: dashboardFeatureFlags[FEATURE_FLAG_KEYS.LINE_TIMESERIES_CHARTS],
    };
};

export { getFeatureFlagVisTypeMap, validateDashboardVisualisations };
//# sourceMappingURL=utils.js.map
