import 'reflect-metadata';
import typeMetadataStorage from '../storage/type-metadata.storage';

export type IndexOptions = {
  type: 'key' | 'fulltext' | 'unique';
  orders: 'ASC' | 'DESC' | ('ASC' | 'DESC')[];
  attributes?: string[];
};

export function Index(options: IndexOptions): PropertyDecorator {
  return (target, propertyKey) => {
    const propKey = propertyKey.toString();

    const optionsCopy = {
      ...options,
      orders: Array.isArray(options.orders) ? options.orders : [options.orders],
      attributes: options.attributes ?? [propKey],
    };
    typeMetadataStorage.addIndexMetadata(target.constructor, propKey, optionsCopy);
  };
}
