export interface EnumItem {
  val: string
  txt: string
}

export type EnhancedEnum<T> = T & {
  // @ts-ignore
  [key in T[number]['val']]: T[number]['val']
} & {
  // @ts-ignore
  [key in `${T[number]['val']}Txt`]: T[number]['txt']
} & {
  // @ts-ignore
  of: (val?: string) => T[number] | undefined
  // @ts-ignore
  txt: (val?: string) => T[number]['txt'] | undefined
}

export function createEnum<T>(enums: T): EnhancedEnum<T> {
  const e = enums as EnhancedEnum<T>
  for (let enumItem of enums as EnumItem[]) {
    // @ts-ignore
    e[enumItem.val] = enumItem.val
    // @ts-ignore
    e[enumItem.val + 'Txt'] = enumItem.txt
  }
  // @ts-ignore
  e.of = (val?: string) => ((enums as EnumItem[]).find((item) => item.val === val))
  // @ts-ignore
  e.txt = (val?: string) => ((enums as EnumItem[]).find((item) => item.val === val)?.txt)
  return e
}
