import { Entity } from '@iqser/common-ui';
import { IPermissionsMapping } from './permissions-mapping';
import { IPermission } from './permissions';

export class PermissionsMapping extends Entity<IPermissionsMapping> implements IPermissionsMapping {
    readonly routerLink = undefined;
    readonly mappedPermissions: IPermission[];
    readonly targetPermission: IPermission;
    readonly sort: number;

    readonly #checked: string[];
    readonly #disabled: string[];

    constructor(permissionsMapping: IPermissionsMapping) {
        super(permissionsMapping);
        this.mappedPermissions = permissionsMapping.mappedPermissions;
        this.targetPermission = permissionsMapping.targetPermission;
        this.sort = this.targetPermission.sort;
        this.#checked = this.mappedPermissions.map(permission => permission.name);
        this.#disabled = this.mappedPermissions.filter(permission => !permission.changeable).map(permission => permission.name);
    }

    get id(): string {
        return this.targetPermission.name;
    }

    get searchKey(): string {
        return this.targetPermission.name;
    }

    isChecked(permissionName: string): boolean {
        return this.#checked.includes(permissionName);
    }

    isDisabled(permissionName: string): boolean {
        return this.#disabled.includes(permissionName);
    }
}
