import * as cornerstoneTools from 'cornerstone-tools'

import renderToolData from './renderToolData'
// import getPixelSpacing from './getPixelSpacing'
import calculateLongestAndShortestDiameters from './calculateLongestAndShortestDiameters'
const throttle = cornerstoneTools.import('util/throttle')
import createNewMeasurement from './createNewMeasurement'
/**
 * @public
 * @class BidirectionalTool
 * @memberof Tools.Annotation
 * @classdesc Create and position an annotation that measures the
 * length and width of a region.
 * @extends Tools.Base.BaseAnnotationTool
 */

export default class BidirectionalTool extends cornerstoneTools.BidirectionalTool {
  constructor(props = {}) {
    const defaultProps = {
      name: 'Bidirectional',
      configuration: {
        drawHandles: false
      }
    }

    super(props, defaultProps)
    this.throttledUpdateCachedStats = throttle(this.updateCachedStats, 50)
    const digits = props?.configuration?.digits
    this.digits = isNaN(parseInt(digits)) ? 2 : digits
    this.createNewMeasurement = createNewMeasurement.bind(this)
    this.renderToolData = renderToolData.bind(this)
  }

  updateCachedStats(image, element, data) {
    // Prevent updating other tools' data
    if (data.toolName !== this.name) {
      return
    }
    // const pixelSpacing = getPixelSpacing(image)
    const {
      longestDiameter,
      shortestDiameter
    } = calculateLongestAndShortestDiameters(data, image, this.digits)

    // Set measurement text to show lesion table
    data.longestDiameter = longestDiameter
    data.shortestDiameter = shortestDiameter
    data.invalidated = false
  }
}
