"use strict";

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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _classnames2 = _interopRequireDefault(require("classnames"));

var _filter = _interopRequireDefault(require("./filter"));

var _sort = _interopRequireDefault(require("./sort"));

var _cell = _interopRequireDefault(require("./cell"));

var _resize = _interopRequireDefault(require("./resize"));

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

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

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 _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 _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

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

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

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 _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; }

var noop = function noop() {};

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

  function Header() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, Header);

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

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Header)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_this), "getCellRef", function (i, j, cell) {
      _this.props.headerCellRef(i, j, cell);
    });

    _defineProperty(_assertThisInitialized(_this), "onSort", function (dataIndex, order, sort) {
      _this.props.onSort(dataIndex, order, sort);
    });

    return _this;
  }

  _createClass(Header, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      /* eslint-disable no-unused-vars */
      var _this$props = this.props,
          prefix = _this$props.prefix,
          className = _this$props.className,
          children = _this$props.children,
          Tag = _this$props.component,
          colGroup = _this$props.colGroup,
          columns = _this$props.columns,
          locale = _this$props.locale,
          filterParams = _this$props.filterParams,
          onFilter = _this$props.onFilter,
          components = _this$props.components,
          affixRef = _this$props.affixRef,
          headerCellRef = _this$props.headerCellRef,
          onSort = _this$props.onSort,
          sort = _this$props.sort,
          sortIcons = _this$props.sortIcons,
          onResizeChange = _this$props.onResizeChange,
          pure = _this$props.pure,
          rtl = _this$props.rtl,
          table = _this$props.table,
          others = _objectWithoutProperties(_this$props, ["prefix", "className", "children", "component", "colGroup", "columns", "locale", "filterParams", "onFilter", "components", "affixRef", "headerCellRef", "onSort", "sort", "sortIcons", "onResizeChange", "pure", "rtl", "table"]);

      delete others.table;
      var _this$context = this.context,
          _this$context$selecte = _this$context.selectedRowKeys,
          selectedRowKeys = _this$context$selecte === void 0 ? [] : _this$context$selecte,
          rowSelection = _this$context.rowSelection;
      var _components$Cell = components.Cell,
          Cell = _components$Cell === void 0 ? _cell["default"] : _components$Cell,
          _components$Filter = components.Filter,
          Filter = _components$Filter === void 0 ? _filter["default"] : _components$Filter,
          _components$Sort = components.Sort,
          Sort = _components$Sort === void 0 ? _sort["default"] : _components$Sort,
          _components$Resize = components.Resize,
          Resize = _components$Resize === void 0 ? _resize["default"] : _components$Resize;
      var rowSpan = columns.length;
      var header = columns.map(function (cols, index) {
        var col = (selectedRowKeys.length && rowSelection.titleAddons ? cols.slice(0, 1) : cols).map(function (col, j) {
          var _classnames;

          /* eslint-disable no-unused-vars, prefer-const */
          var title = col.title,
              colSpan = col.colSpan,
              sortable = col.sortable,
              initialOrder = col.initialOrder,
              resizable = col.resizable,
              dataIndex = col.dataIndex,
              filters = col.filters,
              filterMode = col.filterMode,
              filterMenuProps = col.filterMenuProps,
              width = col.width,
              align = col.align,
              alignHeader = col.alignHeader,
              className = col.className,
              __normalized = col.__normalized,
              lock = col.lock,
              columnId = col.columnId,
              others = _objectWithoutProperties(col, ["title", "colSpan", "sortable", "initialOrder", "resizable", "dataIndex", "filters", "filterMode", "filterMenuProps", "width", "align", "alignHeader", "className", "__normalized", "lock", "columnId"]);

          className = (0, _classnames2["default"])((_classnames = {}, _defineProperty(_classnames, "".concat(prefix, "table-header-node"), true), _defineProperty(_classnames, "".concat(prefix, "table-header-resizable"), resizable), _defineProperty(_classnames, className, className), _classnames));
          var attrs = {};
          var sortElement;
          var filterElement;
          var resizeElement;

          if (col.children && col.children.length) {
            attrs.colSpan = colSpan;
          } else {
            if (sortable) {
              sortElement = _react["default"].createElement(Sort, {
                prefix: prefix,
                dataIndex: dataIndex,
                onSort: _this2.onSort,
                sortIcons: sortIcons,
                initialOrder: initialOrder,
                sort: sort,
                rtl: rtl,
                locale: locale
              });
            }

            if (resizable) {
              resizeElement = _react["default"].createElement(Resize, {
                prefix: prefix,
                rtl: rtl,
                dataIndex: dataIndex,
                onChange: onResizeChange
              });
            }

            if (filters) {
              filterElement = filters.length ? _react["default"].createElement(Filter, {
                dataIndex: dataIndex,
                filters: filters,
                prefix: prefix,
                locale: locale,
                rtl: rtl,
                filterParams: filterParams,
                filterMode: filterMode,
                filterMenuProps: filterMenuProps,
                onFilter: onFilter
              }) : null;
            }

            attrs.rowSpan = rowSpan - index;
          }

          if (selectedRowKeys.length && rowSelection.titleAddons) {
            attrs.colSpan = columns[0].length;
          }

          return _react["default"].createElement(Cell, _extends({}, others, attrs, {
            table: table,
            isInHeader: true,
            key: j,
            prefix: prefix,
            pure: pure,
            rtl: rtl,
            cell: title,
            component: "th",
            align: alignHeader || align,
            className: className,
            ref: _this2.getCellRef.bind(_this2, index, j),
            columnId: columnId,
            type: "header"
          }), sortElement, filterElement, resizeElement);
        });
        return _react["default"].createElement("tr", {
          key: index
        }, col);
      });
      return _react["default"].createElement(Tag, _extends({
        className: className
      }, others), header, children);
    }
  }]);

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

exports["default"] = Header;

_defineProperty(Header, "propTypes", {
  children: _propTypes["default"].any,
  prefix: _propTypes["default"].string,
  pure: _propTypes["default"].bool,
  className: _propTypes["default"].string,
  component: _propTypes["default"].string,
  columns: _propTypes["default"].array,
  colGroup: _propTypes["default"].object,
  headerCellRef: _propTypes["default"].func,
  locale: _propTypes["default"].object,
  filterParams: _propTypes["default"].object,
  onFilter: _propTypes["default"].func,
  components: _propTypes["default"].object,
  sort: _propTypes["default"].object,
  sortIcons: _propTypes["default"].object,
  onSort: _propTypes["default"].func,
  onResizeChange: _propTypes["default"].func,
  table: _propTypes["default"].object
});

_defineProperty(Header, "defaultProps", {
  component: 'thead',
  columns: [],
  headerCellRef: noop,
  onFilter: noop,
  components: {},
  onSort: noop,
  onResizeChange: noop,
  table: null
});

_defineProperty(Header, "contextTypes", {
  rowSelection: _propTypes["default"].object,
  selectedRowKeys: _propTypes["default"].array
});