/**
 * @public
 */
export function Listen(eventName, target = 'self', options) {
    return (prototype, methodName) => {
        const originalConnectedCallback = prototype.connectedCallback;
        const originalDisconnectedCallback = prototype.disconnectedCallback;
        prototype.connectedCallback = function () {
            const actualTarget = target === 'self'
                ? this
                : target === 'window'
                    ? window
                    : document;
            const handler = (event) => {
                if (options?.preventDefault) {
                    event.preventDefault();
                }
                this[methodName](event);
            };
            actualTarget.addEventListener(eventName, handler, options);
            if (originalConnectedCallback) {
                originalConnectedCallback.call(this);
            }
            // Store the handler to remove it later
            this[`__${methodName}_handler`] = handler;
        };
        prototype.disconnectedCallback = function () {
            const actualTarget = target === 'self' ? this : target === 'window' ? window : document;
            actualTarget.removeEventListener(eventName, this[`__${methodName}_handler`]);
            if (originalDisconnectedCallback) {
                originalDisconnectedCallback.call(this);
            }
        };
    };
}
//# sourceMappingURL=ListenDecorator.js.map