import { Host, h } from "@stencil/core";
import { downSmall, upSmall } from "@pine-ds/icons/icons";
export class PdsTableHeadCell {
    constructor() {
        this.handleScroll = () => {
            if (this.tableRef.scrollLeft > 0) {
                this.tableScrolling = true;
            }
            else {
                this.tableScrolling = false;
            }
        };
        this.toggleSort = () => {
            if (this.sortable) {
                const column = this.hostElement.innerText.trim();
                this.sortingDirection = this.sortingDirection === 'asc' ? 'desc' : 'asc';
                this.tableRef.querySelectorAll('pds-table-head-cell').forEach((headCell) => {
                    headCell.classList.remove('is-active');
                });
                this.hostElement.classList.toggle('is-active');
                this.pdsTableSort.emit({ column, direction: this.sortingDirection });
            }
        };
        this.sortable = undefined;
        this.sortingDirection = 'asc';
        this.tableScrolling = false;
        this.isSelected = false;
    }
    componentWillRender() {
        this.tableRef = this.hostElement.closest('pds-table');
    }
    componentDidLoad() {
        if (this.tableRef && this.tableRef.responsive && this.tableRef.fixedColumn) {
            this.tableRef.addEventListener('scroll', this.handleScroll);
        }
    }
    classNames() {
        const classNames = [];
        if (this.tableRef && this.tableRef.compact) {
            classNames.push('is-compact');
        }
        if (this.sortable) {
            classNames.push('is-sortable');
        }
        if (this.sortable && this.sortingDirection !== null) {
            classNames.push('sort-' + this.sortingDirection);
        }
        if (this.tableRef && this.tableRef.fixedColumn && this.tableScrolling) {
            classNames.push('has-scrolled');
        }
        return classNames.join('  ');
    }
    render() {
        return (h(Host, { key: 'dcb6684146176db2dfc73c99ead82af732ef1f72', class: this.classNames(), role: "columnheader", onClick: this.toggleSort, style: this.tableRef &&
                this.tableRef.fixedColumn &&
                this.tableRef.selectable
                ? { '--fixed-cell-position': '40px' }
                : {} }, h("slot", { key: 'ea960d9245e41ce504083f2645aa01bd3a9404f0' }), this.sortable && (h("pds-icon", { key: 'dfa59925bbda74da7b2a3bf87f448cf323ff6342', icon: this.sortingDirection === 'asc' ? upSmall : downSmall }))));
    }
    static get is() { return "pds-table-head-cell"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["../../../global/styles/base.scss", "pds-table-head-cell.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["../../../global/styles/base.css", "pds-table-head-cell.css"]
        };
    }
    static get properties() {
        return {
            "sortable": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Determines whether the table column is sortable when set to `true`."
                },
                "attribute": "sortable",
                "reflect": false
            }
        };
    }
    static get states() {
        return {
            "sortingDirection": {},
            "tableScrolling": {},
            "isSelected": {}
        };
    }
    static get events() {
        return [{
                "method": "pdsTableSort",
                "name": "pdsTableSort",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Event emitted to signal that a table column header has been sorted, providing information about the sorted column's name and sorting direction."
                },
                "complexType": {
                    "original": "{ column: string; direction: string }",
                    "resolved": "{ column: string; direction: string; }",
                    "references": {}
                }
            }];
    }
    static get elementRef() { return "hostElement"; }
}
//# sourceMappingURL=pds-table-head-cell.js.map
