"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AllFragmentsSchema = exports.AttributeFragmentSchema = exports.RecordFragmentSchema = exports.StringArrayFragmentSchema = exports.ArrayFragmentSchema = exports.BooleanFragmentSchema = exports.StringFragmentSchema = void 0;
exports.string = string;
exports.boolean = boolean;
exports.record = record;
exports.attribute = attribute;
exports.array = array;
exports.isValue = isValue;
exports.isPrimitive = isPrimitive;
const tslib_1 = require("tslib");
const zod_1 = require("zod");
const schema_utils_1 = require("../../../util/schema-utils");
const starlark = tslib_1.__importStar(require("./starlark"));
exports.StringFragmentSchema = zod_1.z.object({
    type: zod_1.z.literal('string'),
    value: zod_1.z.string(),
    isComplete: zod_1.z.literal(true),
});
exports.BooleanFragmentSchema = zod_1.z.object({
    type: zod_1.z.literal('boolean'),
    value: zod_1.z.boolean(),
    isComplete: zod_1.z.literal(true),
});
const PrimitiveFragmentsSchema = zod_1.z.discriminatedUnion('type', [
    exports.StringFragmentSchema,
    exports.BooleanFragmentSchema,
]);
exports.ArrayFragmentSchema = zod_1.z.object({
    type: zod_1.z.literal('array'),
    items: (0, schema_utils_1.LooseArray)(PrimitiveFragmentsSchema),
    isComplete: zod_1.z.boolean(),
});
exports.StringArrayFragmentSchema = zod_1.z.object({
    type: zod_1.z.literal('array'),
    items: (0, schema_utils_1.LooseArray)(exports.StringFragmentSchema),
    isComplete: zod_1.z.boolean(),
});
const ValueFragmentsSchema = zod_1.z.discriminatedUnion('type', [
    exports.StringFragmentSchema,
    exports.BooleanFragmentSchema,
    exports.ArrayFragmentSchema,
]);
exports.RecordFragmentSchema = zod_1.z.object({
    type: zod_1.z.literal('record'),
    children: (0, schema_utils_1.LooseRecord)(ValueFragmentsSchema),
    isComplete: zod_1.z.boolean(),
});
exports.AttributeFragmentSchema = zod_1.z.object({
    type: zod_1.z.literal('attribute'),
    name: zod_1.z.string(),
    value: ValueFragmentsSchema.optional(),
    isComplete: zod_1.z.boolean(),
});
exports.AllFragmentsSchema = zod_1.z.discriminatedUnion('type', [
    exports.ArrayFragmentSchema,
    exports.AttributeFragmentSchema,
    exports.BooleanFragmentSchema,
    exports.RecordFragmentSchema,
    exports.StringFragmentSchema,
]);
function string(value) {
    return {
        type: 'string',
        isComplete: true,
        value,
    };
}
function boolean(value) {
    return {
        type: 'boolean',
        isComplete: true,
        value: typeof value === 'string' ? starlark.asBoolean(value) : value,
    };
}
function record(children = {}, isComplete = false) {
    return {
        type: 'record',
        isComplete,
        children,
    };
}
function attribute(name, value, isComplete = false) {
    return {
        type: 'attribute',
        name,
        value,
        isComplete,
    };
}
function array(items = [], isComplete = false) {
    return {
        type: 'array',
        items,
        isComplete,
    };
}
function isValue(data) {
    const result = ValueFragmentsSchema.safeParse(data);
    return result.success;
}
function isPrimitive(data) {
    const result = PrimitiveFragmentsSchema.safeParse(data);
    return result.success;
}
//# sourceMappingURL=fragments.js.map