

const pointList= ['lt', 't', 'rt', 'r', 'rb', 'b', 'lb', 'l'] // 八个方向
const initialAngle= { // 每个点对应的初始角度
    lt: 0,
    t: 45,
    rt: 90,
    r: 135,
    rb: 180,
    b: 225,
    lb: 270,
    l: 315,
}
const angleToCursor=[ // 每个范围的角度对应的光标
    { start: 338, end: 23, cursor: 'nwse' },
    { start: 23, end: 68, cursor: 'ns' },
    { start: 68, end: 113, cursor: 'nesw' },
    { start: 113, end: 158, cursor: 'ew' },
    { start: 158, end: 203, cursor: 'nwse' },
    { start: 203, end: 248, cursor: 'ns' },
    { start: 248, end: 293, cursor: 'nesw' },
    { start: 293, end: 338, cursor: 'ew' },
]
/**
 * 旋转后 获取对应的鼠标手势
 * @param {*} rotate 
 * @returns 
 */
function getCursor(rotate) {
  const result = {}
  // 从上一个命中的角度的索引开始匹配下一个，降低时间复杂度
  let lastMatchIndex = -1 
  let bool=true
  pointList.forEach(point => {
      const angle = (initialAngle[point] + rotate) % 360
      const len = angleToCursor.length
      while (bool) {
          lastMatchIndex = (lastMatchIndex + 1) % len
          const angleLimit = angleToCursor[lastMatchIndex]
          if (angle < 23 || angle >= 338) {
              result[point] = 'nwse-resize'
              return 
          }
          if (angleLimit.start <= angle && angle < angleLimit.end) {
              result[point] = angleLimit.cursor + '-resize'
              return 
          }
      }
  })
  return result
}
 /**
  *  获取一个组件旋转 rotate 后的样式
  * @param {*} item 
  * @returns 
  */
 function getComponentRotatedStyle(item) {
  item = { ...item }
  if (item.angle != 0) {
      const newWidth = item.w * cos(item.angle) + item.h * sin(item.angle)
      const diffX = (item.w - newWidth) / 2 // 旋转后范围变小是正值，变大是负值
      item.x += diffX
      item.right = item.x + newWidth

      const newHeight = item.h * cos(item.angle) + item.w * sin(item.angle)
      const diffY = (newHeight - item.h) / 2 // 始终是正
      item.y -= diffY
      item.bottom = item.y + newHeight

      item.w = newWidth
      item.h = newHeight
  } else {
      item.bottom = item.y + item.h
      item.right = item.x + item.w
  }

  return item
}
 function sin(rotate) {
  return Math.abs(Math.sin(angleToRadian(rotate)))
}
 function cos(rotate) {
  return Math.abs(Math.cos(angleToRadian(rotate)))
}
function angleToRadian(angle) {
  return angle * Math.PI / 360
}


export {
  getComponentRotatedStyle, getCursor
}

export default {
  getCursor,
  getComponentRotatedStyle
}