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: 'eb7e9eef443d0f222482b427a5ced7eaa4c0acf4', role: "row" }, this.tableRef && this.tableRef.selectable && (h("pds-table-head-cell", { key: '6b7c87f0d07aac6e5b12b9e3a63914061f850da7', part: this.tableRef.selectable ? 'checkbox-cell' : '' }, h("pds-checkbox", { key: '74a7b333189bf92219b5a82bf0897d476b7adc8a', componentId: this.generateUniqueId(), indeterminate: this.indeterminate, onInput: this.handleInput, label: "Select All Rows", labelHidden: true, checked: this.isSelected }))), h("slot", { key: '5ef8daf29f10505b082619755b61014a52dbd00c' })));
    }
    static get is() { return "pds-table-head"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["../../../global/styles/base.scss", "pds-table-head.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["../../../global/styles/base.css", "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": "Determines if the select all checkbox is in an indeterminate state."
                },
                "attribute": "indeterminate",
                "reflect": false
            },
            "isSelected": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "defaultValue",
                            "text": "false"
                        }],
                    "text": "Determines if the table 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
