import { Host, h } from "@stencil/core";
import { closest } from "../../../utils/closest";
export class PdsTableHead {
    constructor() {
        this.generateUniqueId = () => {
            const randomString = Math.random().toString(36).substring(2, 8);
            const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
            const uniqueId = `${randomString}-${timestamp}`;
            return uniqueId;
        };
        this.handleInput = (ev) => {
            this.isSelected = !ev.target.checked;
            this.handleSelect(this.isSelected);
        };
        this.handleSelect = (isSelected) => {
            this.indeterminate = false;
            if (closest('pds-table-head', this.hostElement)) {
                this.pdsTableSelectAll.emit({ isSelected });
            }
        };
        this.indeterminate = undefined;
        this.isSelected = undefined;
    }
    componentWillLoad() {
        if (this.isSelected) {
            this.handleSelect(this.isSelected);
        }
    }
    componentWillRender() {
        this.tableRef = this.hostElement.closest('pds-table');
        if (this.tableRef && this.tableRef.fixedColumn) {
            const tableCell = this.hostElement.querySelector('pds-table-head-cell:first-child');
            tableCell === null || tableCell === void 0 ? void 0 : tableCell.classList.add("is-fixed");
        }
    }
    render() {
        return (h(Host, { key: '7729235908073cae63294e4ad641bae0403e2c34', role: "row" }, this.tableRef && this.tableRef.selectable && (h("pds-table-head-cell", { key: '2bb801124a3f8d1c9f18d03d1461953da4cdafc6', part: this.tableRef.selectable ? 'checkbox-cell' : '' }, h("pds-checkbox", { key: '2119be5b2db13a44add35fa9c9fc761a68a2b060', componentId: this.generateUniqueId(), indeterminate: this.indeterminate, onInput: this.handleInput, label: "Select All Rows", labelHidden: true, checked: this.isSelected }))), h("slot", { key: 'baa5b3af10a77e1a8aff537c3c731fbc59a54ca8' })));
    }
    static get is() { return "pds-table-head"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["pds-table-head.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["pds-table-head.css"]
        };
    }
    static get properties() {
        return {
            "indeterminate": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Indicates that the selection state is indeterminate."
                },
                "attribute": "indeterminate",
                "reflect": false
            },
            "isSelected": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "A local state to track whether the row is currently selected."
                },
                "attribute": "is-selected",
                "reflect": false
            }
        };
    }
    static get events() {
        return [{
                "method": "pdsTableSelectAll",
                "name": "pdsTableSelectAll",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Event that is emitted when the select all checkbox is clicked, carrying the selected value."
                },
                "complexType": {
                    "original": "{ isSelected: boolean }",
                    "resolved": "{ isSelected: boolean; }",
                    "references": {}
                }
            }];
    }
    static get elementRef() { return "hostElement"; }
}
//# sourceMappingURL=pds-table-head.js.map
