import { onBeforeUnmount, onMounted } from 'vue'
import type { KbxShortcutDefinition, KbxShortcutScope } from '@kbx/contracts'
import { registerKbxShortcuts } from '../keyboard/manager'

export interface KbxPageShortcut {
  key:string
  enabled?:()=>boolean
  execute:()=>void|Promise<void>
}

export function useKbxShortcuts(shortcuts:KbxShortcutDefinition[]){
  let unregister:(()=>void)|undefined
  onMounted(()=>{unregister=registerKbxShortcuts(shortcuts)})
  onBeforeUnmount(()=>unregister?.())
}

export function useKbxPageShortcuts(shortcuts:KbxPageShortcut[]){
  useKbxShortcuts(shortcuts.map(x=>({...x,scope:'page' as KbxShortcutScope})))
}
