"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChangesetWithChangesSchema = exports.ChangeSchema = exports.ChangeBaseSchema = exports.ChangesetSchema = void 0;
const zod_1 = require("zod");
const field_1 = require("./field");
const ChangesetStatusSchema = zod_1.z.enum(['draft', 'applied']);
exports.ChangesetSchema = zod_1.z.object({
    changesetUuid: zod_1.z.string().uuid(),
    createdAt: zod_1.z.date(),
    updatedAt: zod_1.z.date(),
    createdByUserUuid: zod_1.z.string().uuid(),
    updatedByUserUuid: zod_1.z.string().uuid(),
    projectUuid: zod_1.z.string().uuid(),
    status: ChangesetStatusSchema,
    name: zod_1.z.string().min(1),
});
exports.ChangeBaseSchema = zod_1.z
    .object({
    entityType: zod_1.z.enum(['table', 'dimension', 'metric']),
    entityTableName: zod_1.z.string().min(1),
    entityName: zod_1.z.string().min(1),
})
    .and(zod_1.z.discriminatedUnion('type', [
    zod_1.z.object({
        type: zod_1.z.literal('create'),
        payload: zod_1.z.discriminatedUnion('type', [
            zod_1.z.object({
                type: zod_1.z.literal('metric'),
                value: zod_1.z.object({
                    fieldType: zod_1.z.literal(field_1.FieldType.METRIC),
                    type: zod_1.z.nativeEnum(field_1.MetricType),
                    name: zod_1.z.string(),
                    label: zod_1.z.string(),
                    table: zod_1.z.string(),
                    tableLabel: zod_1.z.string(), // Table friendly name
                    sql: zod_1.z.string(), // Templated sql
                    description: zod_1.z.string().optional(),
                    hidden: zod_1.z.boolean(),
                    compiledSql: zod_1.z.string(),
                    tablesReferences: zod_1.z.array(zod_1.z.string()).optional(),
                    tablesRequiredAttributes: zod_1.z
                        .record(zod_1.z.string(), zod_1.z.record(zod_1.z.string(), zod_1.z.string().or(zod_1.z.array(zod_1.z.string()))))
                        .optional(),
                    tablesAnyAttributes: zod_1.z
                        .record(zod_1.z.string(), zod_1.z.record(zod_1.z.string(), zod_1.z.string().or(zod_1.z.array(zod_1.z.string()))))
                        .optional(),
                }),
            }),
        ]),
    }),
    zod_1.z.object({
        type: zod_1.z.literal('update'),
        payload: zod_1.z.object({
            patches: zod_1.z.array(zod_1.z.object({
                op: zod_1.z.enum(['replace', 'add']),
                path: zod_1.z.string(),
                value: zod_1.z
                    .unknown()
                    .refine((value) => value !== undefined),
            })),
        }),
    }),
    zod_1.z.object({
        type: zod_1.z.literal('delete'),
        payload: zod_1.z.object({}),
    }),
]));
exports.ChangeSchema = exports.ChangeBaseSchema.and(zod_1.z.object({
    changeUuid: zod_1.z.string().uuid(),
    changesetUuid: zod_1.z.string().uuid(),
    createdAt: zod_1.z.date(),
    createdByUserUuid: zod_1.z.string().uuid(),
    sourcePromptUuid: zod_1.z.string().uuid().nullable(),
}));
exports.ChangesetWithChangesSchema = exports.ChangesetSchema.extend({
    changes: zod_1.z.array(exports.ChangeSchema),
});
//# sourceMappingURL=changeset.js.map