import { shallowRef } from 'vue' import type { ConfirmationOptions } from '../shared/types' export const confirmation=shallowRef(null) let resolvePending:((confirmed:boolean)=>void)|null=null let previousFocus:HTMLElement|null=null export function askConfirmation(options:ConfirmationOptions):Promise{ if(resolvePending)return Promise.resolve(false) previousFocus=document.activeElement instanceof HTMLElement?document.activeElement:null confirmation.value=options return new Promise(resolve=>{resolvePending=resolve}) } export function answerConfirmation(confirmed:boolean):void{ const resolve=resolvePending resolvePending=null;confirmation.value=null resolve?.(confirmed) } export function restoreConfirmationFocus(event:Event):void{ event.preventDefault() if(previousFocus?.isConnected&&previousFocus!==document.body&&!previousFocus.matches(':disabled'))previousFocus.focus({preventScroll:true}) else document.querySelector('.nav-item.active')?.focus({preventScroll:true}) previousFocus=null }