var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { LitElement, html, nothing } from 'lit';
import { property } from 'lit/decorators.js';
import { btuRipple } from '../../effects/ripple.js';
import { ClipboardMixin } from '../../util/ClipboardMixin.js';
import { ComponentStatesMixin } from '../../util/ComponentStatesMixin.js';
import { EventEmitterMixin } from '../../util/EventEmitterMixin.js';
import { ReadyMixin } from '../../util/ReadyMixin.js';
/**
 * A button component that copies text to the system clipboard.
 *
 * Provides visual feedback when text is successfully copied by applying
 * a temporary state that can be styled with CSS.
 *
 * Button styling is inherited from `btu-button-*` classes. Override appearance
 * by passing custom classes via the `buttonClasses` attribute.
 *
 * @element btu-copy-to-clipboard
 *
 * @fires {CustomEvent} btu-copy-to-clipboard-ready - Fired after first render and initialization
 * @fires {CustomEvent} btu-copy-to-clipboard-copied - Fired when text is successfully copied to clipboard
 *
 * @cssstate btu-copy-to-clipboard-copied - Applied briefly after a successful copy, targetable via :state(btu-copy-to-clipboard-copied)
 *
 * @example
 * ```html
 * <!-- Basic usage -->
 * <btu-copy-to-clipboard copydata="https://example.com/cms/content/edit">
 *   Copy Link
 * </btu-copy-to-clipboard>
 * ```
 */
export default class CopyToClipboard extends ClipboardMixin(ComponentStatesMixin(EventEmitterMixin(ReadyMixin(LitElement)))) {
    connectedCallback() {
        this.label = this.textContent?.trim();
        this.textContent = '';
        // Capture and remove aria-label from host
        const hostAriaLabel = this.getAttribute('aria-label');
        if (hostAriaLabel) {
            this.ariaLabelOverride = hostAriaLabel;
            this.removeAttribute('aria-label');
        }
        super.connectedCallback();
    }
    firstUpdated() {
        this.emit('btu-copy-to-clipboard-ready');
    }
    /**
     * Copies the copydata to the system clipboard.
     * Displays a success state for 3 seconds after copying.
     */
    async copy() {
        if (!this.copydata)
            return;
        await super.copy(this.copydata);
        if (this.states.has(this.copiedStateKey)) {
            btuRipple({ position: this.querySelector('button'), sparkle: true });
        }
    }
    createRenderRoot() {
        return this;
    }
    render() {
        const isCopied = this.states.has(this.copiedStateKey);
        const iconClass = isCopied ? 'before:btu-icon-clipboard-check' : 'before:btu-icon-clipboard-copy';
        const classes = this.buttonClasses ?? `btu-button btu-button-gray before:btu-icon ${iconClass}`;
        return html `<button
      type="button"
      class="${classes}"
      aria-label="${this.ariaLabelOverride || this.label || nothing}"
      @click=${this.copy}
      ?disabled=${isCopied || !this.copydata}
    >
      ${this.label}
    </button>`;
    }
}
__decorate([
    property({ type: String })
], CopyToClipboard.prototype, "copydata", void 0);
__decorate([
    property({ type: String })
], CopyToClipboard.prototype, "buttonClasses", void 0);
// Register custom element with guard to prevent double registration
if (!customElements.get('btu-copy-to-clipboard')) {
    customElements.define('btu-copy-to-clipboard', CopyToClipboard);
}
//# sourceMappingURL=CopyToClipboard.js.map