"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = 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 _assets = require("@vincent_za/assets");

var _component = _interopRequireDefault(require("../../Svg/component"));

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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

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 _templateObject7() {
  var data = _taggedTemplateLiteral(["\n  flex-shrink: 0;\n  width: 2rem;\n  height: 2rem;\n  ", ";\n  ", ";\n\n  svg {\n    fill: ", ";\n  };\n\n  &:hover {\n    svg {\n      fill: ", ";\n    };\n  };\n"]);

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

  return data;
}

function _templateObject6() {
  var data = _taggedTemplateLiteral(["\n  display: flex;\n  justify-content: center;\n  align-items: center;\n  height: 100%;\n  width: 3.6rem;\n  font-size: ", ";\n  font-family: ", ";\n  cursor: pointer;\n  color: ", ";\n\n  ", ";\n\n  ", ";\n\n  ", ";\n\n  &:hover {\n    ", ";\n    ", ";\n    ", ";\n  };\n"]);

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

  return data;
}

function _templateObject5() {
  var data = _taggedTemplateLiteral(["\n    display: ", ";\n  "]);

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

  return data;
}

function _templateObject4() {
  var data = _taggedTemplateLiteral(["\n  display: none;\n  justify-content: center;\n  margin-top: ", ";\n  height: 3.6rem;\n  user-select: none;\n\n  ", ";\n"]);

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

  return data;
}

function _templateObject3() {
  var data = _taggedTemplateLiteral(["\n    display: none;\n  "]);

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

  return data;
}

function _templateObject2() {
  var data = _taggedTemplateLiteral(["\n  display: ", ";\n  margin-top: ", ";\n  justify-content: center;\n\n  ", ";\n"]);

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

  return data;
}

