import { authRoleEntityType, definitionSchema, } from '@baseplate-dev/project-builder-lib';
import { z } from 'zod';
import { AUTH_DEFAULT_ROLES } from './constants.js';
export const createAuthRoleSchema = definitionSchema((ctx) => ctx.withEnt(z.object({
    id: z.string(),
    name: z.string().min(1),
    comment: z.string().min(1),
    builtIn: z.boolean().default(false),
}), { type: authRoleEntityType }));
/**
 * Defines the schema for an array of roles ensuring that there are no duplicate
 * role names and that the built-in roles are included.
 */
export const createAuthRolesSchema = definitionSchema((ctx) => z
    .array(createAuthRoleSchema(ctx))
    .superRefine((roles, ctx) => {
    const dup = roles
        .map((r) => r.name)
        .filter((v, i, a) => a.indexOf(v) !== i);
    if (dup.length > 0) {
        ctx.addIssue({
            code: 'custom',
            message: `Duplicate role name(s): ${dup.join(', ')}`,
        });
    }
})
    .superRefine((roles, ctx) => {
    for (const defaultRole of AUTH_DEFAULT_ROLES) {
        if (!roles.some((r) => r.name === defaultRole.name && r.builtIn)) {
            ctx.addIssue({
                code: 'custom',
                message: `Missing built-in role: ${defaultRole.name}`,
            });
        }
    }
}));
//# sourceMappingURL=schema.js.map