"use strict";
/**
 * @desc 数据展示表格
 * @author denghuan
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const classnames_1 = __importDefault(require("classnames"));
const ag_grid_react_1 = require("ag-grid-react");
const helper_1 = require("./helper");
const sort_changed_1 = __importDefault(require("./hooks/sort-changed"));
const grid_ready_1 = __importDefault(require("./hooks/grid-ready"));
function JXTable(props) {
    const { wrapClassName, columnDefs, gridOption, tableData, isLoading, onTableReady, onSortChanged, } = props;
    const tableInstance = react_1.useRef();
    // 窗口大小改变事件
    const windowResizeHandler = react_1.useCallback(() => {
        tableInstance.current && tableInstance.current.api.sizeColumnsToFit();
    }, [gridOption, columnDefs]);
    // 窗口移动副作用
    react_1.useEffect(() => {
        window.addEventListener('resize', windowResizeHandler);
        return () => {
            window.removeEventListener('resize', windowResizeHandler);
        };
    }, [gridOption, columnDefs]);
    // 表格数据变化副作用
    react_1.useEffect(() => {
        tableInstance.current && tableInstance.current.api.setRowData(tableData);
    }, [tableData]);
    // 表格加载状态副作用
    react_1.useEffect(() => {
        if (!tableInstance.current || !tableInstance.current.api)
            return;
        if (isLoading) {
            tableInstance.current.api.showLoadingOverlay();
        }
        else if (tableData.length > 0) {
            tableInstance.current.api.hideOverlay();
        }
        else {
            tableInstance.current.api.showNoRowsOverlay();
        }
    }, [isLoading]);
    const sortChanged = sort_changed_1.default(onSortChanged);
    const gridReady = grid_ready_1.default(tableInstance, tableData, onTableReady);
    // 表格配置
    const tableConfig = react_1.useMemo(() => {
        return {
            props: {
                ...helper_1.assemblyTableProps(props),
                onGridReady: gridReady,
                onSortChanged: sortChanged,
            },
        };
    }, [gridOption, isLoading, columnDefs, onTableReady, onSortChanged]);
    return (react_1.default.createElement("div", { className: classnames_1.default('data-table', 'data-table-with-operations', wrapClassName, 'ag-theme-alpine') },
        react_1.default.createElement(ag_grid_react_1.AgGridReact, { headerHeight: 32, domLayout: "autoHeight", ...tableConfig.props })));
}
exports.default = JXTable;
