import { getMetadataStorage } from "../utils/metadata-storage";
import { FieldType, FieldTypes } from "../types/field";
import { OdooFieldMetadata } from "../metadata/field-metadata";

/**
 *
 */
export function OdooField(): Function;

/**
 *
 */
export function OdooField(fieldName: string): Function;

/**
 *
 */
export function OdooField(type: FieldType): Function;

/**
 *
 */
export function OdooField(fieldName: string, type: FieldType);

/**
 *
 */
export function OdooField(fieldName?: string, type?: FieldType): Function {
    return function (object: Object, propertyName: string) {
        if (!name) {
            fieldName = propertyName;
        }

        if (!type) {
            const typeMetadata = Reflect && (Reflect as any).getMetadata ? (Reflect as any).getMetadata("design:type", object, propertyName) : undefined;
            if (typeMetadata) {
                type = FieldTypes.determineTypeFromFunction(typeMetadata);
            }
        }

        const args: OdooFieldMetadata = {
            target: object.constructor,
            fieldName: fieldName
        }

        getMetadataStorage().fields.add(args);
    }
}