'use strict';

Object.defineProperty(exports, "__esModule", {
	value: true
});

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _uuid = require('uuid');

var _uuid2 = _interopRequireDefault(_uuid);

var _reactBootstrap = require('react-bootstrap');

var _Icon = require('../../../Icon');

var _Icon2 = _interopRequireDefault(_Icon);

var _Pagination = require('./Pagination.scss');

var _Pagination2 = _interopRequireDefault(_Pagination);

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

var FIRST = 'first';
var PREV = 'prev';
var NEXT = 'next';
var LAST = 'last';

var getItemsPerPageTitle = function getItemsPerPageTitle(option) {
	return option > 0 ? option : 'All';
};

var getMenuItem = function getMenuItem(option, index) {
	return _react2.default.createElement(
		_reactBootstrap.MenuItem,
		{ key: index, eventKey: option },
		getItemsPerPageTitle(option)
	);
};

function Pagination(_ref) {
	var id = _ref.id,
	    startIndex = _ref.startIndex,
	    itemsPerPage = _ref.itemsPerPage,
	    totalResults = _ref.totalResults,
	    itemsPerPageOptions = _ref.itemsPerPageOptions,
	    onChange = _ref.onChange;

	var currentPage = Math.ceil(startIndex / itemsPerPage);
	var pagesLength = Math.ceil(totalResults / itemsPerPage);
	var onChangeItemsPerPage = function onChangeItemsPerPage(value) {
		onChange(1, value);
	};
	var changePageTo = function changePageTo(type) {
		var from = void 0;
		switch (type) {
			case FIRST:
				{
					from = 1;
					break;
				}
			case PREV:
				{
					from = startIndex - itemsPerPage;
					break;
				}
			case NEXT:
				{
					from = startIndex + itemsPerPage;
					break;
				}
			case LAST:
				{
					from = pagesLength * (itemsPerPage - 1) + 1;
					break;
				}
			default:
				return;
		}
		onChange(from, itemsPerPage);
	};
	return _react2.default.createElement(
		_reactBootstrap.Nav,
		{ onSelect: function onSelect(selectedKey) {
				return changePageTo(selectedKey);
			} },
		_react2.default.createElement(
			_reactBootstrap.NavDropdown,
			{
				id: id ? id + '-size' : _uuid2.default.v4(),
				title: getItemsPerPageTitle(itemsPerPage),
				onSelect: onChangeItemsPerPage
			},
			itemsPerPageOptions.map(function (option, index) {
				return getMenuItem(option, index);
			})
		),
		itemsPerPage > 0 && _react2.default.createElement(
			_reactBootstrap.NavItem,
			{
				eventKey: FIRST,
				id: id && id + '-nav-to-first',
				className: 'btn-link',
				disabled: startIndex === 1
			},
			_react2.default.createElement(_Icon2.default, { name: 'fa fa-backward' })
		),
		itemsPerPage > 0 && _react2.default.createElement(
			_reactBootstrap.NavItem,
			{
				eventKey: PREV,
				id: id && id + '-nav-to-prev',
				className: (0, _classnames2.default)('btn-link', _Pagination2.default['tc-pagination-ctrl-prev'], 'tc-pagination-ctrl-prev'),
				disabled: startIndex === 1
			},
			_react2.default.createElement(_Icon2.default, { name: 'fa fa-play' })
		),
		itemsPerPage > 0 && _react2.default.createElement(
			_reactBootstrap.NavItem,
			{ disabled: true },
			_react2.default.createElement(
				'span',
				{ className: 'btn-link' },
				currentPage,
				'/',
				pagesLength
			)
		),
		itemsPerPage > 0 && _react2.default.createElement(
			_reactBootstrap.NavItem,
			{
				eventKey: NEXT,
				id: id && id + '-nav-to-next',
				className: 'btn-link',
				disabled: startIndex + itemsPerPage > totalResults
			},
			_react2.default.createElement(_Icon2.default, { name: 'fa fa-play' })
		),
		itemsPerPage > 0 && _react2.default.createElement(
			_reactBootstrap.NavItem,
			{
				eventKey: LAST,
				id: id && id + '-nav-to-last',
				className: 'btn-link',
				disabled: startIndex + itemsPerPage > totalResults
			},
			_react2.default.createElement(_Icon2.default, { name: 'fa fa-forward' })
		)
	);
}

Pagination.propTypes = {
	id: _react2.default.PropTypes.string,
	startIndex: _react2.default.PropTypes.number,
	itemsPerPage: _react2.default.PropTypes.number,
	totalResults: _react2.default.PropTypes.number.isRequired,
	itemsPerPageOptions: _react2.default.PropTypes.arrayOf(_react2.default.PropTypes.number),
	onChange: _react2.default.PropTypes.func.isRequired
};

Pagination.defaultProps = {
	itemsPerPage: 5,
	startIndex: 1,
	itemsPerPageOptions: [5, 10, 20, 50]
};

exports.default = Pagination;