import { describe, test, it, expect } from 'vitest'
import {RuntimeConfig} from "@nuxt/schema";
import {AnyAbility} from "@casl/ability/dist/types";
import {AccessProfile, organizationState} from "~/types/interfaces";
import WebsiteListMenuBuilder from "~/services/layout/menuBuilder/websiteListMenuBuilder";
import {MenuGroup} from "~/types/layout";
import {MENU_LINK_TYPE} from "~/types/enum/layout";

let runtimeConfig: RuntimeConfig
let ability: AnyAbility
let organizationProfile: organizationState
let accessProfile: AccessProfile
let menuBuilder: WebsiteListMenuBuilder

beforeEach(()=> {
    runtimeConfig = vi.fn() as any as RuntimeConfig
    ability = vi.fn() as any as AnyAbility
    organizationProfile = {} as any as organizationState
    accessProfile = vi.fn() as any as AccessProfile

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

    menuBuilder = new WebsiteListMenuBuilder(runtimeConfig, ability, organizationProfile, accessProfile)
})


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

describe('build', () => {
    test('without website and parents', () => {
        organizationProfile.website = null
        organizationProfile.parents = []
        expect(menuBuilder.build()).toEqual(null)
    })

    test('with website but no parents', () => {
        organizationProfile.name = 'MyOrganization'
        organizationProfile.website = 'https://some-website.com'
        organizationProfile.parents = []

        const result = menuBuilder.build() as MenuGroup

        expect(result.children).toEqual([
            {
                label: 'MyOrganization',
                icon: undefined,
                to: 'https://some-website.com/typo3',
                type: MENU_LINK_TYPE.EXTERNAL,
                active: false
            }
        ])
    })

    test('with parents but no website', () => {
        organizationProfile.name = 'MyOrganization'
        organizationProfile.website = ''
        organizationProfile.parents = [
            {id: 1, name: 'parent1', website: 'https://parent1.net'},
            {id: 2, name: 'parent2', website: 'https://parent2.net'},
            {id: 3, name: 'parent3', website: 'https://parent3.net'},
        ]

        const result = menuBuilder.build() as MenuGroup

        expect(result.children).toEqual([
            { label: 'parent1', icon: undefined, to: 'https://parent1.net', type: MENU_LINK_TYPE.EXTERNAL, active: false },
            { label: 'parent2', icon: undefined, to: 'https://parent2.net', type: MENU_LINK_TYPE.EXTERNAL, active: false },
            { label: 'parent3', icon: undefined, to: 'https://parent3.net', type: MENU_LINK_TYPE.EXTERNAL, active: false },
        ])
    })

    test('with parents and website, opentalent is excluded from parents', () => {
        organizationProfile.name = 'MyOrganization'
        organizationProfile.website = 'https://some-website.com'
        organizationProfile.parents = [
            {id: 1, name: 'parent1', website: 'https://parent1.net'},
            {id: 2, name: 'parent2', website: 'https://parent2.net'},
            {id: 3, name: 'parent3', website: 'https://parent3.net'},
        ]

        runtimeConfig.OPENTALENT_MANAGER_ID = 3

        const result = menuBuilder.build() as MenuGroup

        expect(result.children).toEqual([
            { label: 'MyOrganization', icon: undefined, to: 'https://some-website.com/typo3', type: MENU_LINK_TYPE.EXTERNAL, active: false },
            { label: 'parent1', icon: undefined, to: 'https://parent1.net', type: MENU_LINK_TYPE.EXTERNAL, active: false },
            { label: 'parent2', icon: undefined, to: 'https://parent2.net', type: MENU_LINK_TYPE.EXTERNAL, active: false },
        ])
    })
})

