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 Admin2iosMenuBuilder from "~/services/layout/menuBuilder/admin2iosMenuBuilder";
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: Admin2iosMenuBuilder

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

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

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


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

describe('build', () => {
    test('has all items', () => {
        ability.can = vi.fn(() => true)

        // Should return a MenuGroup
        const result = menuBuilder.build() as MenuGroup

        expect(result.label).toEqual('admin2ios')
        expect(result.icon).toEqual({name: 'fas fa-sitemap'})
        // @ts-ignore
        expect(result.children.length).toEqual(7)
    })

    test('has no items', () => {
        ability.can = vi.fn(() => false)
        expect(menuBuilder.build()).toEqual(null)
    })

    test('has only rights for menu all_accesses', () => {
        ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'all_accesses_page')

        expect(menuBuilder.build()).toEqual(
            {label: 'all_accesses', icon: {name: 'fas fa-users'}, to: 'https://mydomain.com/all_accesses/list/', type: MENU_LINK_TYPE.V1, active: false}
        )
    })

    test('has only rights for menu all_organizations', () => {
        ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'all_organizations_page')

        expect(menuBuilder.build()).toEqual(
            {label: 'all_organizations', icon: {name: 'fas fa-building'}, to: 'https://mydomain.com/organization_params/list/', type: MENU_LINK_TYPE.V1, active: false}
        )
    })

    test('has only rights for menu tips', () => {
        ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'tips_page')

        expect(menuBuilder.build()).toEqual(
            {label: 'tips', icon: {name: 'fas fa-info-circle'}, to: 'https://mydomain.com/tips/list/', type: MENU_LINK_TYPE.V1, active: false}
        )
    })

    test('has only rights for menu dgv', () => {
        ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'dgv_page')

        expect(menuBuilder.build()).toEqual(
            {label: 'dgv', icon: {name: 'fas fa-house-damage'}, to: 'https://mydomain.com/admin2ios/dgv', type: MENU_LINK_TYPE.V1, active: false}
        )
    })

    test('has only rights for menu cmf_cotisation', () => {
        ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'cmf_cotisation_page')

        expect(menuBuilder.build()).toEqual(
            {label: 'cmf_cotisation', icon: {name: 'fas fa-info-circle'}, to: 'https://mydomain.com/admin2ios/cotisationcmf', type: MENU_LINK_TYPE.V1, active: false}
        )
    })

    test('has only rights for menu right_menu', () => {
        ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'right_page')

        expect(menuBuilder.build()).toEqual(
            {label: 'right_menu', icon: {name: 'fas fa-balance-scale-right'}, to: 'https://mydomain.com/admin2ios/right', type: MENU_LINK_TYPE.V1, active: false}
        )
    })

    test('has only rights for menu tree_menu', () => {
        ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'tree_page')

        expect(menuBuilder.build()).toEqual(
            {label: 'tree_menu', icon: {name: 'fas fa-sitemap'}, to: 'https://mydomain.com/admin2ios/tree', type: MENU_LINK_TYPE.V1, active: false}
        )
    })
})
