'use strict';

var index = require('./index-BUvWX8u2.js');
var index$1 = require('./index-DGBoDsAL.js');
var truncationTooltip = require('./truncation-tooltip-CVzAha5B.js');
require('./floating-ui.dom-BZk7Blsu.js');

const pdsCopytextCss = ":host(.pds-copytext){--copytext-color-background-hover:var(--pine-color-background-inset)}:host(.pds-copytext) pds-button{-ms-flex-align:center;align-items:center;border-radius:var(--pine-border-radius-full);border-width:var(--pine-dimension-none);display:-ms-inline-flexbox;display:inline-flex;font-family:var(--pine-font-family-heading);font-size:var(--pine-font-size-body-md);font-weight:var(--pine-font-weight-body-medium);letter-spacing:var(--pine-letter-spacing);line-height:var(--pine-line-height-body);max-width:100%}:host(.pds-copytext) pds-button::part(button){padding-block:var(--pine-dimension-2xs);padding-inline:var(--pine-dimension-xs)}:host(.pds-copytext) pds-button::part(button):hover{background-color:var(--copytext-color-background-hover);color:var(--pine-color-text-secondary-hover)}:host(.pds-copytext) pds-button span{font-weight:var(--pine-font-weight-medium);-webkit-margin-end:var(--pine-dimension-xs);margin-inline-end:var(--pine-dimension-xs);white-space:nowrap}:host(.pds-copytext) pds-button :nth-child(2){-ms-flex-negative:0;flex-shrink:0}:host(.pds-copytext):host(.pds-copytext--bordered){border-width:var(--pine-dimension-none);padding:var(--pine-dimension-none)}:host(.pds-copytext):host(.pds-copytext--bordered) pds-button{padding:var(--pine-dimension-none)}:host(.pds-copytext):host(.pds-copytext--bordered) pds-button::part(button){padding:var(--pine-dimension-none)}:host(.pds-copytext):host(.pds-copytext--bordered) pds-button::part(button):hover{background-color:transparent}:host(.pds-copytext):host(.pds-copytext--bordered) span{background:var(--pine-color-secondary);border:var(--pine-border);border-radius:var(--pine-border-radius-full);-webkit-margin-end:var(--pine-dimension-sm);margin-inline-end:var(--pine-dimension-sm);padding-block:var(--pine-dimension-xs);padding-inline:var(--pine-dimension-sm)}:host(.pds-copytext):host(.pds-copytext--bordered) :hover span{border:var(--pine-border-hover)}:host(.pds-copytext):host(.pds-copytext--full-width) pds-button,:host(.pds-copytext):host(.pds-copytext--truncated) pds-button{display:-ms-inline-flexbox;display:inline-flex;min-width:auto;width:100%}:host(.pds-copytext):host(.pds-copytext--full-width) pds-button::part(button-content),:host(.pds-copytext):host(.pds-copytext--full-width) pds-button::part(button-text),:host(.pds-copytext):host(.pds-copytext--truncated) pds-button::part(button-content),:host(.pds-copytext):host(.pds-copytext--truncated) pds-button::part(button-text){-ms-flex-negative:1;flex-shrink:1;width:100%}:host(.pds-copytext):host(.pds-copytext--full-width) pds-button span,:host(.pds-copytext):host(.pds-copytext--truncated) pds-button span{text-align:start;width:100%}:host(.pds-copytext):host(.pds-copytext--full-width) pds-button{-ms-flex-pack:justify;justify-content:space-between}:host(.pds-copytext):host(.pds-copytext--truncated) pds-button span{overflow:hidden;text-overflow:ellipsis}";

const PdsCopytext = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.pdsCopyTextClick = index.createEvent(this, "pdsCopyTextClick");
        this.truncationCleanup = null;
        /**
         * Determines whether `copytext` should have a visible border.
         * @defaultValue true
         */
        this.border = true;
        /**
         * Determines whether `copytext` should expand to the full width of its container.
         * @defaultValue false
         */
        this.fullWidth = false;
        /**
         * Determines whether the `value` should truncate and display with an ellipsis.
         * When text overflows, a tooltip showing the full value will appear on hover/focus.
         * @defaultValue false
         */
        this.truncate = false;
        this.copyToClipboard = async (value) => {
            try {
                if (typeof navigator.clipboard !== 'undefined') {
                    await navigator.clipboard.writeText(value);
                    this.pdsCopyTextClick.emit('Copied to clipboard');
                }
            }
            catch (err) {
                this.pdsCopyTextClick.emit(`Error writing text to clipboard: ${err}`);
            }
        };
        this.handleClick = () => {
            this.copyToClipboard(this.value);
        };
    }
    handleTruncateChange(newValue) {
        if (newValue) {
            this.initTruncationTooltip();
        }
        else {
            this.destroyTruncationTooltip();
        }
    }
    handleValueChange() {
        if (this.truncate) {
            this.initTruncationTooltip();
        }
    }
    componentDidLoad() {
        if (this.truncate) {
            this.initTruncationTooltip();
        }
    }
    disconnectedCallback() {
        this.destroyTruncationTooltip();
    }
    initTruncationTooltip() {
        this.destroyTruncationTooltip();
        if (this.valueSpanEl) {
            this.truncationCleanup = truncationTooltip.setupTruncationTooltip({
                hostEl: this.el,
                contentEl: this.valueSpanEl,
                getTooltipText: () => this.value || '',
            });
        }
    }
    destroyTruncationTooltip() {
        if (this.truncationCleanup) {
            this.truncationCleanup();
            this.truncationCleanup = null;
        }
    }
    classNames() {
        const classNames = ['pds-copytext'];
        if (this.border) {
            classNames.push('pds-copytext--bordered');
        }
        if (this.fullWidth) {
            classNames.push('pds-copytext--full-width');
        }
        if (this.truncate) {
            classNames.push('pds-copytext--truncated');
        }
        return classNames.join(' ');
    }
    render() {
        return (index.h(index.Host, { key: '43a3870fa4c24416e2e6e99dd7c423a5c32ac91e', class: this.classNames(), id: this.componentId }, index.h("pds-button", { key: 'f0735d3f245dd7150e88b3dd1a191c3ee86b6293', type: "button", variant: "unstyled", onClick: this.handleClick }, index.h("span", { key: 'ddf7cf205f9bb11dd4b4111435bb7efc15b0af61', ref: (el) => this.valueSpanEl = el }, this.value), index.h("pds-icon", { key: '0f35d997eb1766631ac6098bdb12d0aff9813012', icon: index$1.copy, size: "16px" }))));
    }
    get el() { return index.getElement(this); }
    static get watchers() { return {
        "truncate": ["handleTruncateChange"],
        "value": ["handleValueChange"]
    }; }
};
PdsCopytext.style = pdsCopytextCss;

exports.pds_copytext = PdsCopytext;
//# sourceMappingURL=pds-copytext.entry.cjs.js.map