function _templateObject() {
  var data = _taggedTemplateLiteral([""]);

  _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 Wrap = _styledComponents.default.div(_templateObject());

var MobileControls = _styledComponents.default.ul(_templateObject2(), function (props) {
  return props.isVisible ? 'flex' : 'none';
}, _styles.REM_UNIT_XLG, _styles.media.sm(_templateObject3()));

var Controls = _styledComponents.default.ul(_templateObject4(), _styles.REM_UNIT_XLG, _styles.media.sm(_templateObject5(), function (props) {
  return props.isVisible ? 'flex' : 'none';
}));

var Control = _styledComponents.default.li(_templateObject6(), _styles.REM_FONT_SIZE_LG, _styles.FONT_BOLD, function (props) {
  return props.isActive ? _styles.LIGHT_SEA_GREEN : _styles.SONIC_SILVER;
}, function (props) {
  return props.isMobile && "\n    margin: 0 ".concat(_styles.REM_UNIT_MD, ";\n    width: auto;\n  ");
}, function (props) {
  return props.isDisabled && "\n    pointer-events: none;\n    opacity: 0.3;\n    cursor: not-allowed;\n  ";
}, function (props) {
  return props.isViewChange && "\n    box-shadow: inset 0px 0px 0 3px ".concat(_styles.CHARLESTON_GREEN, ", inset 0px 0px 0px 4px #5d5d5d;\n  ");
}, function (props) {
  return !props.isActive && "color: ".concat(_styles.GRAY_X11);
}, function (props) {
  return props.isGroup && "\n      svg {\n        fill: ".concat(_styles.GRAY_X11, ";\n      };\n    ");
}, function (props) {
  return props.isViewChange && "\n      box-shadow: inset 0px 0px 0 3px ".concat(_styles.CHARLESTON_GREEN, ", inset 0px 0px 0px 4px ").concat(_styles.GRAY_X11, ";\n    ");
});

var Icon = (0, _styledComponents.default)(_component.default)(_templateObject7(), function (props) {
  return props.isFirst && 'margin-right: -1rem';
}, function (props) {
  return props.isLast && 'margin-left: -1rem';
}, _styles.SONIC_SILVER, _styles.GRAY_X11);

var Pagination =
/*#__PURE__*/
function (_React$Component) {
  _inherits(Pagination, _React$Component);

  function Pagination(props) {
    var _this;

    _classCallCheck(this, Pagination);

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

    _defineProperty(_assertThisInitialized(_this), "onNavigateToPage", function (selectedPageNumber) {
      var numberOfPages = _this.getNumberOfPages();

      var addOne = function addOne(item) {
        return item + 1;
      };

      var removeItems = function removeItems(item, amount) {
        return item >= selectedPageNumber - amount;
      };

      var isViewChange = !_this.pageLinksActive.includes(selectedPageNumber);
      var numberOfPagesFromEnd = numberOfPages - selectedPageNumber;

      if (selectedPageNumber <= 10 && isViewChange) {
        _this.pageLinksActive = _toConsumableArray(Array(10).keys()).map(addOne);
      } else if (selectedPageNumber >= numberOfPages - 9 && isViewChange) {
        _this.pageLinksActive = _toConsumableArray(Array(numberOfPages).keys()).map(addOne).filter(function (i) {
          return removeItems(i, 9 - numberOfPagesFromEnd);
        });
      } else if (isViewChange) {
        _this.pageLinksActive = _toConsumableArray(Array(selectedPageNumber + 5).keys()).map(addOne).filter(function (i) {
          return removeItems(i, 5);
        });
      }

      _this.setState({
        activePageNumber: selectedPageNumber,
        pageLinks: _this.setPageLinks()
      });
    });

    _defineProperty(_assertThisInitialized(_this), "getNumberOfPages", function () {
      var updatedList = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.props.list;
      return Math.ceil(updatedList.length / _this.props.pageLimit);
    });

    _defineProperty(_assertThisInitialized(_this), "setPageLinks", function () {
      var updatedList = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.props.list;

      var updatedPageLinks = _toConsumableArray(new Set([].concat(_toConsumableArray(_this.pageLinksActive), _toConsumableArray(_this.pageLinksExponential))));

      var numberOfPages = _this.getNumberOfPages(updatedList);

      return updatedPageLinks.filter(function (item) {
        return item <= numberOfPages;
      }).sort(function (a, b) {
        return a - b;
      });
    });

    _defineProperty(_assertThisInitialized(_this), "resetDefaultPageLinks", function () {
      _this.pageLinksActive = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
      _this.pageLinksExponential = [20, 40, 80, 160, 320];
    });

    _this.resetDefaultPageLinks();

    _this.state = {
      pageLinks: _this.setPageLinks(),
      activePageNumber: 1
    };
    return _this;
  }

  _createClass(Pagination, [{
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(nextProps) {
      if (nextProps.list.length !== this.props.list.length) {
        this.resetDefaultPageLinks();
        this.setState({
          pageLinks: this.setPageLinks(nextProps.list),
          activePageNumber: 1
        });
      }
    }
  }, {
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate(nextProps, nextState) {
      if (nextState.pageLinks !== this.state.pageLinks) return true;
      if (nextState.activePageNumber !== this.state.activePageNumber) return true;
      if (nextProps.isHidden !== this.props.isHidden) return true;
      if (nextProps.list !== this.props.list) return true;
      if (nextProps.headerRowTemplate !== this.props.headerRowTemplate) return true;
      if (nextProps.contentRowTemplate !== this.props.contentRowTemplate) return true;
      if (nextProps.isInitial !== this.props.isInitial) return true;
      return false;
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$state = this.state,
          pageLinks = _this$state.pageLinks,
          activePageNumber = _this$state.activePageNumber;
      var _this$props = this.props,
          isHidden = _this$props.isHidden,
          list = _this$props.list,
          isInitial = _this$props.isInitial,
          pageLimit = _this$props.pageLimit,
          headerRowTemplate = _this$props.headerRowTemplate,
          contentRowTemplate = _this$props.contentRowTemplate;
      var firstPage = 1;
      var lastPage = this.getNumberOfPages();
      var isLoaded = pageLinks.length > 1;
      return _react.default.createElement(Wrap, null, _react.default.createElement(_index.Table, {
        isHidden: isHidden,
        isInitial: isInitial
      }, headerRowTemplate, list.slice((activePageNumber - 1) * pageLimit, activePageNumber * pageLimit).map(contentRowTemplate)), _react.default.createElement(MobileControls, {
        isVisible: isLoaded && !isHidden
      }, _react.default.createElement(Control, {
        isMobile: true,
        isDisabled: activePageNumber === firstPage,
        onClick: function onClick() {
          return _this2.onNavigateToPage(activePageNumber - 1);
        }
      }, "Previous"), _react.default.createElement(Control, {
        isMobile: true,
        isDisabled: activePageNumber === lastPage,
        onClick: function onClick() {
          return _this2.onNavigateToPage(activePageNumber + 1);
        }
      }, "Next")), _react.default.createElement(Controls, {
        isVisible: isLoaded && !isHidden
      }, _react.default.createElement(Control, {
        isGroup: true,
        isDisabled: activePageNumber === firstPage,
        onClick: function onClick() {
          return _this2.onNavigateToPage(firstPage);
        }
      }, _react.default.createElement(Icon, {
        isFirst: true,
        svg: _assets.chevronLeftSvg
      }), _react.default.createElement(Icon, {
        svg: _assets.chevronLeftSvg
      })), _react.default.createElement(Control, {
        isDisabled: activePageNumber === firstPage,
        onClick: function onClick() {
          return _this2.onNavigateToPage(activePageNumber - 1);
        }
      }, _react.default.createElement(Icon, {
        svg: _assets.chevronLeftSvg
      })), this.state.pageLinks.map(function (pageNumber) {
        return _react.default.createElement(Control, {
          isActive: pageNumber === activePageNumber,
          isViewChange: !_this2.pageLinksActive.includes(pageNumber),
          onClick: function onClick() {
            return _this2.onNavigateToPage(pageNumber);
          },
          key: pageNumber
        }, pageNumber);
      }), _react.default.createElement(Control, {
        isDisabled: activePageNumber === lastPage,
        onClick: function onClick() {
          return _this2.onNavigateToPage(activePageNumber + 1);
        }
      }, _react.default.createElement(Icon, {
        svg: _assets.chevronRightSvg
      })), _react.default.createElement(Control, {
        isGroup: true,
        isDisabled: activePageNumber === lastPage,
        onClick: function onClick() {
          return _this2.onNavigateToPage(lastPage);
        }
      }, _react.default.createElement(Icon, {
        svg: _assets.chevronRightSvg
      }), _react.default.createElement(Icon, {
        isLast: true,
        svg: _assets.chevronRightSvg
      }))));
    }
  }]);

  return Pagination;
}(_react.default.Component);

Pagination.propTypes = {
  isHidden: _propTypes.bool,
  pageLimit: _propTypes.number,
  list: _propTypes.array.isRequired,
  headerRowTemplate: _propTypes.object.isRequired,
  contentRowTemplate: _propTypes.func.isRequired,
  isInitial: _propTypes.bool
};
Pagination.defaultProps = {
  isHidden: false,
  isInitial: false,
  pageLimit: 14
};
var _default = Pagination;
exports.default = _default;