import type { GlobalConfig } from 'payload'

export const Navigation: GlobalConfig = {
  slug: 'navigation',
  label: 'Navigation',
  access: {
    read: () => true,
    update: ({ req }) => !!req.user,
  },
  fields: [
    {
      name: 'mainMenu',
      type: 'array',
      label: 'Hauptmenü',
      fields: [
        {
          name: 'label',
          type: 'text',
          required: true,
          localized: true,
        },
        {
          name: 'type',
          type: 'select',
          defaultValue: 'page',
          options: [
            { label: 'Seite', value: 'page' },
            { label: 'Eigener Link', value: 'custom' },
            { label: 'Untermenü', value: 'submenu' },
          ],
        },
        {
          name: 'page',
          type: 'relationship',
          relationTo: 'pages',
          admin: {
            condition: (data, siblingData) => siblingData?.type === 'page',
          },
        },
        {
          name: 'url',
          type: 'text',
          admin: {
            condition: (data, siblingData) => siblingData?.type === 'custom',
          },
        },
        {
          name: 'openInNewTab',
          type: 'checkbox',
          defaultValue: false,
        },
        {
          name: 'submenu',
          type: 'array',
          admin: {
            condition: (data, siblingData) => siblingData?.type === 'submenu',
          },
          fields: [
            {
              name: 'label',
              type: 'text',
              required: true,
              localized: true,
            },
            {
              name: 'linkType',
              type: 'select',
              defaultValue: 'page',
              options: [
                { label: 'Seite', value: 'page' },
                { label: 'Eigener Link', value: 'custom' },
              ],
            },
            {
              name: 'page',
              type: 'relationship',
              relationTo: 'pages',
              admin: {
                condition: (data, siblingData) => siblingData?.linkType === 'page',
              },
            },
            {
              name: 'url',
              type: 'text',
              admin: {
                condition: (data, siblingData) => siblingData?.linkType === 'custom',
              },
            },
          ],
        },
      ],
    },
    {
      name: 'footerMenu',
      type: 'array',
      label: 'Footer-Menü',
      fields: [
        {
          name: 'label',
          type: 'text',
          required: true,
          localized: true,
        },
        {
          name: 'linkType',
          type: 'select',
          defaultValue: 'page',
          options: [
            { label: 'Seite', value: 'page' },
            { label: 'Eigener Link', value: 'custom' },
          ],
        },
        {
          name: 'page',
          type: 'relationship',
          relationTo: 'pages',
          admin: {
            condition: (data, siblingData) => siblingData?.linkType === 'page',
          },
        },
        {
          name: 'url',
          type: 'text',
          admin: {
            condition: (data, siblingData) => siblingData?.linkType === 'custom',
          },
        },
      ],
    },
  ],
}
