/*
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 { Checkbox } from '../checkbox/index.js';
import { useStyletron } from '../styles/index.js';
import ChevronDown from '../icon/chevron-down.js';
import ChevronUp from '../icon/chevron-up.js';
import { SORT_DIRECTIONS } from './constants.js';
const HeaderCell = React.forwardRef((props, ref) => {
  const [css, theme] = useStyletron();
  const backgroundColor = props.isHovered ? theme.colors.mono200 : theme.colors.mono100;
  return React.createElement("div", {
    ref: ref,
    role: "button",
    tabIndex: "0",
    className: css({ ...theme.typography.font150,
      alignItems: 'center',
      backgroundColor,
      boxSizing: 'border-box',
      cursor: props.sortable ? 'pointer' : null,
      display: props.isMeasured ? 'inline-flex' : 'flex',
      height: '100%',
      paddingLeft: theme.sizing.scale500,
      paddingRight: theme.sizing.scale500,
      flexWrap: 'nowrap',
      whiteSpace: 'nowrap'
    }),
    onMouseEnter: props.onMouseEnter,
    onMouseLeave: props.onMouseLeave,
    onKeyUp: event => {
      if (event.key === 'Enter') {
        props.onSort(props.index);
      }
    },
    onClick: () => props.onSort(props.index)
  }, props.isSelectable && React.createElement(Checkbox, {
    onChange: e => {
      if (props.isSelectedAll || props.isSelectedIndeterminate) {
        props.onSelectNone();
      } else {
        props.onSelectAll();
      }
    },
    checked: props.isSelectedAll || props.isSelectedIndeterminate,
    isIndeterminate: props.isSelectedIndeterminate
  }), props.title, React.createElement("div", {
    className: css({
      position: 'relative',
      width: '100%',
      display: 'flex',
      alignItems: 'center'
    })
  }, (props.isHovered || props.sortDirection) && props.sortable && React.createElement("div", {
    style: {
      backgroundColor,
      display: 'flex',
      alignItems: 'center',
      position: 'absolute',
      right: -4
    }
  }, (props.sortDirection === SORT_DIRECTIONS.DESC || !props.sortDirection) && React.createElement(ChevronDown, {
    color: props.sortDirection ? theme.colors.primary : theme.colors.mono600
  }), props.sortDirection === SORT_DIRECTIONS.ASC && React.createElement(ChevronUp, {
    color: props.sortDirection ? theme.colors.primary : theme.colors.mono600
  }))));
});
HeaderCell.displayName = 'HeaderCell';
export default HeaderCell;