import { Injector } from "./Injector";
import { Type, GenericClassDecorator } from "./Util";
import { FrontworkComponent } from "../Types/FrontworkComponent";
import { RPCExporter } from "rpclibrary";

/**
 * @returns {GenericClassDecorator<Type<any>>}
 * @constructor
 */
export const Injectable = (_interface?: Type<any>) : GenericClassDecorator<Type<any>> => {
  return (target: Type<any>) => {
    Injector.modules.push({
      implements: _interface,
      implementation: target
    })
  }
}

/**
 * @returns {GenericClassDecorator<Type<any>>}
 * @constructor
 */
export const RootComponent = (config : {
    injectable : Type<any>
    injects : Type<RPCExporter>[]
  }) : GenericClassDecorator<Type<any>> => {
  return (target: Type<any>) => {
    Injector.rootModules = config.injects
    Injector.rootInterface = config.injectable
    Injector.root = target
  }
}

export const Inject = (type: any) => {
  return function (_this, key) {
    Injector.injectionQueue.push({what: type, target: _this, where:key})
  }
}