import { z } from 'astro/zod'

export const MetadataSchema = z
  .object({
    /**
     * Add a badge to the directory containing the metadata file in the sidebar when displayed in an autogenerated
     * sidebar group.
     */
    badge: z
      .union([
        z.string(),
        z.object({
          class: z.string().optional(),
          text: z.string(),
          variant: z.enum(['note', 'danger', 'success', 'caution', 'tip', 'default']).default('default'),
        }),
      ])
      .transform((value) => (typeof value === 'string' ? { text: value, variant: 'default' as const } : value))
      .optional(),
    /**
     * Cascade some options of the directory containing the metadata file to all nested directories when displayed in an
     * autogenerated sidebar group.
     */
    cascade: z.array(z.union([z.literal('collapsed'), z.literal('sort')])).optional(),
    /**
     * Define whether the directory containing the metadata file in the sidebar should be collapsed or not when
     * displayed in an autogenerated sidebar group.
     *
     * @default false
     */
    collapsed: z.boolean().default(false),
    /**
     * Limit the depth of content in the directory containing the metadata file when displayed in an autogenerated
     * sidebar group.
     *
     * @default Infinity
     */
    depth: z.number().gte(1).optional(),
    /**
     * Prevents the directory containing the metadata file from being included in an autogenerated sidebar group.
     *
     * @default false
     */
    hidden: z.boolean().default(false),
    /**
     * Set the label of the directory containing the metadata file in the sidebar when displayed in an autogenerated
     * sidebar group.
     */
    label: z.string().optional(),
    /**
     * Control the order of the directory containing the metadata file when sorting an autogenerated group.
     */
    order: z.number().optional(),
    /**
     * Control the sorting order of the content in the directory containing the metadata file.
     *
     * @default 'slug'
     */
    sort: z.enum(['slug', 'reverse-slug']).default('slug'),
  })
  .strict()

export function autoSidebarSchema() {
  return MetadataSchema
}
