'use strict';

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

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 FIRST = 'first';
var PREV = 'prev';
var NEXT = 'next';
var LAST = 'last';

function getItemsPerPageTitle(option) {
	if (option > 0) {
		return option;
	}
	return 'All';
}

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,
	    onChange = _ref.onChange,
	    opts = _objectWithoutProperties(_ref, ['id', 'startIndex', 'itemsPerPage', 'totalResults', 'onChange']);

	var itemsPerPageOptions = opts.itemsPerPageOptions,
	    _opts$paginationIconP = opts.paginationIconProps,
	    paginationIconProps = _opts$paginationIconP === undefined ? {} : _opts$paginationIconP;
	var _paginationIconProps$ = paginationIconProps.first,
	    first = _paginationIconProps$ === undefined ? { name: 'talend-chevron-end' } : _paginationIconProps$,
	    _paginationIconProps$2 = paginationIconProps.prev,
	    prev = _paginationIconProps$2 === undefined ? { name: 'talend-chevron-left' } : _paginationIconProps$2,
	    _paginationIconProps$3 = paginationIconProps.next,
	    next = _paginationIconProps$3 === undefined ? { name: 'talend-chevron-left', transform: 'rotate-180' } : _paginationIconProps$3,
	    _paginationIconProps$4 = paginationIconProps.last,
	    last = _paginationIconProps$4 === undefined ? { name: 'talend-chevron-end', transform: 'rotate-180' } : _paginationIconProps$4;

	var currentPage = Math.ceil(startIndex / itemsPerPage);
	var pagesLength = Math.ceil(totalResults / itemsPerPage);
	var isNavigationShown = itemsPerPage > 0 && pagesLength > 1;
	function onChangeItemsPerPage(value) {
		return onChange(1, value);
	}
	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 - 1) * itemsPerPage + 1;
					break;
				}
			default:
				return;
		}
		onChange(from, itemsPerPage);
	}
	function getNavigationItems() {
		return [_react2.default.createElement(
			_reactBootstrap.NavItem,
			{
				eventKey: FIRST,
				id: id && id + '-nav-to-first',
				className: 'btn-link',
				disabled: startIndex <= 1
			},
			_react2.default.createElement(_Icon2.default, first)
		), _react2.default.createElement(
			_reactBootstrap.NavItem,
			{
				eventKey: PREV,
				id: id && id + '-nav-to-prev',
				className: 'btn-link',
				disabled: startIndex <= 1
			},
			_react2.default.createElement(_Icon2.default, prev)
		), _react2.default.createElement(
			_reactBootstrap.NavItem,
			{ disabled: true },
			_react2.default.createElement(
				'span',
				{ className: 'btn-link' },
				currentPage,
				'/',
				pagesLength
			)
		), _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, next)
		), _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, last)
		)];
	}
	return _react2.default.createElement(
		_reactBootstrap.Nav,
		{
			className: _Pagination2.default['tc-pagination'],
			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);
			})
		),
		isNavigationShown && getNavigationItems()
	);
}

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;