import CRUDBase from '../CRUDBase.js'

class PermissionResource extends CRUDBase {
    constructor() {
        super()

        this.endpoint = '/api/v1/iam/permission'
        this.required_fields = ['target_type', 'permission']
        this.permission_base = 'v1:iam:permission'

        this.item = 'IAM Permission'
        this.plural = 'IAM Permissions'

        this.listing_definition = {
            display: `Permissions are custom actions that can be performed on a given IAM target by the subject.`,
            columns: [
                {
                    name: 'Target Type',
                    field: 'target_type',
                    renderer: type => type.split('_').map(x => `${x.charAt(0).toUpperCase()}${x.slice(1)}`).join(' '),
                },
                {
                    name: 'Permission',
                    field: 'permission',
                },
            ],
            actions: [
                {
                    type: 'resource',
                    position: 'main',
                    action: 'insert',
                    text: 'Create New',
                    color: 'success',
                },
                {
                    type: 'resource',
                    position: 'row',
                    action: 'update',
                    icon: 'fa fa-edit',
                    color: 'primary',
                },
                {
                    type: 'resource',
                    position: 'row',
                    action: 'delete',
                    icon: 'fa fa-times',
                    color: 'danger',
                    confirm: true,
                },
            ],
        }

        this.form_definition = {
            fields: [
                {
                    name: 'Target Type',
                    field: 'target_type',
                    required: true,
                    type: 'select',
                    options: [
                        {display: 'Application', value: 'application'},
                        {display: 'Api Scope', value: 'api_scope'},
                        {display: 'Machine', value: 'machine'},
                        {display: 'Machine Group', value: 'machine_group'},
                    ],
                },
                {
                    name: 'Permission',
                    field: 'permission',
                    required: true,
                    type: 'text',
                },
            ],
            /*handlers: {
                insert: {
                    action: 'back',
                },
                update: {
                    action: 'back',
                },
            },*/
        }
    }
}

const iam_permission = new PermissionResource()
export { iam_permission }
