import { Host, h } from "@stencil/core";
import { AllCommunityModule, createGrid, ModuleRegistry, provideGlobalGridOptions, } from "ag-grid-community";
import classNames from "classnames";
import { isNestedInIfxComponent } from "../..//shared/utils/dom-utils";
import { detectFramework } from "../..//shared/utils/framework-detection";
import { trackComponent } from "../../shared/utils/tracking";
import { CustomLoadingOverlay } from "./customLoadingOverlay";
import { CustomNoRowsOverlay } from "./customNoRowsOverlay";
export class Table {
    gridOptions;
    /** Column definitions for the table (array or JSON string). */
    cols;
    /** Row data for the table (array or JSON string). */
    rows;
    columnDefs = [];
    rowData = [];
    /** Row height preset (e.g. default, compact). */
    rowHeight = "default";
    /** Overall table height (e.g. fixed px value or 'auto'). */
    tableHeight = "auto";
    /** Visual style variant of the table (e.g. default, compact). */
    variant = "default";
    uniqueKey;
    host;
    container;
    gridApi;
    gridInitialized = false;
    componentWillLoad() {
        ModuleRegistry.registerModules([AllCommunityModule]);
        provideGlobalGridOptions({ theme: "legacy" });
        this.uniqueKey = `unique-${Math.floor(Math.random() * 1000000)}`;
        this.setColsAndRows(); //needed?
        this.setGridOptions();
    }
    setGridOptions() {
        this.gridOptions = {
            rowHeight: this.rowHeight === "default" ? 40 : 32,
            headerHeight: 40,
            defaultColDef: {
                resizable: true,
                autoHeight: true,
            },
            suppressCellFocus: false,
            enableCellTextSelection: true,
            suppressDragLeaveHidesColumns: true,
            suppressRowHoverHighlight: true,
            onFirstDataRendered: this.onFirstDataRendered,
            columnDefs: this.columnDefs,
            rowData: this.rowData,
            loadingOverlayComponent: CustomLoadingOverlay,
            noRowsOverlayComponent: CustomNoRowsOverlay,
            noRowsOverlayComponentParams: {
                noRowsMessageFunc: () => "No rows found at: " + new Date().toLocaleTimeString(),
            },
            icons: {
                sortAscending: '<ifx-icon icon="arrow-triangle-up-16"></ifx-icon>',
                sortDescending: '<ifx-icon icon="arrow-triangle-down-16"></ifx-icon>',
                sortUnSort: '<a class="unsort-icon-custom-color"><ifx-icon icon="arrow-triangle-vertikal-16"></ifx-icon></a>',
            },
            rowDragManaged: this.columnDefs.some((col) => col.dndSource === true)
                ? true
                : false,
            animateRows: this.columnDefs.some((col) => col.dndSource === true)
                ? true
                : false,
        };
    }
    setColsAndRows() {
        if (typeof this.rows === "string" && typeof this.cols === "string") {
            try {
                if (this.cols) {
                    this.columnDefs = JSON.parse(this.cols);
                }
                if (this.rows) {
                    this.rowData = JSON.parse(this.rows);
                }
            }
            catch (err) {
                console.error("Failed to parse input:", err);
            }
        }
        else if ((Array.isArray(this.rows) || typeof this.rows === "object") &&
            (Array.isArray(this.cols) || typeof this.cols === "object")) {
            this.columnDefs = this.cols;
            this.rowData = this.rows;
        }
        else {
            console.error("Unexpected value for cols and rows:", this.rows, this.cols);
        }
    }
    getRowData() {
        let rows = [];
        if (typeof this.rows === "string") {
            try {
                if (this.rows) {
                    rows = JSON.parse(this.rows);
                }
            }
            catch (err) {
                console.error("Failed to parse input:", err);
            }
        }
        else if (Array.isArray(this.rows) || typeof this.rows === "object") {
            rows = this.rows;
        }
        else {
            console.error("Unexpected value for rows: ", this.rows);
        }
        return rows;
    }
    getColData() {
        let cols = [];
        if (typeof this.cols === "string") {
            try {
                if (this.cols) {
                    cols = JSON.parse(this.cols);
                }
            }
            catch (err) {
                console.error("Failed to parse input:", err);
            }
        }
        else if (Array.isArray(this.cols) || typeof this.cols === "object") {
            cols = this.cols;
        }
        else {
            console.error("Unexpected value for cols: ", this.cols);
        }
        return cols;
    }
    onFirstDataRendered(params) {
        params.api.sizeColumnsToFit();
    }
    componentWillUpdate() {
        this.setColsAndRows();
        this.gridOptions.columnDefs = this.columnDefs;
        this.gridOptions.rowData = this.rowData;
        if (this.gridApi) {
            this.gridApi.setGridOption("rowData", this.rowData);
            this.gridApi.setGridOption("columnDefs", this.columnDefs);
        }
    }
    async componentDidLoad() {
        if (this.container && !this.gridInitialized) {
            if (!isNestedInIfxComponent(this.host)) {
                const framework = detectFramework();
                trackComponent("ifx-basic-table", await framework);
            }
            this.gridApi = createGrid(this.container, this.gridOptions);
            if (this.gridApi) {
                this.gridApi.sizeColumnsToFit({
                    defaultMinWidth: 100,
                });
                this.gridApi.setGridOption("columnDefs", this.getColData());
                this.gridApi.setGridOption("rowData", this.getRowData());
                this.gridInitialized = true;
            }
        }
    }
    getClassNames() {
        return classNames(this.tableHeight === "auto" && "table-wrapper ag-root-wrapper-body", "table-wrapper");
    }
    getTableStyle() {
        if (this.tableHeight !== "auto") {
            return {
                height: this.tableHeight,
            };
        }
        return {};
    }
    render() {
        return (h(Host, { key: '8cdc65425deb8ff1afc616adaac1756a1ad99518' }, h("div", { key: '88f1bb01ae32d34a45610df525da7b26bcb8b054', id: "table-wrapper", class: this.getClassNames() }, h("div", { key: 'bb41d94b2fd3f9fb802e8a907ce546ae2b5da81a', id: `ifxTable-${this.uniqueKey}`, class: `ifx-ag-grid ${this.variant === "zebra" ? "zebra" : ""}`, style: this.getTableStyle(), ref: (el) => (this.container = el) }))));
    }
    static get is() { return "ifx-basic-table"; }
    static get originalStyleUrls() {
        return {
            "$": ["table.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["table.css"]
        };
    }
    static get properties() {
        return {
            "cols": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "any[] | string",
                    "resolved": "any[] | string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Column definitions for the table (array or JSON string)."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "cols"
            },
            "rows": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "any[] | string",
                    "resolved": "any[] | string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Row data for the table (array or JSON string)."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "rows"
            },
            "rowHeight": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Row height preset (e.g. default, compact)."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "row-height",
                "defaultValue": "\"default\""
            },
            "tableHeight": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Overall table height (e.g. fixed px value or 'auto')."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "table-height",
                "defaultValue": "\"auto\""
            },
            "variant": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Visual style variant of the table (e.g. default, compact)."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "variant",
                "defaultValue": "\"default\""
            }
        };
    }
    static get states() {
        return {
            "gridOptions": {},
            "columnDefs": {},
            "rowData": {},
            "uniqueKey": {}
        };
    }
    static get elementRef() { return "host"; }
}
