import { importShared } from './__federation_fn_import-BLFDCY-V.js';

const {authRoleEntityType: authRoleEntityType$1} = await importShared('@baseplate-dev/project-builder-lib');

const AUTH_DEFAULT_ROLES = [
  {
    name: "public",
    comment: "All users (including unauthenticated and authenticated users)",
    builtIn: true
  },
  {
    name: "user",
    comment: "All authenticated users",
    builtIn: true
  },
  {
    name: "system",
    comment: "System processes without a user context, e.g. background jobs",
    builtIn: true
  }
];
function createDefaultAuthRoles() {
  return AUTH_DEFAULT_ROLES.map((role) => ({
    ...role,
    id: authRoleEntityType$1.generateNewId()
  }));
}

const {authRoleEntityType,definitionSchema: definitionSchema$1} = await importShared('@baseplate-dev/project-builder-lib');

const {z: z$1} = await importShared('zod');
const createAuthRoleSchema = definitionSchema$1(
  (ctx) => ctx.withEnt(
    z$1.object({
      id: z$1.string(),
      name: z$1.string().min(1),
      comment: z$1.string().min(1),
      builtIn: z$1.boolean().default(false)
    }),
    { type: authRoleEntityType }
  )
);
const createAuthRolesSchema = definitionSchema$1(
  (ctx) => z$1.array(createAuthRoleSchema(ctx)).superRefine((roles, ctx2) => {
    const dup = roles.map((r) => r.name).filter((v, i, a) => a.indexOf(v) !== i);
    if (dup.length > 0) {
      ctx2.addIssue({
        code: "custom",
        message: `Duplicate role name(s): ${dup.join(", ")}`
      });
    }
  }).superRefine((roles, ctx2) => {
    for (const defaultRole of AUTH_DEFAULT_ROLES) {
      if (!roles.some((r) => r.name === defaultRole.name && r.builtIn)) {
        ctx2.addIssue({
          code: "custom",
          message: `Missing built-in role: ${defaultRole.name}`
        });
      }
    }
  })
);

const {definitionSchema,featureEntityType} = await importShared('@baseplate-dev/project-builder-lib');

const {z} = await importShared('zod');
const createAuthPluginDefinitionSchema = definitionSchema(
  (ctx) => z.object({
    implementationPluginKey: z.string().min(1, "Auth implementation plugin must be selected"),
    authFeatureRef: ctx.withRef({
      type: featureEntityType,
      onDelete: "RESTRICT"
    }),
    roles: createAuthRolesSchema(ctx)
  })
);

export { createAuthPluginDefinitionSchema as a, createDefaultAuthRoles as b, createAuthRoleSchema as c };
//# sourceMappingURL=plugin-definition-DJzXScRp.js.map
