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

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

import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { tagPropType, mapToCssModules } from './Shared/helper.js';
import CPaginationItem from './CPaginationItem';
import CPaginationLink from './CPaginationLink';

export var Context = React.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 = mapToCssModules(classNames(className), cssModule);

  var listClasses = mapToCssModules(classNames(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(React.createElement(
        CPaginationItem,
        { custom: true, key: i, active: activePage == i ? true : false },
        React.createElement(
          CPaginationLink,
          { type: 'number', n: i },
          i
        )
      ));
    }var pagesBefore = pageFrom > pageMin;
    var pagesAfter = pageTo < pageMax;
    autoChildren = React.createElement(
      React.Fragment,
      null,
      pagesBefore && firstButtonHtml ? React.createElement(
        CPaginationItem,
        { custom: true },
        React.createElement(
          CPaginationLink,
          { type: 'first' },
          firstButtonHtml
        )
      ) : '',
      pagesBefore && previousButtonHtml ? React.createElement(
        CPaginationItem,
        { custom: true },
        React.createElement(
          CPaginationLink,
          { type: 'previous' },
          previousButtonHtml
        )
      ) : '',
      !hideDots && pagesBefore ? React.createElement(
        CPaginationItem,
        { custom: true },
        React.createElement(
          CPaginationLink,
          { type: 'less' },
          '...'
        )
      ) : '',
      list,
      !hideDots && pagesAfter ? React.createElement(
        CPaginationItem,
        { custom: true },
        React.createElement(
          CPaginationLink,
          { type: 'more' },
          '...'
        )
      ) : '',
      pagesAfter && nextButtonHtml ? React.createElement(
        CPaginationItem,
        { custom: true },
        React.createElement(
          CPaginationLink,
          { type: 'next' },
          nextButtonHtml
        )
      ) : '',
      pagesAfter && lastButtonHtml ? React.createElement(
        CPaginationItem,
        { custom: true },
        React.createElement(
          CPaginationLink,
          { type: 'last' },
          lastButtonHtml
        )
      ) : ''
    );
  }

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

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

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

export default CPagination;