import { test, expect, beforeEach, afterAll } from 'vitest'
import { userEvent } from 'vitest/browser'
const globalKeys=[]
for(const type of ['keydown','keyup'])document.addEventListener(type,event=>globalKeys.push({type,key:event.key,code:event.code,shift:event.shiftKey,repeat:event.repeat,trusted:event.isTrusted}))
let prior=[]
beforeEach(()=>{prior=globalKeys.splice(0);document.body.innerHTML=''})
afterAll(async()=>{await userEvent.cleanup()})
const input=()=>{const el=document.createElement('input');el.setAttribute('aria-label','entry');document.body.append(el);el.focus();return el}
const trace=el=>{const events=[];for(const type of ['keydown','keypress','beforeinput','input','keyup'])el.addEventListener(type,event=>events.push({type,key:event.key??null,code:event.code??null,shift:event.shiftKey??null,ctrl:event.ctrlKey??null,meta:event.metaKey??null,repeat:event.repeat??null,inputType:event.inputType??null,data:event.data??null,trusted:event.isTrusted,value:el.value,selection:[el.selectionStart,el.selectionEnd]}));return events}
const record=(name,el,events,extra={})=>console.log('HELD_RAW '+JSON.stringify({name,value:el.value,selection:[el.selectionStart,el.selectionEnd],events,...extra}))
test('held Shift preserves literal text and native modifier events across commands',async()=>{const el=input(),events=trace(el);await userEvent.keyboard('{Shift>}');await userEvent.keyboard('ab');await userEvent.keyboard('{/Shift}c');record('held Shift events',el,events);expect(el.value).toBe('abc');expect(events.filter(e=>e.type==='keydown').map(e=>[e.key,e.shift])).toEqual([['Shift',true],['a',true],['b',true],['c',false]])})
test('keyboard repeat descriptors retain native repeat events and explicit release',async()=>{const el=input(),events=trace(el);await userEvent.type(el,'abcd');await userEvent.keyboard('{ArrowLeft>2/}{Backspace>2/}');record('native repeated keys',el,events);expect(el.value).toBe('cd')})
test('type auto-close releases native Shift but retains its cleanup bookkeeping',async()=>{const el=input(),events=trace(el);await userEvent.keyboard('{Shift>}');await userEvent.type(el,'ab');await userEvent.keyboard('c');await userEvent.cleanup();record('type native auto-close',el,events);expect(el.value).toBe('abc');expect(events.filter(e=>e.type==='keydown'&&e.key!=='Shift').map(e=>e.shift)).toEqual([true,true,false]);expect(events.filter(e=>e.type==='keyup'&&e.key==='Shift')).toHaveLength(2)})
test('skipAutoClose preserves native modifier events until explicit release',async()=>{const el=input(),events=trace(el);await userEvent.type(el,'{Shift>}ab',{skipAutoClose:true});await userEvent.keyboard('c{/Shift}');record('type skipAutoClose events',el,events);expect(el.value).toBe('abc');expect(events.filter(e=>e.type==='keydown').map(e=>e.shift)).toEqual([true,true,true,true])})
test('setup instances keep independent cleanup state on the shared native keyboard',async()=>{const el=input(),events=trace(el),first=userEvent.setup(),second=userEvent.setup();await first.keyboard('{Shift>}');await second.keyboard('a');await second.cleanup();await first.cleanup();await second.type(el,'b');record('setup shared native keyboard',el,events);expect(el.value).toBe('ab');expect(events.filter(e=>e.type==='keydown'&&e.key!=='Shift').map(e=>e.shift)).toEqual([true,false])})
test('held modifier state is released when the authored case closes',async()=>{const el=input(),events=trace(el);await userEvent.keyboard('{Shift>}');record('attempt cleanup hold',el,events);expect(el.value).toBe('')})
test('following case receives clean native keyboard state',async()=>{const el=input(),events=trace(el);await userEvent.keyboard('a');record('attempt cleanup following',el,events,{prior});expect(el.value).toBe('a')})
let retainedSetup
test('setup-owned modifier persists after its authored case without default cleanup ownership',async()=>{const el=input(),events=trace(el);retainedSetup=userEvent.setup();await retainedSetup.keyboard('{Shift>}');record('setup case holds modifier',el,events);expect(events.at(-1).shift).toBe(true)})
test('following default userEvent sees setup-owned native modifier until owner cleanup',async()=>{const el=input(),events=trace(el);await userEvent.keyboard('a');await retainedSetup.cleanup();await userEvent.keyboard('b');record('setup following explicit cleanup',el,events,{prior});expect(el.value).toBe('ab');expect(events.filter(e=>e.type==='keydown').map(e=>e.shift)).toEqual([true,false])})
test('held repeated editing key continues native repeat state across commands',async()=>{const el=input(),events=trace(el);await userEvent.type(el,'abcd');await userEvent.keyboard('{ArrowLeft>2}');await userEvent.keyboard('{ArrowLeft>2/}');await userEvent.keyboard('{Delete}');record('held editing repeat commands',el,events);expect(el.value).toBe('bcd');expect(events.filter(e=>e.type==='keydown'&&e.key==='ArrowLeft').map(e=>e.repeat)).toEqual([false,true,false,true])})
