import { LitElement } from 'lit';
/**
 * Mixin that adds standardized event emission capabilities to LitElement components.
 *
 * Automatically emits lifecycle events when components connect/disconnect from the DOM.
 * Provides a type-safe `emit()` method for dispatching custom events with consistent
 * configuration (bubbles, composed, cancelable).
 *
 * @mixin
 *
 * @fires {CustomEvent} btu-element-connected - Fired when element connects to DOM
 * @fires {CustomEvent} btu-element-disconnected - Fired when element disconnects from DOM
 * @fires {CustomEvent<{error: Error}>} btu-element-error - Fired on connection error
 *
 * @example
 * ```typescript
 * import { EventEmitterMixin } from './EventEmitterMixin'
 *
 * class MyComponent extends EventEmitterMixin(LitElement) {
 *   handleClick() {
 *     this.emit('my-custom-event', { data: 'value' })
 *   }
 * }
 * ```
 */
export const EventEmitterMixin = (superClass) => {
    class EventEmitterMixinClass extends superClass {
        /**
         * Lifecycle callback fired when element is connected to the DOM.
         * Emits btu-element-connected or btu-element-error.
         * @internal
         */
        connectedCallback() {
            super.connectedCallback();
            Promise.resolve().then(() => this.emit('btu-element-connected', {}), error => this.emit('btu-element-error', { error }));
        }
        /**
         * Lifecycle callback fired when element is disconnected from the DOM.
         * Emits btu-element-disconnected.
         * @internal
         */
        disconnectedCallback() {
            super.disconnectedCallback();
            this.emit('btu-element-disconnected');
        }
        /**
         * Dispatches a type-safe CustomEvent with standardized configuration.
         *
         * All events are configured with:
         * - bubbles: true (event propagates up the DOM tree)
         * - cancelable: false (event cannot be cancelled)
         * - composed: true (event crosses shadow DOM boundaries)
         *
         * @param type - Event name from CustomEventMap
         * @param detail - Optional event detail payload
         * @returns {boolean} False if event is cancelable and was cancelled, true otherwise
         *
         * @example
         * ```typescript
         * this.emit('btu-widget-collapse-changed', { collapsed: true })
         * ```
         */
        emit(type, detail) {
            // Event is constructed separately so CEM's dispatchEvent scanner doesn't
            // misread the `type` parameter as a literal event name.
            const event = new CustomEvent(type, {
                bubbles: true,
                cancelable: false,
                composed: true,
                detail,
            });
            return this.dispatchEvent(event);
        }
    }
    return EventEmitterMixinClass;
};
//# sourceMappingURL=EventEmitterMixin.js.map