// #region Imports
import { HTMLElementEventEmitter, PureEventEmitter } from '@breadstone/mosaik-elements';
import { isServer } from 'lit';
// #endregion
/**
 * @public
 */
export function Emit(options) {
    return (target, propertyKey, originalDescriptor) => {
        let propertyName = options?.eventName ?? (propertyKey.toString() ?? target.key);
        // If is not a explicit eventName
        if (!options?.eventName) {
            // Remove unnecessary characters like $, _, etc
            propertyName = propertyName.replace(/[^a-zA-Z0-9]/g, '');
        }
        const eventFieldName = `__$${propertyName}Event$`;
        const descriptor = {
            get() {
                if (!this[eventFieldName]) {
                    this[eventFieldName] = !isServer && this instanceof HTMLElement
                        ? new HTMLElementEventEmitter(this, propertyName, options)
                        : new PureEventEmitter();
                }
                return this[eventFieldName];
            },
            enumerable: true,
            configurable: true
        };
        if (propertyKey !== undefined) {
            // Legacy TS decorator
            return Object.defineProperty(target, propertyKey.toString(), descriptor);
        }
        // TC39 Decorators proposal
        return {
            kind: 'method',
            placement: 'prototype',
            key: target.key,
            descriptor
        };
    };
}
//# sourceMappingURL=EmitDecorator.js.map