// jsdom and Node's experimental global `localStorage` don't cooperate under
// vitest: Node's is gated behind `--localstorage-file` and shadows jsdom's, so
// the bare `localStorage` the widget's storage.ts relies on is `undefined`.
// Install a browser-equivalent in-memory Storage so app code and tests behave
// like a real browser.
class MemoryStorage {
	private store = new Map<string, string>();
	get length(): number {
		return this.store.size;
	}
	clear(): void {
		this.store.clear();
	}
	getItem( key: string ): string | null {
		return this.store.has( key ) ? this.store.get( key )! : null;
	}
	key( index: number ): string | null {
		return Array.from( this.store.keys() )[ index ] ?? null;
	}
	removeItem( key: string ): void {
		this.store.delete( key );
	}
	setItem( key: string, value: string ): void {
		this.store.set( key, String( value ) );
	}
}

const storage = new MemoryStorage();
Object.defineProperty( globalThis, 'localStorage', { value: storage, configurable: true, writable: true } );
if ( typeof window !== 'undefined' ) {
	Object.defineProperty( window, 'localStorage', { value: storage, configurable: true, writable: true } );
}
