import {
    IsArray,
    IsOptional
} from "class-validator";
import { UserGroup } from '../entities/UserGroup';
import { ResponseObjectType } from '../enums/ResponseObjectType';
import { IResponse } from './IResponse';
import { ResponsePermission } from './ResponsePermission';

/**
 * Defines the group permission response (get /api/groups/:id/permissions).
*/
export class ResponseUserGroupPermissions implements IResponse {
    /**
    * The responseType.
    * This contains the type of class/entity this response contains.
    */
    responseType: ResponseObjectType = ResponseObjectType.USERPERMISSIONS;

    /**
     * The permissions directly granted to the group.
     */
    @IsArray()
    @IsOptional()
    directlyGranted: ResponsePermission[] = new Array<ResponsePermission>();

    /**
     * Is just here for compatability.
     */
    @IsArray()
    @IsOptional()
    inherited: ResponsePermission[] = new Array<ResponsePermission>();

    /**
     * Creates a ResponseUserPermissions object from a group.
     * @param group The group the response shall be build for.
     */
    public constructor(group: UserGroup) {
        for (let permission of group.permissions) {
            this.directlyGranted.push(permission.toResponse());
        }
    }
}
