"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"));

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

;

function ActionButton(props) {
  return /*#__PURE__*/_react["default"].createElement(_consoleComponents.Button, {
    type: "primary",
    text: true,
    onClick: props.onClick,
    className: "lp-button"
  }, props.text, props.inconType && /*#__PURE__*/_react["default"].createElement(_consoleComponents.Icon, {
    type: props.inconType,
    size: "xxs"
  }));
}

var LPCollapse =
/** @class */
function (_super) {
  (0, _tslib.__extends)(LPCollapse, _super);

  function LPCollapse(props) {
    var _this = _super.call(this, props) || this;

    _this.onExpand = function () {
      _this.setState({
        showExpand: true
      });

      _this.props.extendsIt && _this.props.extendsIt('Expand');
    };

    _this.onCollapse = function () {
      _this.setState({
        showExpand: false
      });

      _this.props.extendsIt && _this.props.extendsIt('Collapse');
    };

    _this.triggerBoxId = "triggerBox" + Math.random();
    _this.state = {
      showCollapse: _react["default"].Children.count(props.content || props.children) > props.maxLine,
      showExpand: false,
      noString: /*#__PURE__*/_react["default"].isValidElement(props.content || props.children) || Array.isArray(props.content || props.children)
    };
    return _this;
  }

  LPCollapse.prototype.componentDidMount = function () {
    if (!this.state.noString) {
      //数据源是文本
      var maxLine = this.props.maxLine;
      var maxLine_ = 1;

      if (typeof maxLine === "string") {
        maxLine_ = parseInt(maxLine);
      } else {
        maxLine_ = maxLine;
      }

      var triggerBox = document.getElementById(this.triggerBoxId);
      var height = triggerBox ? triggerBox.offsetHeight : 0;
      var lineHeight = window.getComputedStyle(triggerBox, null).getPropertyValue('line-height');
      this.setState({
        showCollapse: height > maxLine_ * (parseFloat(lineHeight) + 0.5)
      });
    }
  };

  LPCollapse.prototype.render = function () {
    var _a = this.props,
        content = _a.content,
        maxLine = _a.maxLine,
        children = _a.children,
        local = _a.local,
        className = _a.className,
        style = _a.style;
    var _b = this.state,
        showCollapse = _b.showCollapse,
        showExpand = _b.showExpand,
        noString = _b.noString;
    var dom = null;
    var content_ = content || children;

    if (!noString) {
      //数据源是文本
      dom = showCollapse ? /*#__PURE__*/_react["default"].createElement("div", {
        className: className,
        style: style
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: "lp-collapse"
      }, showExpand ? /*#__PURE__*/_react["default"].createElement("span", null, content_, /*#__PURE__*/_react["default"].createElement(ActionButton, {
        onClick: this.onCollapse,
        text: local.collapse,
        inconType: 'caret-up'
      })) : /*#__PURE__*/_react["default"].createElement("div", {
        className: "lp-collapse-flex"
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: "lp-text-overflow lp-line-clamp-" + maxLine
      }, content_), /*#__PURE__*/_react["default"].createElement("div", {
        className: "lp-button-area"
      }, /*#__PURE__*/_react["default"].createElement(ActionButton, {
        onClick: this.onExpand,
        text: local.expand,
        inconType: 'caret-down'
      }))))) : /*#__PURE__*/_react["default"].createElement("div", {
        className: className,
        style: style
      }, /*#__PURE__*/_react["default"].createElement("div", {
        id: this.triggerBoxId
      }, content || children));
    } else {
      //数据源是react元素
      dom = _react["default"].Children.map(content_, function (child, index) {
        if (index < maxLine || showExpand) {
          return /*#__PURE__*/_react["default"].cloneElement(child, {
            key: "iotCollapse" + index
          });
        } else {
          return null;
        }
      });

      if (showCollapse) {
        dom.push(showExpand ? /*#__PURE__*/_react["default"].createElement(ActionButton, {
          key: "iotCollapsecollapse",
          onClick: this.onCollapse,
          text: local.collapse
        }) : /*#__PURE__*/_react["default"].createElement(ActionButton, {
          key: "iotCollapseexpand",
          onClick: this.onExpand,
          text: local.expand
        }));
      }
    }

    return dom;
  };

  LPCollapse.displayName = 'iotCollapse';
  return LPCollapse;
}(_react["default"].Component);

var _default = LPCollapse;
exports["default"] = _default;
LPCollapse.defaultProps = {
  maxLine: 1,
  local: {
    expand: '展开',
    collapse: '收起'
  }
};