import { GlobalConfig } from 'payload'
import { isAdmin } from '@/collections/access/admin'
import { revalidateTag } from 'next/cache'
import { validateHref } from '@/globals/ValidateHref'

export const FooterGlobal: GlobalConfig = {
  slug: 'footer',
  label: {
    de: 'Fußzeile',
  },
  admin: {
    description:
      'Hier können Sie die Linkgruppen im Fußzeile konfigurieren.',
    hidden: args => args.user?.roles === "user"
  },
  fields: [
    {
      name: 'groups',
      label: {
        de: 'Linkgruppen',
      },
      type: 'array',
      fields: [
        {
          name: 'title',
          type: 'text',
          required: true,
          label: {
            de: 'Titel',
          },
        },
        {
          name: 'links',
          type: 'array',
          label: {
            de: 'Links',
          },
          fields: [
            {
              name: 'label',
              type: 'text',
              required: true,
              label: {
                de: 'Bezeichnung',
              },
            },
            {
              name: 'href',
              type: 'text',
              required: true,
              label: {
                de: 'Zieladresse',
              },
              validate: validateHref,
            },
          ],
        },
      ],
    },
  ],
  access: {
    read: () => true,
    update: isAdmin(),
  },
  hooks: {
    afterChange: [() => revalidateTag('footer')],
  },
}
