import {ISchemaAttribute} from '@essential-projects/core_contracts';
import {ISchemaAttributeDecorator, MetadataType} from '@essential-projects/metadata_contracts';
import {MetadataProvider} from './../provider';

export function schemaAttribute(schema: ISchemaAttribute, namespace?: string): ISchemaAttributeDecorator {

  return function schemaAttributeFactory(...args: Array<any>): void|PropertyDescriptor {

    switch (args.length) {
      case 3:
        if (typeof args[2] === 'number') {
          // this is a parameter descriptor
          // those should not support schemas
        }

        return methodSchemaAttribute.apply(this, [args[0], args[1], args[2], schema, namespace]);
      default:
        throw new Error('Decorators are not valid here!');
    }
  };
}

export function methodSchemaAttribute(target: any, key: string, descriptor: any, schema: ISchemaAttribute, namespace?: string): PropertyDescriptor {
  const type: string = target.constructor.name;
  MetadataProvider.setForType(MetadataType.SchemaAttribute, schema, namespace, type, key);

  return descriptor;
}
