import { LitElement } from 'lit';
import { announce } from '../util/aria.js';
/**
 * Mixin that adds clipboard copy functionality with visual feedback.
 *
 * Provides a `copy()` method that:
 * - Writes text to the system clipboard
 * - Adds a temporary `<consumer-tag>-copied` state for 3 seconds
 * - Emits a `<consumer-tag>-copied` event
 * - Prevents duplicate copy operations while in success state
 *
 * The state and event name are resolved from the host element's tagName
 * at runtime (per LESSONS-NAMING §3) so the mixin stays reusable across
 * different consumers without a hardcoded `btu-clipboard-*` prefix.
 *
 * @mixin
 *
 * @requires ComponentStatesMixin - For state management
 * @requires EventEmitterMixin - For event emission
 *
 * @example
 * ```typescript
 * class MyComponent extends ClipboardMixin(
 *   ComponentStatesMixin(
 *     EventEmitterMixin(
 *       ReadyMixin(LitElement)
 *     )
 *   )
 * ) {
 *   async handleCopy() {
 *     await this.copy('Hello, World!')
 *   }
 * }
 * ```
 */
export const ClipboardMixin = (superClass) => {
    class ClipboardMixinClass extends superClass {
        /** Custom state applied briefly after a successful copy; also emitted as an event. */
        get copiedStateKey() {
            return `${this.tagName.toLowerCase()}-copied`;
        }
        /**
         * Copies text to the system clipboard with visual feedback.
         *
         * @param text - The text to copy to clipboard
         * @param timeout - Duration in milliseconds to show success state (default: 3000)
         * @returns Promise that resolves when copy operation is complete
         */
        async copy(text, timeout = 3000) {
            if (!text)
                return;
            const copiedKey = this.copiedStateKey;
            if (this.states.has(copiedKey))
                return;
            try {
                if (!navigator.clipboard || typeof navigator.clipboard.writeText !== 'function') {
                    throw new Error('Clipboard API is not available in this environment.');
                }
                await navigator.clipboard.writeText(text);
            }
            catch (error) {
                // eslint-disable-next-line no-console
                console.error('Failed to copy text to clipboard:', error);
                announce('Copy failed');
                return;
            }
            this.states.add(copiedKey);
            this.requestUpdate();
            this.emit(copiedKey);
            announce('Copied!');
            setTimeout(() => {
                this.states.delete(copiedKey);
                this.requestUpdate();
            }, timeout);
        }
    }
    return ClipboardMixinClass;
};
//# sourceMappingURL=ClipboardMixin.js.map