import { Set as SetSingle, Card as CardSingle, SetResume } from '../../../meta/definitions/api' import { getSets, isSetAvailable, setToSetSimple, setToSetSingle } from '../utils/setUtil' import { Languages, Set } from '../../../interfaces' import { Endpoint } from '../compilerInterfaces' import { cardToCardSingle, getCards } from '../utils/cardUtil' type SetList = Array export default class implements Endpoint> { public constructor( private lang: keyof Languages ) {} public async index(common: Array): Promise { const sets = common .sort((a, b) => a.releaseDate > b.releaseDate ? 1 : -1) const tmp: SetList = await Promise.all(sets.map((el) => setToSetSimple(el, this.lang))) return tmp } public async item(common: Array): Promise> { const sets = await Promise.all(common .map((set) => setToSetSingle(set, this.lang))) const res: Record = {} for (const set of sets) { res[set.name] = set res[set.id] = set } return res } public async common(): Promise> { return getSets(undefined, this.lang) } public async sub(common: Array, item: string): Promise | undefined> { const set = common.find((s) => s.name[this.lang] === item || s.id === item) if (!set || !isSetAvailable(set, this.lang)) { return undefined } const lit = await getCards(this.lang, set) const list: Record = {} for await (const card of lit) { list[card[0]] = await cardToCardSingle(card[0], card[1], this.lang) } return list } }