import { describe, test, expect, vi, beforeEach } from 'vitest'
import type { RuntimeConfig } from '@nuxt/schema'
import type { AnyAbility } from '@casl/ability/dist/types'
import type { Router } from 'vue-router'
import type { AccessProfile, organizationState } from '~/types/interfaces'
import WebsiteAdminMenuBuilder from '~/services/layout/menuBuilder/websiteAdminMenuBuilder'
import { MENU_LINK_TYPE } from '~/types/enum/layout'

let runtimeConfig: RuntimeConfig
let ability: AnyAbility
let organizationProfile: organizationState
let accessProfile: AccessProfile
let menuBuilder: WebsiteAdminMenuBuilder
let router: Router

beforeEach(() => {
  runtimeConfig = vi.fn() as any as RuntimeConfig
  ability = vi.fn() as any as AnyAbility
  organizationProfile = vi.fn() as any as organizationState
  accessProfile = vi.fn() as any as AccessProfile
  // @ts-ignore
  router = vi.fn() as Router

  runtimeConfig.baseUrlAdminLegacy = 'https://mydomain.com/'

  menuBuilder = new WebsiteAdminMenuBuilder(
    runtimeConfig,
    ability,
    organizationProfile,
    accessProfile,
    router,
  )
})

describe('getMenuName', () => {
  test('validate name', () => {
    expect(menuBuilder.getMenuName()).toEqual('WebsiteAdmin')
  })
})

describe('build', () => {
  test('without website', () => {
    organizationProfile.website = null
    expect(menuBuilder.build()).toEqual(null)
  })
  test('without admin access', () => {
    organizationProfile.website = 'https://some-website.com'
    accessProfile.isAdminAccess = false
    expect(menuBuilder.build()).toEqual(null)
  })
  test('with website and admin access', () => {
    organizationProfile.website = 'https://some-website.com'
    accessProfile.isAdminAccess = true
    expect(menuBuilder.build()).toEqual({
      label: 'advanced_modification',
      icon: { name: 'fas fa-globe-americas' },
      to: 'https://some-website.com/typo3',
      type: MENU_LINK_TYPE.EXTERNAL,
      active: false,
    })
  })
})
