"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.TableStyled = void 0;

var _react = _interopRequireDefault(require("react"));

var _propTypes = require("prop-types");

var _styledComponents = _interopRequireDefault(require("styled-components"));

var _styles = require("@vincent_za/styles");

var _form = require("../../form");

var _index = require("../index");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

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

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _templateObject2() {
  var data = _taggedTemplateLiteral(["\n    font-size: ", ";\n  "]);

  _templateObject2 = function _templateObject2() {
    return data;
  };

  return data;
}

function _templateObject() {
  var data = _taggedTemplateLiteral(["\n  display: ", ";\n  width: 100%;\n  font-size: ", ";\n  color: ", ";\n  line-height: normal;\n\n  ", ";\n"]);

  _templateObject = function _templateObject() {
    return data;
  };

  return data;
}

function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }

var TableStyled = _styledComponents.default.table(_templateObject(), function (props) {
  return props.isInitial || props.isHidden ? 'none' : 'table';
}, _styles.REM_FONT_SIZE__SM, _styles.GRAY_X11, _styles.media.sm(_templateObject2(), _styles.REM_FONT_SIZE_NORMAL));

exports.TableStyled = TableStyled;

function tableHOC(WrappedComponent) {
  var _class, _temp;

  return _temp = _class =
  /*#__PURE__*/
  function (_React$Component) {
    _inherits(Table, _React$Component);

    function Table(props) {
      var _this;

      _classCallCheck(this, Table);

      _this = _possibleConstructorReturn(this, _getPrototypeOf(Table).call(this, props));

      _defineProperty(_assertThisInitialized(_this), "onSort", function (_ref) {
        var _ref$keyToSort = _ref.keyToSort,
            keyToSort = _ref$keyToSort === void 0 ? _this.state.keyToSort : _ref$keyToSort;
        var list = _this.state.list;

        var tableHeaders = _this.state.tableHeaders.map(function (header) {
          if (header.keyToSort === keyToSort) {
            return _objectSpread({}, header, {
              asc: !header.asc,
              isSorted: true
            });
          }

          return _objectSpread({}, header, {
            asc: false,
            isSorted: false
          });
        });

        _this.setState({
          tableHeaders: tableHeaders,
          list: _this.sorting(tableHeaders, list, keyToSort),
          keyToSort: keyToSort
        });
      });

      _this.state = {
        tableHeaders: props.tableHeaders,
        list: props.list,
        keyToSort: undefined
      };

      _this.sorting = function (tableHeaders, list, keyToSort) {
        return list.sort(function (a, b) {
          if (!keyToSort) return 0;
          var sortAscending = tableHeaders.find(function (header) {
            return header.keyToSort === keyToSort;
          }).asc;
          if (a[keyToSort] > b[keyToSort]) return sortAscending ? 1 : -1;
          if (a[keyToSort] < b[keyToSort]) return sortAscending ? -1 : 1;
          return 0;
        });
      };

      return _this;
    }

    _createClass(Table, [{
      key: "shouldComponentUpdate",
      value: function shouldComponentUpdate(nextProps, nextState) {
        if (nextState.tableHeaders !== this.state.tableHeaders) return true;
        if (nextState.keyToSort !== this.state.keyToSort) return true;
        if (nextState.list !== this.state.list) return true;
        if (nextProps.isChecked !== this.props.isChecked) return true;
        if (nextProps.tableHeaders !== this.props.tableHeaders) return true;
        if (nextProps.list !== this.props.list) return true;
        if (nextProps.isInitial !== this.props.isInitial) return true;
        if (nextProps.isHidden !== this.props.isHidden) return true;
        return false;
      }
    }, {
      key: "componentDidUpdate",
      value: function componentDidUpdate(prevProps) {
        var _this$state = this.state,
            keyToSort = _this$state.keyToSort,
            tableHeaders = _this$state.tableHeaders,
            list = this.props.list;

        if (prevProps.list !== list) {
          this.setState({
            list: this.sorting(tableHeaders, list, keyToSort)
          });
        }
      }
    }, {
      key: "render",
      value: function render() {
        var _this2 = this;

        var tableHeaders = this.state.tableHeaders;
        var _this$props = this.props,
            isMinimal = _this$props.isMinimal,
            isNoHeader = _this$props.isNoHeader,
            id = _this$props.id,
            isChecked = _this$props.isChecked,
            onChange = _this$props.onChange,
            isInitial = _this$props.isInitial,
            isHidden = _this$props.isHidden,
            className = _this$props.className;
        return _react.default.createElement(TableStyled, {
          isMinimal: isMinimal,
          isInitial: isInitial,
          isHidden: isHidden,
          className: className
        }, _react.default.createElement("tbody", null, !isNoHeader && _react.default.createElement(_index.Row, {
          isHeader: true
        }, tableHeaders.map(function (_ref2) {
          var isCheckboxWrap = _ref2.isCheckboxWrap,
              title = _ref2.title,
              isSorted = _ref2.isSorted,
              isSortable = _ref2.isSortable,
              asc = _ref2.asc,
              keyToSort = _ref2.keyToSort,
              noTitleKey = _ref2.noTitleKey;
          return _react.default.createElement(_index.Cell, {
            title: title,
            isHeader: true,
            isSorted: isSorted,
            isSortable: isSortable,
            asc: asc,
            onClick: function onClick() {
              return keyToSort ? _this2.onSort({
                keyToSort: keyToSort
              }) : false;
            },
            key: title || noTitleKey
          }, isCheckboxWrap && _react.default.createElement(_form.CheckboxField, {
            id: id,
            isChecked: isChecked,
            onChange: onChange
          }));
        })), _react.default.createElement(WrappedComponent, _extends({}, this.props, this.state))));
      }
    }]);

    return Table;
  }(_react.default.Component), _defineProperty(_class, "propTypes", {
    id: _propTypes.string,
    isChecked: _propTypes.bool,
    tableHeaders: (0, _propTypes.arrayOf)((0, _propTypes.shape)({})),
    list: (0, _propTypes.arrayOf)((0, _propTypes.shape)({})).isRequired,
    isMinimal: _propTypes.bool,
    isNoHeader: _propTypes.bool,
    isInitial: _propTypes.bool,
    isHidden: _propTypes.bool,
    onChange: _propTypes.func,
    className: _propTypes.string
  }), _defineProperty(_class, "defaultProps", {
    isMinimal: false,
    isNoHeader: false,
    id: undefined,
    isChecked: false,
    isInitial: false,
    isHidden: false,
    tableHeaders: [],
    onChange: function onChange() {},
    className: undefined
  }), _temp;
}

var _default = tableHOC;
exports.default = _default;