'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 _utils = require('./utils');

var _Action = require('../../../Actions/Action');

var _Action2 = _interopRequireDefault(_Action);

var _Item = require('./Item.scss');

var _Item2 = _interopRequireDefault(_Item);

var _Item3 = require('./Item.propTypes');

var _Item4 = _interopRequireDefault(_Item3);

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

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 itemClasses(isSelected) {
	var _classNames;

	return (0, _classnames2.default)((_classNames = {}, _defineProperty(_classNames, _Item2.default['tc-enumeration-item'], true), _defineProperty(_classNames, _Item2.default['selected-item'], isSelected), _defineProperty(_classNames, 'tc-enumeration-item', true), _defineProperty(_classNames, 'selected-item', isSelected), _classNames));
}

function itemLabelClasses() {
	var _classNames2;

	return (0, _classnames2.default)((_classNames2 = {}, _defineProperty(_classNames2, _Item2.default['tc-enumeration-item-label'], true), _defineProperty(_classNames2, 'tc-enumeration-item-label', true), _classNames2));
}

function itemDefaultActionsClasses() {
	var _classNames3;

	return (0, _classnames2.default)((_classNames3 = {}, _defineProperty(_classNames3, _Item2.default['tc-enumeration-item-actions'], true), _defineProperty(_classNames3, 'tc-enumeration-item-actions', true), _defineProperty(_classNames3, _Item2.default.editable, true), _classNames3));
}

function Item(_ref) {
	var id = _ref.id,
	    item = _ref.item,
	    searchCriteria = _ref.searchCriteria;
	var _item$itemProps = item.itemProps,
	    key = _item$itemProps.key,
	    actions = _item$itemProps.actions,
	    onSelectItem = _item$itemProps.onSelectItem;


	function getAction(action, index) {
		function onClick(event) {
			if (action.onClick) {
				action.onClick(event, {
					value: event.target.value,
					index: item.index
				});
			}
		}

		return _react2.default.createElement(_Action2.default, {
			key: index,
			label: action.label,
			icon: action.icon,
			onClick: onClick,
			inProgress: action.inProgress,
			tooltipPlacement: 'bottom',
			hideLabel: true,
			link: true
		});
	}

	/**
  * This function allow to get component rendering based on searchCriteria
  * @param label the current label to parse & to render
  */
	function getSearchedLabel(label) {
		var indexes = (0, _utils.allIndexOf)(label.toLowerCase(), searchCriteria.toLowerCase());
		indexes = (0, _utils.removeDuplicates)(indexes, searchCriteria);
		return _react2.default.createElement(
			'span',
			null,
			indexes[0] !== 0 ? label.substring(0, indexes[0]) : null,
			indexes.map(function (matchIndex, index, matchIndexes) {
				return _react2.default.createElement(
					'span',
					{ key: index },
					_react2.default.createElement(
						'strong',
						null,
						label.substring(matchIndex, matchIndex + searchCriteria.length)
					),
					index === matchIndex.length + 1 ? null : label.substring(matchIndex + searchCriteria.length, matchIndexes[index + 1])
				);
			})
		);
	}

	function getActionLabel() {
		if (searchCriteria) {
			return _react2.default.createElement(
				'button',
				{
					className: itemLabelClasses(),
					disabled: 'disabled'
				},
				getSearchedLabel(item[key].join(','))
			);
		}
		return _react2.default.createElement(_Action2.default, {
			key: item.index,
			label: item[key].join(','),
			onClick: function onClick(event) {
				return onSelectItem(item, event);
			},
			className: itemLabelClasses(),
			tooltip: true
		});
	}

	return _react2.default.createElement(
		'li',
		{ className: itemClasses(item.isSelected), id: id },
		getActionLabel(),
		_react2.default.createElement(
			'div',
			{ className: itemDefaultActionsClasses() },
			actions.map(function (action, index) {
				return getAction(action, index);
			})
		)
	);
}

Item.propTypes = _Item4.default;

exports.default = Item;