export type DriveMarqueeRect = {
  left: number
  top: number
  width: number
  height: number
}

export function rectsIntersect(
  a: { left: number; top: number; right: number; bottom: number },
  b: DOMRect
) {
  return !(a.right < b.left || a.left > b.right || a.bottom < b.top || a.top > b.bottom)
}

export function pathsInMarqueeRect(
  cardRefs: Map<string, HTMLDivElement>,
  rect: DriveMarqueeRect
): string[] {
  const sel = {
    left: rect.left,
    top: rect.top,
    right: rect.left + rect.width,
    bottom: rect.top + rect.height,
  }
  const paths: string[] = []
  cardRefs.forEach((el, path) => {
    if (rectsIntersect(sel, el.getBoundingClientRect())) {
      paths.push(path)
    }
  })
  return paths
}

export function shouldIgnoreDriveMarqueeStart(target: HTMLElement): boolean {
  if (target.closest("[data-drive-card]")) return true
  if (target.closest("[data-drive-menu-btn]")) return true
  if (
    target.closest(
      'button, a, input, textarea, select, [role="menuitem"], [role="menuitemradio"], [data-drive-menu-surface], [contenteditable="true"]'
    )
  ) {
    return true
  }
  return false
}
