"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _faSortDown = require("@fortawesome/free-solid-svg-icons/faSortDown");
var _faSortUp = require("@fortawesome/free-solid-svg-icons/faSortUp");
var _faSort = require("@fortawesome/free-solid-svg-icons/faSort");
var _fontAwesomeIcon = require("../../../font-awesome-icon");
var _truncateWithTooltip = require("../../../truncate-with-tooltip");
var _sortableTableThButton = _interopRequireDefault(require("./sortable-table-th-button"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const SortableTableTh = ({
  sortColumnName,
  sortDirection,
  onSortChange = () => {},
  column,
  className,
  ThComponent,
  children,
  name,
  buttonTestId,
  ...props
}) => {
  const sortCallback = (0, _react.useCallback)(() => onSortChange(column?.accessor, sortDirection), [onSortChange, column?.accessor]);
  const isActive = sortColumnName === column?.accessor;
  let thProps = {
    ...props,
    className: (0, _classnames.default)(className, 'sortable-table-r-th',
    // sometimes we need to have access to specific column's th
    `${column?.accessor}-col`)
  };
  if (!thProps['test-id']) {
    // set default `test-id` only if it has not been defined
    thProps = {
      ...thProps,
      'test-id': thProps.testId || 'sortable-table-r-th'
    };
  }
  const icon = !column?.disabled && isActive && /*#__PURE__*/(0, _jsxRuntime.jsx)(_fontAwesomeIcon.FontAwesomeIcon, {
    icon: sortDirection ? _faSortUp.faSortUp : _faSortDown.faSortDown,
    className: "sortable-table-order-direction",
    "test-id": "table-sort-direction"
  });
  const inactiveIcon = !column?.disabled && !isActive && /*#__PURE__*/(0, _jsxRuntime.jsx)(_fontAwesomeIcon.FontAwesomeIcon, {
    icon: _faSort.faSort,
    className: "sortable-table-order-direction",
    "test-id": "table-sort-direction-inactive"
  });
  const thChildren = /*#__PURE__*/(0, _jsxRuntime.jsx)(_sortableTableThButton.default, {
    onClick: sortCallback,
    isDisabled: column?.disabled,
    isActive: isActive,
    testId: buttonTestId,
    children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_truncateWithTooltip.TruncateWithTooltip, {
        className: "ellipse cell-text-content",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
          name: name,
          "test-id": "th-cell-text-content",
          children: children
        })
      }), icon || inactiveIcon]
    })
  });
  if (ThComponent) {
    return /*#__PURE__*/_react.default.createElement(ThComponent, {
      ...thProps,
      column
    }, thChildren);
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    "test-id": thProps['test-id'],
    className: (0, _classnames.default)(thProps.className, 'r-th'),
    children: thChildren
  });
};
var _default = exports.default = SortableTableTh;