"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var antd_1 = require("antd");
var React = require("react");
var _ = require("lodash");
var idxRender = function (d, i, o) {
    return o + 1;
};
var PAGE_SIZE = 50;
/**
 * 排行榜单
*/
var TopTable = /** @class */ (function (_super) {
    __extends(TopTable, _super);
    function TopTable() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.topColor = true;
        _this.autoRow = true;
        _this.state = {
            columns: [],
            current: 0
        };
        return _this;
    }
    TopTable.prototype.componentDidMount = function () {
        var notShowIndex = this.props.showIndex === false;
        this.topColor = this.props.topColor === false ? false : true;
        this.autoRow = this.props.autoRow === false ? false : true;
        var columns = [];
        if (!notShowIndex) {
            var title = _.isString(this.props.showIndex) ? this.props.showIndex : '排行';
            columns.push({
                width: 80,
                className: '_index',
                title: title,
                render: idxRender,
            });
        }
        columns = _.concat(columns, this.props.columns);
        this.setState({ columns: columns });
        if (this.autoRow) {
            this.goTo0();
        }
    };
    TopTable.prototype.goTo0 = function () {
        if (_.isArray(this.props.dataSource) && this.props.dataSource.length > 0) {
            this.onRowClick(this.props.dataSource[0], 0);
        }
        else {
            this.onRowClick(null, -1);
        }
    };
    TopTable.prototype.componentDidUpdate = function (old) {
        if (!_.isEqual(old.dataSource, this.props.dataSource)) {
            this.goTo0();
        }
    };
    TopTable.prototype.onRowClick = function (record, idx, evt) {
        if (evt === void 0) { evt = null; }
        if (this.props.onRowClick) {
            this.setState({ current: idx });
            this.props.onRowClick(record, idx, evt);
        }
    };
    TopTable.prototype.render = function () {
        var _this = this;
        var pageSize = this.props.pageSize || PAGE_SIZE;
        var dataSource = this.props.dataSource;
        if (_.isArray(dataSource) && dataSource.length > pageSize) {
            dataSource.length = pageSize;
        }
        return React.createElement(antd_1.Table, __assign({}, this.props, { pagination: false, columns: this.state.columns, onRowClick: function (record, idx, evt) { return _this.onRowClick(record, idx, evt); }, className: "vap-top-table", rowClassName: function (item, index) {
                var _class = ["top-item"];
                if (index == _this.state.current && _this.props.onRowClick) {
                    _class.push('ant-table-row-selected');
                }
                if (_this.topColor) {
                    _class.push("top-item-" + index);
                }
                if (_this.props.rowClassName) {
                    var className = _this.props.rowClassName(item, index);
                    if (className) {
                        _class.push(className);
                    }
                }
                return _class.join(' ');
            } }));
    };
    return TopTable;
}(React.PureComponent));
exports.default = TopTable;
