import { promiseTimeout } from "./promise-utils.js";
function buildCustomEvent(name, detail) {
    return new CustomEvent(name, {
        detail,
        bubbles: true,
        cancelable: true,
        composed: true
    });
}
function listenOnce(element, type, listener, options) {
    const _listener = (evt)=>{
        element.removeEventListener(type, _listener, options);
        'object' == typeof listener ? listener.handleEvent.call(element, evt) : listener.call(element, evt);
    };
    element.addEventListener(type, _listener, options);
}
function eventPromise(element, type, timeoutMs = 0) {
    let promise = new Promise((resolve)=>{
        listenOnce(element, type, (e)=>{
            resolve(e);
        });
    });
    if (timeoutMs > 0) promise = promiseTimeout(promise, timeoutMs);
    return promise;
}
export { buildCustomEvent, eventPromise, listenOnce };
