"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _tslib = require("tslib");

var _consoleComponents = require("@alicloud/console-components");

var _react = _interopRequireDefault(require("react"));

var _balloon = _interopRequireDefault(require("../balloon"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

var Row = _consoleComponents.Grid.Row,
    Col = _consoleComponents.Grid.Col;
;

var IotGrid =
/** @class */
function (_super) {
  (0, _tslib.__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 = (0, _tslib.__rest)(tip, ["tipContext"]);

        if (typeof tip === "string") {
          tipContext = tip;
        }

        return /*#__PURE__*/_react["default"].createElement(_balloon["default"].Icon, (0, _tslib.__assign)({
          title: /*#__PURE__*/_react["default"].createElement(_balloon["default"], {
            maxLine: maxLine,
            overShowBalloon: true,
            trigger: value
          }, value),
          iconStyle: {
            position: "absolute",
            bottom: 0,
            right: 0
          }
        }, restTip), tipContext);
      } else if (typeof value === "string") {
        return /*#__PURE__*/_react["default"].createElement(_balloon["default"], {
          maxLine: maxLine,
          overShowBalloon: true,
          trigger: value
        }, value);
      } else if (value.balloonProps && value.text) {
        return /*#__PURE__*/_react["default"].createElement(_balloon["default"], (0, _tslib.__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 /*#__PURE__*/_react["default"].createElement(Col, {
        span: item.span,
        key: "lp_grid_" + index,
        lang: _this.props.lang
      }, /*#__PURE__*/_react["default"].createElement(Row, {
        wrap: true,
        className: "lp-grid-col"
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: "lp-grid-col-left",
        style: {
          width: nameWidth
        }
      }, _this.renderContext(item.name, item.tip, maxLine)), /*#__PURE__*/_react["default"].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 /*#__PURE__*/_react["default"].createElement("div", {
      className: "lp-grid"
    }, /*#__PURE__*/_react["default"].createElement(Row, {
      wrap: true,
      className: "lp-grid-row"
    }, content));
  };

  return IotGrid;
}(_react["default"].Component);

var _default = IotGrid;
exports["default"] = _default;
IotGrid.defaultProps = {
  maxLine: 2,
  lang: "en",
  layout: {
    nameWidth: 120
  }
};