import { useDriveUIStore } from "@/lib/stores/drive-ui-store"

/** Elements that must not bubble open/navigate on the file card underneath. */
export const DRIVE_MENU_INTERACTIVE_SELECTOR =
  '[data-drive-menu-btn],[data-drive-menu-surface],[data-slot="dropdown-menu-content"],[data-slot="dropdown-menu-item"],[data-slot="context-menu-content"],[data-slot="context-menu-item"]'

export function isFromDriveMenu(target: EventTarget | null): boolean {
  return target instanceof Element && target.closest(DRIVE_MENU_INTERACTIVE_SELECTOR) != null
}

export function isCardOpenSuppressed(): boolean {
  return Date.now() < useDriveUIStore.getState().suppressCardOpenUntil
}

/** Block grid/list card open after menu use (Radix ghost click). */
export function guardDriveMenuPointer(ms = 700) {
  useDriveUIStore.getState().blockCardOpen(ms)
}

/** Stop card/grid handlers only — safe on menu trigger button. */
export function stopDriveMenuBubble(e: { stopPropagation: () => void }) {
  e.stopPropagation()
}

/** Menu item selection — also blocks ghost card open. */
export function stopDriveMenuEvent(e: { stopPropagation: () => void }) {
  e.stopPropagation()
  guardDriveMenuPointer()
}
