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

/*
Copyright (c) 2018-2019 Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
import * as React from 'react';
import { getOverrides } from '../helpers/overrides.js';
import TriangleDown from '../icon/triangle-down.js';
import TriangleUp from '../icon/triangle-up.js';
import { SORT_DIRECTION } from './constants.js';
import { StyledHeadCell, StyledSortableLabel } from './styled-components.js';

function SortDirectionIcon({
  direction
}) {
  switch (direction) {
    case SORT_DIRECTION.ASC:
      return React.createElement(TriangleDown, null);

    case SORT_DIRECTION.DESC:
      return React.createElement(TriangleUp, null);

    default:
      return null;
  }
}

export default function SortableHeadCell(props) {
  const {
    overrides = {},
    fillClickTarget,
    disabled
  } = props;
  const [HeadCell, headCellProps] = getOverrides(overrides.HeadCell, StyledHeadCell);
  const [SortableLabel, sortableLabelProps] = getOverrides(overrides.SortableLabel, StyledSortableLabel);

  const onClick = () => {
    props.onSort && props.onSort();
  };

  const enableHeadClick = fillClickTarget && !disabled;
  return React.createElement(HeadCell, _extends({
    role: "columnheader"
  }, headCellProps, {
    $cursor: enableHeadClick ? 'pointer' : undefined,
    onClick: enableHeadClick ? onClick : undefined
  }), React.createElement(SortableLabel, _extends({
    "aria-label": `sorts table by ${props.title} column`,
    disabled: disabled,
    onClick: !fillClickTarget ? onClick : undefined
  }, sortableLabelProps), React.createElement(SortDirectionIcon, {
    direction: props.direction
  }), props.title), props.children);
}