import { z } from 'zod';
/**
 * Creates a Guard around a Zod schema.
 *
 * @param schema      Zod schema to validate against.
 * @param instigator  Label included in thrown error messages — gives callers
 *                    a clear hint about which guard rejected their input.
 *                    Use the entity / schema name (e.g. 'reviewAction').
 */
export function createGuard(schema, instigator) {
    const arraySchema = z.array(schema);
    return {
        isValid: (data) => {
            return schema.safeParse(data).success;
        },
        parse: (data) => {
            const result = schema.safeParse(data);
            if (!result.success) {
                throw new Error(`[${instigator}] Validation failed: ${result.error.message}`);
            }
            return result.data;
        },
        safeParse: (data) => {
            return schema.safeParse(data);
        },
        isValidCollection: (data) => {
            return arraySchema.safeParse(data).success;
        },
        parseCollection: (data) => {
            const result = arraySchema.safeParse(data);
            if (!result.success) {
                throw new Error(`[${instigator}] Collection validation failed: ${result.error.message}`);
            }
            return result.data;
        },
        safeParseCollection: (data) => {
            return arraySchema.safeParse(data);
        },
        filterCollection: (items) => {
            const valid = [];
            const rejected = [];
            for (const item of items) {
                const result = schema.safeParse(item);
                if (result.success) {
                    valid.push(result.data);
                }
                else {
                    rejected.push({ rawData: item, issues: result.error.issues });
                }
            }
            return { valid, rejected };
        },
    };
}
//# sourceMappingURL=guard.base.js.map