import { W3cEventNameToLynx } from '../../../constants.js';
function toCloneableObject(obj) {
    const cloneableObj = {};
    for (const key in obj) {
        const value = obj[key];
        if (typeof value === 'boolean' || typeof value === 'number'
            || typeof value === 'string' || value === null) {
            cloneableObj[key] = value;
        }
    }
    return cloneableObj;
}
export function createCrossThreadEvent(domEvent) {
    const type = domEvent.type;
    const params = {};
    const isTrusted = domEvent.isTrusted;
    const otherProperties = {};
    let detail = domEvent.detail ?? {};
    if (type.match(/^transition/)) {
        Object.assign(params, {
            'animation_type': 'keyframe-animation',
            'animation_name': domEvent.propertyName,
            new_animator: true, // we support the new_animator only
        });
    }
    else if (type.match(/animation/)) {
        Object.assign(params, {
            'animation_type': 'keyframe-animation',
            'animation_name': domEvent.animationName,
            new_animator: true, // we support the new_animator only
        });
    }
    else if (type.startsWith('touch')) {
        const touchEvent = domEvent;
        const touch = [...touchEvent.touches];
        const targetTouches = [...touchEvent.targetTouches];
        const changedTouches = [...touchEvent.changedTouches];
        Object.assign(otherProperties, {
            touches: isTrusted ? touch.map(toCloneableObject) : touch,
            targetTouches: isTrusted
                ? targetTouches.map(toCloneableObject)
                : targetTouches,
            changedTouches: isTrusted
                ? changedTouches.map(toCloneableObject)
                : changedTouches,
        });
        if (touch[0]) {
            detail = {
                x: touch[0].clientX,
                y: touch[0].clientY,
            };
        }
    }
    else if (type.startsWith('mouse')) {
        const mouseEvent = domEvent;
        Object.assign(otherProperties, {
            button: mouseEvent.button,
            buttons: mouseEvent.buttons,
            x: mouseEvent.x,
            y: mouseEvent.y,
            pageX: mouseEvent.pageX,
            pageY: mouseEvent.pageY,
            clientX: mouseEvent.clientX,
            clientY: mouseEvent.clientY,
        });
    }
    else if (type === 'click') {
        detail = {
            x: domEvent.x,
            y: domEvent.y,
        };
    }
    const lynxEventName = W3cEventNameToLynx[type] ?? type;
    return {
        type: lynxEventName,
        timestamp: domEvent.timeStamp,
        // @ts-expect-error
        detail,
        params,
        ...otherProperties,
    };
}
//# sourceMappingURL=createCrossThreadEvent.js.map