import { getEnabledElement } from '@cornerstonejs/core'
import * as cornerstoneTools from '@cornerstonejs/tools'


class MIPJumpToClickTool extends cornerstoneTools.MIPJumpToClickTool {
  static toolName = 'MIPJumpToClickTool'

  constructor(toolProps = {}, defaultToolProps) {
    const mergedDefaultToolProps = defaultToolProps || {
      supportedInteractionTypes: ['Mouse', 'Touch'],
      configuration: {
        targetViewportIds: [],
        sourceViewportIds: [],
      },
    }

    super(toolProps, mergedDefaultToolProps)
  }

  mouseClickCallback(evt) {
    const { element, viewportId } = evt.detail || {}
    const enabledElement = element ? getEnabledElement(element) : null
    const sourceViewportId = viewportId || enabledElement?.viewport?.id || ''
    const sourceViewportIds = this.configuration?.sourceViewportIds
    const hasSourceLimit = Array.isArray(sourceViewportIds) && sourceViewportIds.length > 0
    if (hasSourceLimit && !sourceViewportIds.includes(sourceViewportId)) {
      return
    }

    return super.mouseClickCallback(evt)
  }
}

export default MIPJumpToClickTool
