import {OrderDirection, QueryRow} from '@extollo/lib'

export enum FieldType {
    text = 'text',
    textarea = 'textarea',
    html = 'html',
    email = 'email',
    number = 'number',
    integer = 'integer',
    date = 'date',
    select = 'select',
    bool = 'bool',
}

enum ResourceAction {
    create = 'create',
    read = 'read',
    readOne = 'readOne',
    update = 'update',
    delete = 'delete',
}

export enum Renderer {
    text = 'text',
    html = 'html',
    bool = 'bool',
    date = 'date',
    time = 'time',
    datetime = 'datetime',
}

const allResourceActions = [
    ResourceAction.create, ResourceAction.read, ResourceAction.readOne,
    ResourceAction.update, ResourceAction.delete,
]

export type FieldBase = {
    key: string,
    display: string,
    type: FieldType,
    required: boolean,
    queryable?: boolean,
    sort?: 'asc' | 'desc',
    renderer?: Renderer,
    readonly?: boolean,
    helpText?: string,
    placeholder?: string,
    hideOn?: {
        form?: boolean,
        listing?: boolean,
    },
}

export type SelectOptions = {display: string, value: any}[]

export type FieldDefinition = FieldBase
    | FieldBase & { type: FieldType.select, options: SelectOptions }

export interface ResourceConfiguration {
    key: string,
    collection: string,
    primaryKey: string,
    orderField?: string,
    orderDirection?: OrderDirection,
    generateKeyOnInsert?: () => string|number,
    process?: {
        afterRead?: (row: QueryRow) => QueryRow,
    },
    display: {
        field?: string,
        singular: string,
        plural: string,
    },
    supportedActions: ResourceAction[],
    fields: FieldDefinition[],
}

export { ResourceAction, allResourceActions }
