'use strict';

exports.__esModule = true;
exports.Context = undefined;

var _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; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _helper = require('./Shared/helper.js');

var _CPaginationItem = require('./CPaginationItem');

var _CPaginationItem2 = _interopRequireDefault(_CPaginationItem);

var _CPaginationLink = require('./CPaginationLink');

var _CPaginationLink2 = _interopRequireDefault(_CPaginationLink);

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

var Context = exports.Context = _react2.default.createContext({});

//component - CoreUI / CPagination

var CPagination = function CPagination(props) {
  var _classNames;

  var Tag = props.tag,
      children = props.children,
      className = props.className,
      cssModule = props.cssModule,
      custom = props.custom,
      innerRef = props.innerRef,
      listClassName = props.listClassName,
      pageFrom = props.pageFrom,
      pageTo = props.pageTo,
      pageMin = props.pageMin,
      pageMax = props.pageMax,
      activePage = props.activePage,
      size = props.size,
      firstButtonHtml = props.firstButtonHtml,
      previousButtonHtml = props.previousButtonHtml,
      nextButtonHtml = props.nextButtonHtml,
      lastButtonHtml = props.lastButtonHtml,
      hideDots = props.hideDots,
      hideArrows = props.hideArrows,
      hideDoubleArrows = props.hideDoubleArrows,
      ListTag = props.listTag,
      label = props['aria-label'],
      listProps = props.listProps,
      onClick = props.onClick,
      attributes = _objectWithoutProperties(props, ['tag', 'children', 'className', 'cssModule', 'custom', 'innerRef', 'listClassName', 'pageFrom', 'pageTo', 'pageMin', 'pageMax', 'activePage', 'size', 'firstButtonHtml', 'previousButtonHtml', 'nextButtonHtml', 'lastButtonHtml', 'hideDots', 'hideArrows', 'hideDoubleArrows', 'listTag', 'aria-label', 'listProps', 'onClick']);

  var paginationClick = function paginationClick(e, type, n) {
    onClick && onClick(e, type, n);
  };

  //render

  var classes = (0, _helper.mapToCssModules)((0, _classnames2.default)(className), cssModule);

  var listClasses = (0, _helper.mapToCssModules)((0, _classnames2.default)(listClassName, 'pagination', (_classNames = {}, _classNames['pagination-' + size] = !!size, _classNames)), cssModule);

  var autoChildren = void 0;

  if (!custom) {
    var list = [];
    for (var i = pageFrom; i <= pageTo; i++) {
      list.push(_react2.default.createElement(
        _CPaginationItem2.default,
        { custom: true, key: i, active: activePage == i ? true : false },
        _react2.default.createElement(
          _CPaginationLink2.default,
          { type: 'number', n: i },
          i
        )
      ));
    }var pagesBefore = pageFrom > pageMin;
    var pagesAfter = pageTo < pageMax;
    autoChildren = _react2.default.createElement(
      _react2.default.Fragment,
      null,
      pagesBefore && firstButtonHtml ? _react2.default.createElement(
        _CPaginationItem2.default,
        { custom: true },
        _react2.default.createElement(
          _CPaginationLink2.default,
          { type: 'first' },
          firstButtonHtml
        )
      ) : '',
      pagesBefore && previousButtonHtml ? _react2.default.createElement(
        _CPaginationItem2.default,
        { custom: true },
        _react2.default.createElement(
          _CPaginationLink2.default,
          { type: 'previous' },
          previousButtonHtml
        )
      ) : '',
      !hideDots && pagesBefore ? _react2.default.createElement(
        _CPaginationItem2.default,
        { custom: true },
        _react2.default.createElement(
          _CPaginationLink2.default,
          { type: 'less' },
          '...'
        )
      ) : '',
      list,
      !hideDots && pagesAfter ? _react2.default.createElement(
        _CPaginationItem2.default,
        { custom: true },
        _react2.default.createElement(
          _CPaginationLink2.default,
          { type: 'more' },
          '...'
        )
      ) : '',
      pagesAfter && nextButtonHtml ? _react2.default.createElement(
        _CPaginationItem2.default,
        { custom: true },
        _react2.default.createElement(
          _CPaginationLink2.default,
          { type: 'next' },
          nextButtonHtml
        )
      ) : '',
      pagesAfter && lastButtonHtml ? _react2.default.createElement(
        _CPaginationItem2.default,
        { custom: true },
        _react2.default.createElement(
          _CPaginationLink2.default,
          { type: 'last' },
          lastButtonHtml
        )
      ) : ''
    );
  }

  return _react2.default.createElement(
    Context.Provider,
    { value: {
        paginationClick: paginationClick
      } },
    _react2.default.createElement(
      Tag,
      _extends({}, attributes, { className: classes, 'aria-label': label, ref: innerRef }),
      _react2.default.createElement(ListTag, _extends({ className: listClasses, children: autoChildren || children }, listProps))
    )
  );
};

CPagination.propTypes = process.env.NODE_ENV !== "production" ? {
  tag: _helper.tagPropType,
  children: _propTypes2.default.node,
  className: _propTypes2.default.string,
  cssModule: _propTypes2.default.object,
  custom: _propTypes2.default.bool,
  //
  innerRef: _propTypes2.default.oneOfType([_propTypes2.default.object, _propTypes2.default.func, _propTypes2.default.string]),
  pageMin: _propTypes2.default.number,
  pageMax: _propTypes2.default.number,
  pageFrom: _propTypes2.default.number,
  pageTo: _propTypes2.default.number,
  activePage: _propTypes2.default.number,
  size: _propTypes2.default.string,
  listTag: _helper.tagPropType,
  hideDots: _propTypes2.default.bool,
  hideArrows: _propTypes2.default.bool,
  hideDoubleArrows: _propTypes2.default.bool,
  firstButtonHtml: _propTypes2.default.oneOfType([_propTypes2.default.object, _propTypes2.default.string]),
  previousButtonHtml: _propTypes2.default.oneOfType([_propTypes2.default.object, _propTypes2.default.string]),
  nextButtonHtml: _propTypes2.default.oneOfType([_propTypes2.default.object, _propTypes2.default.string]),
  lastButtonHtml: _propTypes2.default.oneOfType([_propTypes2.default.object, _propTypes2.default.string]),
  'aria-label': _propTypes2.default.string,
  listClassName: _propTypes2.default.string,
  listProps: _propTypes2.default.object,
  onClick: _propTypes2.default.func
} : {};

CPagination.defaultProps = {
  tag: 'nav',
  listTag: 'ul',
  'aria-label': 'pagination',
  pageFrom: 2,
  pageTo: 3,
  pageMin: 1,
  pageMax: 5,
  activePage: 2,
  firstButtonHtml: _react2.default.createElement(
    _react2.default.Fragment,
    null,
    '\xAB'
  ),
  previousButtonHtml: _react2.default.createElement(
    _react2.default.Fragment,
    null,
    '\u2039'
  ),
  nextButtonHtml: _react2.default.createElement(
    _react2.default.Fragment,
    null,
    '\u203A'
  ),
  lastButtonHtml: _react2.default.createElement(
    _react2.default.Fragment,
    null,
    '\xBB'
  )
};

exports.default = CPagination;