import { Host, h } from "@stencil/core";
import { closest } from "../../../utils/closest";
export class PdsTableRow {
    constructor() {
        this.handleClick = () => {
            this.isSelected = !this.isSelected; // Toggle the selected state
            this.handleSelect(this.isSelected);
        };
        this.handleSelect = (isSelected) => {
            this.indeterminate = false;
            if (!closest('pds-table-head', this.hostElement)) {
                const rowIndex = Array.from(this.hostElement.parentNode.children).indexOf(this.hostElement);
                this.pdsTableRowSelected.emit({
                    rowIndex,
                    isSelected,
                });
            }
        };
        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.indeterminate = undefined;
        this.isSelected = undefined;
    }
    classNames() {
        const classNames = [];
        if (this.isSelected) {
            classNames.push("is-selected");
        }
        return classNames.join('  ');
    }
    componentWillRender() {
        this.tableRef = this.hostElement.closest('pds-table');
        if (this.tableRef && this.tableRef.fixedColumn) {
            const tableCell = this.hostElement.querySelector('pds-table-cell');
            tableCell === null || tableCell === void 0 ? void 0 : tableCell.classList.add("is-fixed");
        }
    }
    componentWillLoad() {
        if (this.isSelected) {
            this.handleSelect(this.isSelected);
        }
    }
    render() {
        return (h(Host, { key: '9dceed18de8775f4356afec4bafe85b8e01d784d', class: this.classNames(), role: "row" }, this.tableRef && this.tableRef.selectable && (h("pds-table-cell", { key: 'a56e002c28377096430b6370744dc32726f578d3', part: this.tableRef.fixedColumn ? 'checkbox-cell' : '', class: this.tableRef.selectable ? 'has-checkbox' : '' }, h("pds-checkbox", { key: '5d7d6b2632d619bc7e7447f1eaf6ec7d9a2e2318', componentId: this.generateUniqueId(), onClick: this.handleClick, indeterminate: this.indeterminate, label: "Select Row", labelHidden: true, checked: this.isSelected }))), h("slot", { key: '9dcbe00aba5b4bba6318a301609ce2226c303178' })));
    }
    static get is() { return "pds-table-row"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["../../../global/styles/base.scss", "pds-table-row.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["../../../global/styles/base.css", "pds-table-row.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 row selected is in an indeterminate state."
                },
                "attribute": "indeterminate",
                "reflect": false
            },
            "isSelected": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Determines if the table row is currently selected."
                },
                "attribute": "is-selected",
                "reflect": false
            }
        };
    }
    static get events() {
        return [{
                "method": "pdsTableRowSelected",
                "name": "pdsTableRowSelected",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Event that is emitted when the checkbox is clicked, carrying the selected value."
                },
                "complexType": {
                    "original": "{ rowIndex: number; isSelected: boolean; }",
                    "resolved": "{ rowIndex: number; isSelected: boolean; }",
                    "references": {}
                }
            }];
    }
    static get elementRef() { return "hostElement"; }
}
//# sourceMappingURL=pds-table-row.js.map
