import { proxyCustomElement, HTMLElement, h, Host } from '@stencil/core/internal/client';
import { i as isScrolledFromInlineStart } from './scroll.js';
import { s as setupTruncationTooltip } from './truncation-tooltip.js';

const pdsTableCellCss = ":host{--box-shadow-fixed:3px 3px 6px -2px rgba(0, 0, 0, 0.2);color:var(--pine-color-text);display:table-cell;font-family:var(--pine-font-family-body);font-size:var(--pine-font-size);font-weight:var(--pine-font-weight-regular);inset-inline-start:var(--pine-dimension-none);letter-spacing:var(--pine-letter-spacing);line-height:var(--pine-line-height-body);padding:var(--pine-dimension-sm);vertical-align:middle}:host(.is-compact){padding-block:var(--pine-dimension-2xs)}:host(.is-fixed){background:var(--pine-color-background-container);inset-inline-start:0;position:-webkit-sticky;position:sticky;z-index:var(--pine-z-index-raised)}:host-context([dir=rtl]){--box-shadow-fixed:-3px 3px 6px -2px rgba(0, 0, 0, 0.2)}:host(.has-scrolled.is-fixed){-webkit-box-shadow:var(--box-shadow-fixed);box-shadow:var(--box-shadow-fixed)}:host(.is-truncated){max-width:var(--pds-table-cell-truncate-max-width, 100px);overflow:hidden;text-overflow:ellipsis;white-space:nowrap}:host(.has-checkbox){vertical-align:middle;width:16px}:host(.pds-table-cell--align-start){text-align:start}:host(.pds-table-cell--align-center){text-align:center}:host(.pds-table-cell--align-end){text-align:end}:host(.pds-table-cell--align-justify){text-align:justify}";

const PdsTableCell = /*@__PURE__*/ proxyCustomElement(class PdsTableCell extends HTMLElement {
    constructor(registerHost) {
        super();
        if (registerHost !== false) {
            this.__registerHost();
        }
        this.__attachShadow();
        this.scrollContainer = null;
        this.setupRetries = 0;
        this.truncationCleanup = null;
        /**
         * Determines if the table is currently scrolling.
         * @defaultValue false
         */
        this.tableScrolling = false;
        /**
         * Handles scroll events to update fixed column shadow state.
         * Updates the tableScrolling state to control CSS classes for fixed column shadows.
         * @private
         */
        this.handleScroll = () => {
            if (!this.scrollContainer) {
                return;
            }
            try {
                this.tableScrolling = isScrolledFromInlineStart(this.scrollContainer);
            }
            catch (error) {
                console.warn('Scroll handler error:', error);
            }
        };
    }
    componentWillRender() {
        this.tableRef = this.hostElement.closest('pds-table');
    }
    componentDidLoad() {
        if (this.tableRef && this.tableRef.responsive && this.tableRef.fixedColumn) {
            // For responsive tables with fixed columns, set up scroll detection
            // This enables the first column to show a shadow when the table is scrolled horizontally
            this.setupScrollListener();
        }
        if (this.truncate) {
            this.initTruncationTooltip();
        }
    }
    disconnectedCallback() {
        this.cleanupScrollListener();
        this.destroyTruncationTooltip();
    }
    setupScrollListener() {
        var _a;
        if (!this.tableRef)
            return;
        // Query shadowRoot once and cache the container
        const container = (_a = this.tableRef.shadowRoot) === null || _a === void 0 ? void 0 : _a.querySelector('.pds-table-responsive-container');
        if (container) {
            // Container available immediately
            this.scrollContainer = container;
            this.scrollContainer.addEventListener('scroll', this.handleScroll, { passive: true });
            this.handleScroll(); // Initial check
            this.setupRetries = 0; // Reset counter on success
        }
        else {
            // Container not ready, set up timer for retry with bounds
            this.setupTimer = window.setTimeout(() => {
                if (this.scrollContainer)
                    return; // Already found
                this.setupRetries = (this.setupRetries || 0) + 1;
                if (this.setupRetries <= 50) {
                    this.setupScrollListener();
                }
                else {
                    console.warn('Failed to find responsive container after 50 attempts');
                }
            }, 100);
        }
    }
    cleanupScrollListener() {
        if (this.scrollContainer) {
            this.scrollContainer.removeEventListener('scroll', this.handleScroll);
            this.scrollContainer = null;
        }
        if (this.setupTimer !== undefined) {
            window.clearTimeout(this.setupTimer);
            this.setupTimer = undefined;
        }
        this.setupRetries = 0; // Reset retry counter
    }
    handleTruncateChange(newValue) {
        if (newValue) {
            this.initTruncationTooltip();
        }
        else {
            this.destroyTruncationTooltip();
        }
    }
    initTruncationTooltip() {
        this.destroyTruncationTooltip();
        this.truncationCleanup = setupTruncationTooltip({
            hostEl: this.hostElement,
            contentEl: this.hostElement,
            getTooltipText: () => this.hostElement.textContent || '',
        });
    }
    destroyTruncationTooltip() {
        if (this.truncationCleanup) {
            this.truncationCleanup();
            this.truncationCleanup = null;
        }
    }
    classNames() {
        const classNames = [];
        if (this.tableRef && this.tableRef.compact) {
            classNames.push('is-compact');
        }
        if (this.cellAlign) {
            classNames.push(`pds-table-cell--align-${this.cellAlign}`);
        }
        if (this.truncate) {
            classNames.push('is-truncated');
        }
        if (this.tableRef && this.tableRef.fixedColumn && this.tableScrolling) {
            classNames.push('has-scrolled');
        }
        return classNames.join(' ');
    }
    render() {
        return (h(Host, { key: 'b108b0fce3ff0d5c503922a6c936ffd6ca52523f', class: this.classNames(), role: "gridcell", part: "cell", tabIndex: this.truncate ? 0 : undefined, style: this.tableRef &&
                this.tableRef.fixedColumn &&
                this.tableRef.selectable
                ? { '--fixed-cell-position': '40px' }
                : {} }, h("slot", { key: 'ebc1709ef52029bc6afec2fa89a90d47620892cb' })));
    }
    get hostElement() { return this; }
    static get watchers() { return {
        "truncate": ["handleTruncateChange"]
    }; }
    static get style() { return pdsTableCellCss; }
}, [257, "pds-table-cell", {
        "cellAlign": [1, "cell-align"],
        "truncate": [4],
        "tableScrolling": [32]
    }, undefined, {
        "truncate": ["handleTruncateChange"]
    }]);
function defineCustomElement() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["pds-table-cell"];
    components.forEach(tagName => { switch (tagName) {
        case "pds-table-cell":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, PdsTableCell);
            }
            break;
    } });
}

export { PdsTableCell as P, defineCustomElement as d };
//# sourceMappingURL=pds-table-cell2.js.map

//# sourceMappingURL=pds-table-cell2.js.map