import type { EnvironmentConfig } from "@yugioh-deck-tool/core" ; import { Environment , getLogger , TYPES } from "@yugioh-deck-tool/core" ; import type { YgoprodeckService } from "@yugioh-deck-tool/ygoprodeck" ; import { YGOPRODECK_TYPES } from "@yugioh-deck-tool/ygoprodeck" ; import type { Instance } from "tippy.js" ; import { delegate } from "tippy.js" ; import type { TooltipInstance } from "../api" ; import type { TooltipController } from "../controller/TooltipController" ; import { tooltipContainer } from "../inversify.config" ; import { TOOLTIP_TYPES } from "../types" ; import { bindReferenceLink } from "./bindReferenceLink" ; import { createErrorTooltip , createLoadingTooltip , createTooltipElement , } from "./createTooltipElement" ; const tooltipController = tooltipContainer . get < TooltipController > ( TOOLTIP_TYPES . TooltipController ) ; const environmentConfig = tooltipContainer . get < EnvironmentConfig > ( TYPES . EnvironmentConfig ) ; const ygoprodeckService = tooltipContainer . get < YgoprodeckService > ( YGOPRODECK_TYPES . YgoprodeckService ) ; const logger = getLogger ( "bindTooltip" ) ; const showTooltip = ( instance , target , cardKey ) : void => { logger . trace ( `Attempting to show tooltip for '${ cardKey }'.` ) ; tooltipController . loadCard ( cardKey ) . then ( ( card ) => { logger . trace ( "Loaded card." , card ) ; instance . setContent ( createTooltipElement ( card ) ) ; if ( target instanceof HTMLAnchorElement ) { bindReferenceLink ( target , card ) ; } if ( environmentConfig . getEnvironment ( ) == Environment . YGOPRODECK ) { ygoprodeckService . increaseCardViewCount ( card ) . then ( ( ) => logger . trace ( "Updated view count." ) ) . catch ( ( err ) => logger . warn ( "Could not update view count." , err ) ) ; } } ) . catch ( ( err ) => { instance . setContent ( createErrorTooltip ( "Error while loading card." ) ) ; logger . error ( "Error while loading card." , err ) ; } ) ; } ; export const bindTooltipHandlers = ( context ) : TooltipInstance => { const delegateInstance = delegate ( context , { target : "[data-name]" , delay : [ 500 , 0 ] , placement : "auto" , maxWidth : "none" , allowHTML : true , content : ( ) => createLoadingTooltip ( ) , onShow : ( instance ) => { const target = instance . reference as HTMLElement ; const cardKey = target . dataset [ "name" ] ! ; showTooltip ( instance , target , cardKey ) ; } , } ) ; let cancelPendingEnable = false ; return { disable ( ) { delegateInstance . disable ( ) ; cancelPendingEnable = true ; } , enable : ( ) => { cancelPendingEnable = false ; setTimeout ( ( ) => { if ( ! cancelPendingEnable ) { delegateInstance . enable ( ) ; } } , 100 ) ; } , } ; } ;