'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _recompose = require('recompose');

var _styledComponents = require('styled-components');

var _styledComponents2 = _interopRequireDefault(_styledComponents);

var _changeCase = require('change-case');

var _Anchor = require('../Anchor');

var _Anchor2 = _interopRequireDefault(_Anchor);

var _TableHeaderSortArrowIcon = require('./styles/TableHeaderSortArrowIcon');

var _TableHeaderSortArrowIcon2 = _interopRequireDefault(_TableHeaderSortArrowIcon);

var _TableHeaderStyles = require('./styles/TableHeaderStyles');

var _TableHeaderStyles2 = _interopRequireDefault(_TableHeaderStyles);

var _TableHeaderSortArrows = require('./styles/TableHeaderSortArrows');

var _TableHeaderSortArrows2 = _interopRequireDefault(_TableHeaderSortArrows);

var _TableHeaderColumnName = require('./styles/TableHeaderColumnName');

var _TableHeaderColumnName2 = _interopRequireDefault(_TableHeaderColumnName);

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

var TableHeader = function (_React$Component) {
  (0, _inherits3.default)(TableHeader, _React$Component);

  function TableHeader() {
    var _ref;

    var _temp, _this, _ret;

    (0, _classCallCheck3.default)(this, TableHeader);

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

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = TableHeader.__proto__ || (0, _getPrototypeOf2.default)(TableHeader)).call.apply(_ref, [this].concat(args))), _this), _this.createClickHandler = function (naturalOrder) {
      return function () {
        return _this.props.onClick ? _this.props.onClick(naturalOrder) : _this.props.handleClick(naturalOrder);
      };
    }, _this.renderColumnName = function () {
      var _this$props = _this.props,
          sortable = _this$props.sortable,
          children = _this$props.children,
          sortOrder = _this$props.sortOrder,
          Anchor = _this$props.Anchor,
          ColumnName = _this$props.ColumnName;


      if (sortable) {
        return _react2.default.createElement(
          Anchor,
          {
            type: 'text',
            onClick: _this.createClickHandler(sortOrder === 0 ? 1 : -sortOrder)
          },
          _react2.default.createElement(
            ColumnName,
            { sortable: true },
            children
          )
        );
      }

      return _react2.default.createElement(
        ColumnName,
        null,
        children
      );
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(TableHeader, [{
    key: 'render',
    value: function render() {
      var _props = this.props,
          sortable = _props.sortable,
          sortOrder = _props.sortOrder,
          handleClick = _props.handleClick,
          onClick = _props.onClick,
          id = _props.id,
          className = _props.className,
          Styles = _props.Styles,
          SortArrows = _props.SortArrows,
          Anchor = _props.Anchor,
          Icon = _props.Icon;


      return _react2.default.createElement(
        Styles,
        { sortable: sortable, className: className, id: id },
        this.renderColumnName(),
        sortable && _react2.default.createElement(
          SortArrows,
          { sortOrder: sortOrder },
          _react2.default.createElement(
            Anchor,
            { type: 'icon', onClick: this.createClickHandler(1) },
            _react2.default.createElement(Icon, { name: 'up' })
          ),
          _react2.default.createElement(
            Anchor,
            { type: 'icon', onClick: this.createClickHandler(-1) },
            _react2.default.createElement(Icon, { name: 'down' })
          )
        )
      );
    }
  }]);
  return TableHeader;
}(_react2.default.Component);

TableHeader.propTypes = {
  /**
   * Contents of the header cell.
   */
  children: _propTypes2.default.node.isRequired,
  /**
   * [Deprecated] use onClick
   */
  handleClick: _propTypes2.default.func,
  /**
   * Called when the user clicks the header cell.
   */
  onClick: _propTypes2.default.func,
  /**
   * Specifies the sorting order of this column. [-1, 0, 1] for descending, none, or ascending.
   */
  sortOrder: _propTypes2.default.number,
  /**
   * Indicates whether this column is sortable. This changes how it's rendered.
   */
  sortable: _propTypes2.default.bool,
  /**
   * Adds a class name to the element.
   */
  className: _propTypes2.default.string,
  /**
   * Adds an id to the element.
   */
  id: _propTypes2.default.string,
  /**
   * A component for rendering an icon, used for sort arrows.
   */
  Icon: _propTypes2.default.func,
  /**
   * An anchor component. Defaults Anchor
   */
  Anchor: _propTypes2.default.func,
  /**
   * A component for rendering the th element styles
   */
  Styles: _propTypes2.default.func,
  /**
   * A component for rendering the sort arrow container
   */
  SortArrows: _propTypes2.default.func,
  /**
   * A component for rendering the column name text
   */
  ColumnName: _propTypes2.default.func
};
TableHeader.defaultProps = {
  sortable: false,
  sortOrder: 0,
  handleClick: function handleClick() {
    return null;
  },
  onClick: function onClick() {
    return null;
  },
  className: null,
  id: null,
  Icon: _TableHeaderSortArrowIcon2.default,
  Anchor: _Anchor2.default,
  Styles: _TableHeaderStyles2.default,
  SortArrows: _TableHeaderSortArrows2.default,
  ColumnName: _TableHeaderColumnName2.default
};


TableHeader.Small = (0, _recompose.withProps)({ Styles: _TableHeaderStyles2.default.Small })(TableHeader);

exports.default = TableHeader;