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

export function schemaClass(schema: ISchemaClass, namespace?: string): ISchemaClassDecorator {

  // if this is decorating a class, typeof args[0] is the type of the decorated class
  return function schemaClassFactory(...args: Array<any>): typeof args[0] {

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

function classSchemaClass(target: any, schema: ISchemaClass, namespace?: string): typeof target {
  const type: string = target.prototype.constructor.name;

  MetadataProvider.setForType(MetadataType.SchemaClass, schema, namespace, type);

  return target;
}
