import { Component, inject } from '@angular/core';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import { Roles } from '@users/roles';
import { IqserPermissionsService } from '@iqser/common-ui';
import { User } from '@red/domain';
import { UserService } from '@users/user.service';
import { getCurrentUser } from '@iqser/common-ui/lib/users';
import { List } from '@iqser/common-ui/lib/utils';
import { TenantsService } from '@iqser/common-ui/lib/tenants';

interface MenuItem {
    readonly id: string;
    readonly name: string;
    readonly routerLink?: string;
    readonly show: boolean;
    readonly action?: () => void;
    readonly showDot?: () => boolean;
}

@Component({
    selector: 'app-user-menu',
    templateUrl: './user-menu.component.html',
    styleUrls: ['./user-menu.component.scss'],
})
export class UserMenuComponent {
    readonly #permissionsService = inject(IqserPermissionsService);
    readonly currentUser = getCurrentUser<User>();
    readonly tenantsService = inject(TenantsService);
    readonly userService = inject(UserService);
    readonly userMenuItems: List<MenuItem> = [
        {
            id: 'account',
            name: _('top-bar.navigation-items.my-account.children.account'),
            routerLink: '/main/account',
            show: true,
        },
        {
            id: 'admin',
            name: _('top-bar.navigation-items.my-account.children.admin'),
            routerLink: '/main/admin',
            show: (this.currentUser.isManager || this.currentUser.isUserAdmin) && this.#permissionsService.has([Roles.templates.read]),
        },
        {
            id: 'downloads',
            name: _('top-bar.navigation-items.my-account.children.downloads'),
            routerLink: '/main/downloads',
            show: this.currentUser.isUser && this.#permissionsService.has(Roles.readDownloadStatus),
        },
        {
            id: 'trash',
            name: _('top-bar.navigation-items.my-account.children.trash'),
            routerLink: '/main/trash',
            show: this.currentUser.isUser && this.#permissionsService.has([Roles.dossiers.read, Roles.files.readStatus]),
        },
    ];

    trackBy(_index: number, item: MenuItem) {
        return item.id;
    }
}
