"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
exports.__esModule = true;
exports["default"] = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _system = require("@wix/wix-ui-icons-common/system");
var _constants = require("./constants");
var _TableListHeaderSt = require("./TableListHeader.st.css.js");
var _Checkbox = _interopRequireDefault(require("../Checkbox"));
var _InfoIcon = _interopRequireDefault(require("../InfoIcon"));
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/TableListHeader/TableListHeader.jsx",
  _this = void 0;
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(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 (var _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); }
var ALIGN = {
  left: 'left',
  center: 'center',
  right: 'right'
};
var CHECKBOX_STATE = {
  normal: 'normal',
  checked: 'checked',
  indeterminate: 'indeterminate',
  hasError: 'hasError',
  disabled: 'disabled',
  hidden: 'hidden'
};
var getWidthStyle = function getWidthStyle(options) {
  return options.reduce(function (acc, _ref) {
    var width = _ref.width;
    return "".concat(acc, " ").concat(typeof width === 'number' ? width + 'px' : width || '1fr');
  }, '');
};

/** TableListHeader */
var TableListHeader = /*#__PURE__*/(0, _react.forwardRef)(function (_ref2, ref) {
  var _ref2$options = _ref2.options,
    options = _ref2$options === void 0 ? [] : _ref2$options,
    _ref2$checkboxState = _ref2.checkboxState,
    checkboxState = _ref2$checkboxState === void 0 ? CHECKBOX_STATE.hidden : _ref2$checkboxState,
    _ref2$onCheckboxChang = _ref2.onCheckboxChange,
    onCheckboxChange = _ref2$onCheckboxChang === void 0 ? function () {} : _ref2$onCheckboxChang,
    _ref2$onSortChange = _ref2.onSortChange,
    onSortChange = _ref2$onSortChange === void 0 ? function () {} : _ref2$onSortChange,
    className = _ref2.className,
    dataHook = _ref2.dataHook;
  var onColumnClick = (0, _react.useCallback)(function (sortable, colNum) {
    return function (evt) {
      sortable && onSortChange && onSortChange(colNum, evt);
    };
  }, [onSortChange]);
  return /*#__PURE__*/_react["default"].createElement("div", {
    ref: ref,
    className: (0, _TableListHeaderSt.st)(_TableListHeaderSt.classes.root, className),
    "data-hook": dataHook,
    __self: _this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 55,
      columnNumber: 7
    }
  }, checkboxState !== CHECKBOX_STATE.hidden && /*#__PURE__*/_react["default"].createElement(_Checkbox["default"], {
    dataHook: _constants.dataHooks.tableListHeaderCheckbox,
    checked: checkboxState === CHECKBOX_STATE.checked,
    indeterminate: checkboxState === CHECKBOX_STATE.indeterminate,
    hasError: checkboxState === CHECKBOX_STATE.hasError,
    disabled: checkboxState === CHECKBOX_STATE.disabled,
    selectionArea: "none",
    selectionAreaSkin: "filled",
    selectionAreaPadding: "13px 12px 13px 6px",
    onChange: onCheckboxChange,
    __self: _this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 61,
      columnNumber: 11
    }
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: _TableListHeaderSt.classes.optionsContainer,
    style: {
      gridTemplateColumns: getWidthStyle(options)
    },
    "data-hook": _constants.dataHooks.tableListHeaderOptionsContainer,
    __self: _this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 74,
      columnNumber: 9
    }
  }, options.map(function (_ref3, index) {
    var value = _ref3.value,
      align = _ref3.align,
      sortable = _ref3.sortable,
      sortDescending = _ref3.sortDescending,
      infoTooltipProps = _ref3.infoTooltipProps;
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: (0, _TableListHeaderSt.st)(_TableListHeaderSt.classes.option, {
        position: ALIGN[align],
        sortable: sortable
      }),
      key: index,
      "data-hook": _constants.dataHooks.tableListHeaderValue,
      onClick: onColumnClick(sortable, index),
      __self: _this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 86,
        columnNumber: 15
      }
    }, value, /*#__PURE__*/_react["default"].createElement(TableListHeaderSortingArrow, {
      sortDescending: sortDescending,
      __self: _this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 96,
        columnNumber: 17
      }
    }), /*#__PURE__*/_react["default"].createElement(TableListHeaderInfoTooltip, {
      tooltipProps: infoTooltipProps,
      __self: _this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 97,
        columnNumber: 17
      }
    }));
  })));
});
TableListHeader.displayName = 'TableListHeader';
var _default = exports["default"] = TableListHeader;
var TableListHeaderSortingArrow = function TableListHeaderSortingArrow(_ref4) {
  var sortDescending = _ref4.sortDescending;
  if (sortDescending === undefined) {
    return null;
  }
  var Arrow = sortDescending ? _system.SortByArrowDown : _system.SortByArrowUp;
  var dataHook = sortDescending ? _constants.dataHooks.tableListHeaderArrowDescending : _constants.dataHooks.tableListHeaderArrowAscending;
  return /*#__PURE__*/_react["default"].createElement(Arrow, {
    "data-hook": dataHook,
    className: _TableListHeaderSt.classes.sortArrow,
    height: 12,
    __self: _this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 122,
      columnNumber: 5
    }
  });
};
var TableListHeaderInfoTooltip = function TableListHeaderInfoTooltip(_ref5) {
  var tooltipProps = _ref5.tooltipProps;
  if (tooltipProps === undefined) {
    return null;
  }
  return /*#__PURE__*/_react["default"].createElement(_InfoIcon["default"], (0, _extends2["default"])({
    dataHook: _constants.dataHooks.tableListHeaderInfoIcon
  }, tooltipProps, {
    __self: _this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 132,
      columnNumber: 5
    }
  }));
};