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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

import React from 'react';
import { LinkDown } from 'grommet-icons/es6/icons/LinkDown';
import { LinkUp } from 'grommet-icons/es6/icons/LinkUp';
import { Blank } from 'grommet-icons/es6/icons/Blank';
import { Box } from 'grommet/es6/components/Box';
import { Button } from 'grommet/es6/components/Button';

import { StyledThComponent } from '../StyledPagingTable';

export default (function (_ref) {
  var toggleSort = _ref.toggleSort,
      sort = _ref.sort,
      resizable = _ref.resizable,
      children = _ref.children,
      pivot = _ref.pivot,
      hidden = _ref.hidden,
      sortable = _ref.sortable,
      expander = _ref.expander,
      CellTextComponent = _ref.CellTextComponent,
      props = _objectWithoutProperties(_ref, ['toggleSort', 'sort', 'resizable', 'children', 'pivot', 'hidden', 'sortable', 'expander', 'CellTextComponent']);

  if (!expander && Array.isArray(children) && children.length > 1 && !children[0] && !children[1]) {
    return null;
  }

  var style = props.style,
      rest = _objectWithoutProperties(props, ['style']);

  var childRendered = React.createElement(CellTextComponent, _extends({}, rest, { value: children }));
  var content = void 0;
  if (sortable) {
    var sortAsc = sort === '-sort-asc';
    var sortDesc = sort === '-sort-desc';
    var SortIcon = Blank;
    if (sortAsc || sortDesc) {
      SortIcon = sortAsc ? LinkUp : LinkDown;
    }
    content = React.createElement(
      Box,
      { direction: 'row', gap: 'xsmall', align: 'center' },
      childRendered,
      React.createElement(
        Button,
        { onClick: function onClick(e) {
            return toggleSort && toggleSort(e);
          } },
        React.createElement(SortIcon, null)
      )
    );
  } else {
    content = childRendered;
  }

  var boxProps = _extends({ pad: 'xsmall' }, rest);
  return (

    // eslint-disable-next-line jsx-a11y/interactive-supports-focus
    React.createElement(
      StyledThComponent,
      {
        role: 'columnheader',
        onClick: function onClick(e) {
          return toggleSort && toggleSort(e);
        },
        direction: 'row',
        pivot: pivot,
        hidden: hidden,
        sortable: sortable,
        style: style,
        resizable: resizable
      },
      React.createElement(
        Box,
        _extends({}, boxProps, { fill: true }),
        content
      )
    )
  );
});