"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChartAsCodeInternalization = void 0;
const zod_1 = require("zod");
const savedCharts_1 = require("../../types/savedCharts");
const abstract_1 = require("./abstract");
const merge_1 = require("./merge");
const chartAsCodeSchema = zod_1.z.object({
    name: zod_1.z.string(),
    description: zod_1.z
        .string()
        .nullable()
        .optional()
        .transform((str) => str ?? undefined),
    chartConfig: zod_1.z.union([
        // cartesian chart schema
        zod_1.z.object({
            type: zod_1.z.literal(savedCharts_1.ChartType.CARTESIAN),
            config: zod_1.z
                .object({
                eChartsConfig: zod_1.z.object({
                    xAxis: zod_1.z
                        .array(zod_1.z.object({
                        name: zod_1.z.string().optional(),
                    }))
                        .optional(),
                    yAxis: zod_1.z
                        .array(zod_1.z.object({
                        name: zod_1.z.string().optional(),
                    }))
                        .optional(),
                    // chartConfig.config?.eChartsConfig.series[0].markLine?.data[0].name
                    series: zod_1.z
                        .array(zod_1.z.object({
                        name: zod_1.z.string().optional(),
                        markLine: zod_1.z
                            .object({
                            data: zod_1.z.array(zod_1.z.object({
                                name: zod_1.z.string().optional(),
                            })),
                        })
                            .optional(),
                    }))
                        .optional(),
                }),
            })
                .nullable()
                .optional()
                .transform((value) => value ?? undefined),
        }),
        // pie chart schema
        zod_1.z.object({
            type: zod_1.z.literal(savedCharts_1.ChartType.PIE),
            config: zod_1.z
                .object({
                groupLabelOverrides: zod_1.z
                    .record(zod_1.z.string(), zod_1.z.string())
                    .optional(),
            })
                .nullable()
                .optional()
                .transform((value) => value ?? undefined),
        }),
        // funnel chart schema
        zod_1.z.object({
            type: zod_1.z.literal(savedCharts_1.ChartType.FUNNEL),
            config: zod_1.z
                .object({
                labelOverrides: zod_1.z.record(zod_1.z.string(), zod_1.z.string()).optional(),
            })
                .nullable()
                .optional()
                .transform((value) => value ?? undefined),
        }),
        // big number chart schema
        zod_1.z.object({
            type: zod_1.z.literal(savedCharts_1.ChartType.BIG_NUMBER),
            config: zod_1.z
                .object({
                label: zod_1.z.string().optional(),
                comparisonLabel: zod_1.z.string().optional(),
                comparisonField: zod_1.z.string().optional(),
            })
                .nullable()
                .optional()
                .transform((value) => value ?? undefined),
        }),
        // table chart schema
        zod_1.z.object({
            type: zod_1.z.literal(savedCharts_1.ChartType.TABLE),
            config: zod_1.z
                .object({
                columns: zod_1.z
                    .record(zod_1.z.string(), zod_1.z.object({
                    name: zod_1.z.string(),
                }))
                    .optional(),
            })
                .nullable()
                .optional()
                .transform((value) => value ?? undefined),
        }),
        // custom chart schema
        zod_1.z.object({
            type: zod_1.z.literal(savedCharts_1.ChartType.CUSTOM),
            config: zod_1.z
                .object({
                spec: zod_1.z.record(zod_1.z.unknown()).optional(),
            })
                .nullable()
                .optional()
                .transform((value) => value ?? undefined),
        }),
    ]),
});
class ChartAsCodeInternalization extends abstract_1.AsCodeInternalization {
    constructor(schema = chartAsCodeSchema) {
        super();
        this.schema = schema;
    }
    getLanguageMap(chartAsCode) {
        const languageMapSchema = this.schema.deepPartial().strip();
        const parsedChartAsCode = languageMapSchema.safeParse(chartAsCode);
        const languageMapEntry = parsedChartAsCode.success
            ? parsedChartAsCode.data
            : chartAsCodeSchema
                .pick({ name: true, description: true })
                .strip()
                .parse(chartAsCode);
        return {
            chart: {
                [chartAsCode.slug]: languageMapEntry,
            },
        };
    }
    // eslint-disable-next-line class-methods-use-this
    merge(internalizationMap, content) {
        return (0, merge_1.mergeExisting)(content, internalizationMap);
    }
}
exports.ChartAsCodeInternalization = ChartAsCodeInternalization;
//# sourceMappingURL=chartAsCode.js.map