import type { Position } from "./types.ts";

export class PointerLocationRequest extends Event {
    static readonly eventName = "fridge-pointer-location";
    position!: Position;
    constructor() {
        super(PointerLocationRequest.eventName, { bubbles: true, composed: true });
    }
}

export function pointerLocation(self?: HTMLElement): Position {
    const node = self ?? window;
    const ev = new PointerLocationRequest();
    node.dispatchEvent(ev);
    return ev.position;
}

declare global {
    interface GlobalEventHandlersEventMap {
        [PointerLocationRequest.eventName]: PointerLocationRequest;
    }
}
