var _excluded = ["prefixCls", "className", "data", "hasLine", "isCarousel", "square", "activeStyle", "activeClassName"],
    _excluded2 = ["columnNum", "carouselMaxRow", "onClick", "renderItem"];

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? Object(arguments[i]) : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import classnames from 'classnames';
import React from 'react';
import TouchFeedback from 'rmc-feedback';
import Carousel from '../carousel';
import './index.less';

var Grid = /*#__PURE__*/function (_React$Component) {
  _inherits(Grid, _React$Component);

  var _super = _createSuper(Grid);

  function Grid() {
    var _this;

    _classCallCheck(this, Grid);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));

    _defineProperty(_assertThisInitialized(_this), "state", {
      initialSlideWidth: 0 // only used in carousel model

    });

    _defineProperty(_assertThisInitialized(_this), "renderCarousel", function (rowsArr, pageCount, rowCount) {
      var prefixCls = _this.props.prefixCls;
      var carouselMaxRow = _this.props.carouselMaxRow;
      var pagesArr = [];

      for (var pageIndex = 0; pageIndex < pageCount; pageIndex++) {
        var pageRows = [];

        for (var ii = 0; ii < carouselMaxRow; ii++) {
          var rowIndex = pageIndex * carouselMaxRow + ii;

          if (rowIndex < rowCount) {
            pageRows.push(rowsArr[rowIndex]);
          } else {
            // 空节点为了确保末尾页的最后未到底的行有底线(样式中last-child会没线)
            pageRows.push( /*#__PURE__*/React.createElement("div", {
              key: "gridline-".concat(rowIndex)
            }));
          }
        }

        pagesArr.push( /*#__PURE__*/React.createElement("div", {
          key: "pageitem-".concat(pageIndex),
          className: "".concat(prefixCls, "-carousel-page")
        }, pageRows));
      }

      return pagesArr;
    });

    _defineProperty(_assertThisInitialized(_this), "renderItem", function (dataItem, index, columnNum, renderItem) {
      var prefixCls = _this.props.prefixCls;
      var itemEl = null;

      if (renderItem) {
        itemEl = renderItem(dataItem, index);
      } else if (dataItem) {
        var icon = dataItem.icon,
            text = dataItem.text;
        itemEl = /*#__PURE__*/React.createElement("div", {
          className: "".concat(prefixCls, "-item-inner-content column-num-").concat(columnNum)
        }, React.isValidElement(icon) ? icon : /*#__PURE__*/React.createElement("img", {
          className: "".concat(prefixCls, "-icon"),
          src: icon
        }), /*#__PURE__*/React.createElement("div", {
          className: "".concat(prefixCls, "-text")
        }, text));
      }

      return /*#__PURE__*/React.createElement("div", {
        className: "".concat(prefixCls, "-item-content")
      }, itemEl);
    });

    _defineProperty(_assertThisInitialized(_this), "getRows", function (rowCount, dataLength) {
      var _this$props = _this.props,
          columnNum = _this$props.columnNum,
          data = _this$props.data,
          renderItem = _this$props.renderItem,
          flexPrefixCls = _this$props.flexPrefixCls,
          prefixCls = _this$props.prefixCls,
          _onClick = _this$props.onClick,
          activeStyle = _this$props.activeStyle,
          activeClassName = _this$props.activeClassName,
          itemStyle = _this$props.itemStyle;
      var rowsArr = [];
      var rowWidth = "".concat(100 / columnNum, "%");

      var colStyle = _objectSpread({
        width: rowWidth
      }, itemStyle);

      for (var i = 0; i < rowCount; i++) {
        var rowArr = [];

        var _loop = function _loop(j) {
          var dataIndex = i * columnNum + j;
          var itemEl = void 0;

          if (dataIndex < dataLength) {
            var el = data && data[dataIndex];
            itemEl = /*#__PURE__*/React.createElement(TouchFeedback, {
              key: "griditem-".concat(dataIndex),
              activeClassName: activeClassName || "".concat(prefixCls, "-item-active"),
              activeStyle: activeStyle
            }, /*#__PURE__*/React.createElement("div", {
              className: classnames("".concat(flexPrefixCls, "-item ").concat(prefixCls, "-item")),
              onClick: function onClick() {
                return _onClick && _onClick(el, dataIndex);
              },
              style: colStyle
            }, _this.renderItem(el, dataIndex, columnNum, renderItem)));
          } else {
            itemEl = /*#__PURE__*/React.createElement("div", {
              className: classnames("".concat(flexPrefixCls, "-item ").concat(prefixCls, "-item ").concat(prefixCls, "-null-item")),
              key: "griditem-".concat(dataIndex),
              style: colStyle
            });
          }

          rowArr.push(itemEl);
        };

        for (var j = 0; j < columnNum; j++) {
          _loop(j);
        }

        rowsArr.push( /*#__PURE__*/React.createElement("div", {
          className: classnames(flexPrefixCls, "".concat(flexPrefixCls, "-justify-center "), "".concat(flexPrefixCls, "-align-stretch")),
          key: "gridline-".concat(i)
        }, rowArr));
      }

      return rowsArr;
    });

    return _this;
  }

  _createClass(Grid, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.setState({
        initialSlideWidth: document.documentElement.clientWidth
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _classnames;

      var _this$props2 = this.props,
          prefixCls = _this$props2.prefixCls,
          className = _this$props2.className,
          data = _this$props2.data,
          hasLine = _this$props2.hasLine,
          isCarousel = _this$props2.isCarousel,
          square = _this$props2.square,
          activeStyle = _this$props2.activeStyle,
          activeClassName = _this$props2.activeClassName,
          restProps = _objectWithoutProperties(_this$props2, _excluded);

      var columnNum = restProps.columnNum,
          carouselMaxRow = restProps.carouselMaxRow,
          onClick = restProps.onClick,
          renderItem = restProps.renderItem,
          restPropsForCarousel = _objectWithoutProperties(restProps, _excluded2);

      var initialSlideWidth = this.state.initialSlideWidth;
      var dataLength = data && data.length || 0;
      var rowCount = Math.ceil(dataLength / columnNum);
      var rowsArr;
      var renderEl;
      var showDecorator = false; // 是否有底部轮播条

      if (isCarousel) {
        if (initialSlideWidth < 0) {
          // carousel  server render. because carousel dependes on document
          return null;
        }

        if (rowCount % carouselMaxRow !== 0) {
          rowCount = rowCount + carouselMaxRow - rowCount % carouselMaxRow;
        }

        var pageCount = Math.ceil(rowCount / carouselMaxRow);
        rowsArr = this.getRows(rowCount, dataLength);
        var carouselProps = {};

        if (pageCount <= 1) {
          carouselProps = {
            dots: false,
            dragging: false,
            swiping: false
          };
        } else {
          showDecorator = true;
        }

        renderEl = /*#__PURE__*/React.createElement(Carousel, _extends({
          initialSlideWidth: initialSlideWidth
        }, restPropsForCarousel, carouselProps), this.renderCarousel(rowsArr, pageCount, rowCount));
      } else {
        rowsArr = this.getRows(rowCount, dataLength);
        renderEl = rowsArr;
      }

      var cls = classnames(prefixCls, className, (_classnames = {}, _defineProperty(_classnames, "".concat(prefixCls, "-square"), square), _defineProperty(_classnames, "".concat(prefixCls, "-line"), hasLine), _defineProperty(_classnames, "".concat(prefixCls, "-carousel"), isCarousel), _defineProperty(_classnames, "".concat(prefixCls, "-hasDecorator"), showDecorator), _classnames));
      return /*#__PURE__*/React.createElement("div", {
        className: cls
      }, renderEl);
    }
  }]);

  return Grid;
}(React.Component);

_defineProperty(Grid, "defaultProps", {
  data: [],
  hasLine: true,
  isCarousel: false,
  columnNum: 4,
  carouselMaxRow: 2,
  flexPrefixCls: 'fxm-flexbox',
  prefixCls: 'fxm-grid',
  square: true,
  itemStyle: {}
});

export { Grid as default };