'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 _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 itemLabelClasses() {
	return (0, _classnames2.default)(_Item2.default['tc-listview-item-label'], 'tc-listview-item-label');
}

function Item(props) {
	var id = props.id,
	    item = props.item,
	    searchCriteria = props.searchCriteria;

	/**
  * This function allow to get component rendering based on searchCriteria
  * @param label the current label to parse & to render
  */

	function getSearchedLabel(label) {
		var splitRegex = new RegExp('(' + searchCriteria + ')', 'gi');
		return label.split(splitRegex).map(function (part, index) {
			var higlighted = part.toLowerCase() === searchCriteria.toLowerCase() ? _react2.default.createElement(
				'b',
				null,
				part
			) : part;
			return _react2.default.createElement(
				'span',
				{ key: index },
				higlighted
			);
		});
	}

	var itemId = 'checkbox-' + id;

	return _react2.default.createElement(
		'div',
		{ id: id },
		_react2.default.createElement(
			'div',
			{ className: 'checkbox-container' },
			_react2.default.createElement(
				'div',
				{
					className: 'checkbox',
					key: item.index
				},
				_react2.default.createElement(
					'label',
					{ htmlFor: itemId },
					_react2.default.createElement('input', {
						id: itemId,
						type: 'checkbox',
						checked: item.checked,
						onChange: function onChange(event) {
							return item.onChange(item, event);
						}
					}),
					_react2.default.createElement(
						'span',
						{ className: itemLabelClasses() },
						searchCriteria ? getSearchedLabel(item.label) : item.label
					)
				)
			)
		)
	);
}

Item.propTypes = _Item4.default;

exports.default = Item;