import { injectable } from "inversify" ; import type { Card } from "./Card" ; import { intersection } from "lodash" ; import { countMapBy } from "lightdash" ; import type { CardTypeCategory } from "./type/CardTypeCategory" ; import type { CardType } from "./type/CardType" ; export type Counted < T > = Map < T , number > ; @ injectable ( ) export class CardService { param card return getAllNames ( card ) { const names = [ card . name ] ; if ( card . treatedAs != null ) { names . push ( card . treatedAs ) ; } if ( card . betaName != null ) { names . push ( card . betaName ) ; } return names ; } param cardA param cardB return isTreatedAsSame ( cardA , cardB ) { return ( intersection ( this . getAllNames ( cardA ) , this . getAllNames ( cardB ) ) . length > 0 ) ; } param cards return countByCard ( cards ) { return countMapBy ( cards , ( card ) => card ) ; } param cards return countByType ( cards ) { return countMapBy ( cards , ( card ) => card . type ) ; } param cards return countByTypeCategory ( cards ) { return countMapBy ( cards , ( card ) => card . type . category ) ; } param cards return createFormattedCardCountList ( cards ) { return Array . from ( this . countByCard ( cards ) . entries ( ) ) . filter ( ( [ , count ] ) => count > 0 ) . map ( ( [ card , count ] ) => `${ count }x ${ card . name }` ) ; } param card return getReferenceLink ( card ) { const url = new URL ( "https://db.ygoprodeck.com/card/" ) ; url . searchParams . append ( "search" , card . name ) ; return url ; } }