import { __assign, __extends, __rest } from "tslib";
import { Grid } from '@alicloud/console-components';
import React from 'react';
import LPBalloon from '../balloon';
var Row = Grid.Row, Col = Grid.Col;
;
var IotGrid = /** @class */ (function (_super) {
    __extends(IotGrid, _super);
    function IotGrid() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.renderContext = function (value, tip, maxLine) {
            if (tip) {
                var tipContext = tip.tipContext, restTip = __rest(tip, ["tipContext"]);
                if (typeof tip === "string") {
                    tipContext = tip;
                }
                return React.createElement(LPBalloon.Icon, __assign({ title: React.createElement(LPBalloon, { maxLine: maxLine, overShowBalloon: true, trigger: value }, value), iconStyle: { position: "absolute", bottom: 0, right: 0 } }, restTip), tipContext);
            }
            else if (typeof value === "string") {
                return React.createElement(LPBalloon, { maxLine: maxLine, overShowBalloon: true, trigger: value }, value);
            }
            else if (value.balloonProps && value.text) {
                return React.createElement(LPBalloon, __assign({ maxLine: maxLine, overShowBalloon: true, trigger: value.text }, value.balloonProps), value.text);
            }
            else {
                return value;
            }
        };
        _this.renderCol = function (item, index, maxLine) {
            var layout = _this.props.layout;
            var nameWidth = layout.nameWidth;
            var _a = item.visible, visible = _a === void 0 ? true : _a;
            if (!visible && typeof visible === 'boolean') {
                return null;
            }
            return (React.createElement(Col, { span: item.span, key: "lp_grid_" + index, lang: _this.props.lang },
                React.createElement(Row, { wrap: true, className: "lp-grid-col" },
                    React.createElement("div", { className: "lp-grid-col-left", style: { width: nameWidth } }, _this.renderContext(item.name, item.tip, maxLine)),
                    React.createElement("div", { className: "lp-grid-col-right" }, _this.renderContext(item.value, undefined, maxLine)))));
        };
        return _this;
    }
    IotGrid.prototype.render = function () {
        var _a = this.props, dataSource = _a.dataSource, maxLine = _a.maxLine;
        if (!(dataSource && dataSource.length > 0)) {
            return null;
        }
        var visitableDataSource = dataSource.filter(function (i) {
            if (i.visible === false) {
                return false;
            }
            else {
                return true;
            }
        });
        var content = [];
        var total = 0, nextTotal = 0;
        for (var i = 0; i < visitableDataSource.length; i++) {
            var nextOne = visitableDataSource[i + 1];
            var currentOne = visitableDataSource[i];
            if (currentOne.span) {
                total = total + currentOne.span;
                if (nextOne) {
                    var nextOneSpan = nextOne.span ? nextOne.span : 0;
                    nextTotal = total + nextOneSpan;
                    if (nextTotal > 24 && total <= 24) {
                        currentOne.span = currentOne.span + 24 - total;
                        total = 0;
                        nextTotal = 0;
                    }
                }
                else if (total < 24) {
                    currentOne.span = currentOne.span + 24 - total;
                    total = 0;
                    nextTotal = 0;
                }
            }
            content.push(this.renderCol(currentOne, i, maxLine));
        }
        return (React.createElement("div", { className: "lp-grid" },
            React.createElement(Row, { wrap: true, className: "lp-grid-row" }, content)));
    };
    return IotGrid;
}(React.Component));
export default IotGrid;
IotGrid.defaultProps = {
    maxLine: 2,
    lang: "en",
    layout: {
        nameWidth: 120
    }
};
