import {
  PropertyKeyType,
} from '@loafer/core';

import {
  TypeUtil,
} from '@loafer/core/util/TypeUtil';


import {
  DecoratorType,
  DecoratorParametersType,
} from './type';

export class DecoratorUtil {
  /**
   *
   * @param {any[]} args
   * @returns {DecoratorType}
   */
  public static getDecoratorType(args: any[]): DecoratorType {
    const [, propertyKey, descriptor] = args;

    if (typeof descriptor === 'number') {
        return DecoratorType.PARAMETER;
    }

    if (propertyKey && descriptor === undefined || descriptor && (descriptor.get || descriptor.set)) {
        return DecoratorType.PROPERTY;
    }
    return (descriptor && descriptor.value) ? DecoratorType.METHOD : DecoratorType.CLASS;
  }

  /**
   *
   * @param target
   * @param {string} propertyKey
   * @returns {DecoratorParametersType}
   */
  public static decoratorArgs(target: any, propertyKey: PropertyKeyType): DecoratorParametersType {
    return [
        target,
        propertyKey,
        TypeUtil.descriptorOf(target, propertyKey),
    ];
  }

}
