'use strict';

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _Action2 = _interopRequireDefault(_Action);

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

var _Icon2 = _interopRequireDefault(_Icon);

var _getClassNames = require('../getClassNames');

var _getClassNames2 = _interopRequireDefault(_getClassNames);

var _TreeViewItem = require('./TreeViewItem.scss');

var _TreeViewItem2 = _interopRequireDefault(_TreeViewItem);

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

var getClassName = (0, _getClassNames2.default)(_TreeViewItem2.default);
var getActionHandler = function getActionHandler(func, item) {
	return function (e) {
		e.stopPropagation();func(item);
	};
};
var getIconAction = function getIconAction(label, iconName, onClickHandler, item) {
	return _react2.default.createElement(_Action2.default, {
		label: label,
		icon: iconName,
		onClick: getActionHandler(onClickHandler, item),
		tooltipPlacement: 'right',
		hideLabel: true,
		link: true
	});
};

/**
 *
 * Single item of TreeView component
 *
 * @param item required, item to display, consists of:
 * @param itemSelectCallback required, callback function to trigger once item was clicked
 * @param itemToggleCallback required, callback function to trigger once item was clicked
 * @param itemRemoveCallback optional, callback function to trigger once trash icon was clicked
 * @param itemRemoveLabel optional, label string for trash icon
 * @param depth optional, depth of an item in a tree
 *
 * @returns XML, jsx to display
 */

function TreeViewItem(_ref) {
	var item = _ref.item,
	    itemSelectCallback = _ref.itemSelectCallback,
	    itemToggleCallback = _ref.itemToggleCallback,
	    itemRemoveCallback = _ref.itemRemoveCallback,
	    itemRemoveLabel = _ref.itemRemoveLabel,
	    depth = _ref.depth;

	var toggleIconLabel = item.toggled ? 'Collapse' : 'Expand';
	var selectHandler = function selectHandler() {
		return itemSelectCallback(item);
	};

	return _react2.default.createElement(
		'li',
		{ className: getClassName('tc-treeview-li'), 'data-tc-treeview-hidden': item.hidden },
		_react2.default.createElement(
			'div',
			{ // eslint-disable-line jsx-a11y/no-static-element-interactions
				className: getClassName('tc-treeview-item'),
				onClick: selectHandler,
				'data-tc-treeview-depth': depth,
				'data-tc-treeview-selected': item.selected
			},
			item.children && _react2.default.createElement(
				'div',
				{
					className: getClassName('tc-treeview-toggle'),
					'data-tc-treeview-toggled': item.toggled
				},
				getIconAction(toggleIconLabel, 'talend-caret-down', itemToggleCallback, item)
			),
			_react2.default.createElement(
				'span',
				{ className: getClassName('tc-treeview-folder') },
				_react2.default.createElement(_Icon2.default, { name: 'talend-folder' })
			),
			_react2.default.createElement(
				'span',
				null,
				item.name
			),
			itemRemoveCallback && _react2.default.createElement(
				'div',
				{ className: getClassName('tc-treeview-remove') },
				getIconAction(itemRemoveLabel, 'talend-trash', itemRemoveCallback, item)
			)
		),
		item.children && item.toggled && _react2.default.createElement(
			'ul',
			{ className: getClassName('tc-treeview-ul') },
			item.children.map(function (child) {
				return _react2.default.createElement(TreeViewItem, {
					item: child,
					itemSelectCallback: itemSelectCallback,
					itemToggleCallback: itemToggleCallback,
					itemRemoveCallback: itemRemoveCallback,
					itemRemoveLabel: itemRemoveLabel,
					depth: depth + 1,
					key: child.name
				});
			})
		)
	);
}

TreeViewItem.propTypes = {
	item: _react.PropTypes.shape({
		name: _react.PropTypes.string.isRequired,
		toggled: _react.PropTypes.bool,
		selected: _react.PropTypes.bool,
		children: _react.PropTypes.arrayOf(_react.PropTypes.object)
	}),
	itemSelectCallback: _react.PropTypes.func.isRequired,
	itemToggleCallback: _react.PropTypes.func.isRequired,
	itemRemoveCallback: _react.PropTypes.func,
	itemRemoveLabel: _react.PropTypes.string,
	depth: _react.PropTypes.number
};

exports.default = TreeViewItem;