'use strict';

Object.defineProperty(exports, "__esModule", {
	value: true
});
exports.TYPE_BADGE = exports.TYPE_ACTION = exports.TYPE_STATUS = undefined;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

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

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

var _Action2 = _interopRequireDefault(_Action);

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

var _Icon2 = _interopRequireDefault(_Icon);

var _Status = require('../Status');

var _Status2 = _interopRequireDefault(_Status);

var _TooltipTrigger = require('./../TooltipTrigger');

var _TooltipTrigger2 = _interopRequireDefault(_TooltipTrigger);

var _CollapsiblePanel = require('./CollapsiblePanel.scss');

var _CollapsiblePanel2 = _interopRequireDefault(_CollapsiblePanel);

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 TYPE_STATUS = exports.TYPE_STATUS = 'status';
var TYPE_ACTION = exports.TYPE_ACTION = 'action';
var TYPE_BADGE = exports.TYPE_BADGE = 'badge';

var getActionHandler = function getActionHandler(func, item) {
	return function (e) {
		e.stopPropagation();
		func(e, item);
	};
};

var displayModes = [TYPE_ACTION, TYPE_BADGE, TYPE_STATUS];

var statusPropTypes = _extends({
	displayMode: _react.PropTypes.oneOf(displayModes)
}, _Status2.default.propTypes);

var actionPropTypes = _extends({
	displayMode: _react.PropTypes.oneOf(displayModes)
}, _Action2.default.propTypes);

var simplePropTypes = {
	displayMode: _react.PropTypes.oneOf(displayModes),
	label: _react.PropTypes.string,
	bsStyle: _react.PropTypes.string,
	tooltipPlacement: _reactBootstrap.OverlayTrigger.propTypes.placement
};

function renderHeaderItem(_ref, key) {
	var displayMode = _ref.displayMode,
	    headerItem = _objectWithoutProperties(_ref, ['displayMode']);

	switch (displayMode) {
		case TYPE_STATUS:
			{
				var actions = headerItem.actions,
				    restStatus = _objectWithoutProperties(headerItem, ['actions']);

				var adaptActions = actions.map(function (action) {
					return _extends({}, action, {
						onClick: getActionHandler(action.onClick, headerItem)
					});
				});
				return _react2.default.createElement(_Status2.default, _extends({ key: key, actions: adaptActions }, restStatus));
			}
		case TYPE_ACTION:
			{
				var onClick = headerItem.onClick,
				    restAction = _objectWithoutProperties(headerItem, ['onClick']);

				return _react2.default.createElement(_Action2.default, _extends({ key: key, onClick: getActionHandler(onClick, headerItem) }, restAction));
			}
		case TYPE_BADGE:
			{
				var label = headerItem.label,
				    tooltipPlacement = headerItem.tooltipPlacement,
				    rest = _objectWithoutProperties(headerItem, ['label', 'tooltipPlacement']);

				return _react2.default.createElement(
					_TooltipTrigger2.default,
					{ key: key, label: label, tooltipPlacement: tooltipPlacement },
					_react2.default.createElement(
						_reactBootstrap.Label,
						rest,
						label
					)
				);
			}
		default:
			{
				var _label = headerItem.label,
				    _tooltipPlacement = headerItem.tooltipPlacement;

				return _react2.default.createElement(
					_TooltipTrigger2.default,
					{ key: key, label: _label, tooltipPlacement: _tooltipPlacement },
					_react2.default.createElement(
						'span',
						null,
						_label
					)
				);
			}
	}
}
renderHeaderItem.propTypes = _react.PropTypes.oneOfType([_react.PropTypes.shape(statusPropTypes), _react.PropTypes.shape(actionPropTypes), _react.PropTypes.shape(simplePropTypes), _react.PropTypes.arrayOf(_react.PropTypes.oneOfType([_react.PropTypes.shape(statusPropTypes), _react.PropTypes.shape(actionPropTypes), _react.PropTypes.shape(simplePropTypes)]))]);

function renderHeader(_ref2) {
	var header = _ref2.header,
	    caret = _ref2.caret;

	var headerColumnClass = 'col-' + header.length;
	var headerItems = header.map(function (headerItem, index) {
		if (Array.isArray(headerItem)) {
			var elements = headerItem.map(renderHeaderItem);
			return _react2.default.createElement(
				'div',
				{ key: index, className: (0, _classnames2.default)(_CollapsiblePanel2.default.group, _CollapsiblePanel2.default[headerColumnClass]) },
				elements
			);
		}
		return _react2.default.createElement(
			'div',
			{ key: index, className: _CollapsiblePanel2.default[headerColumnClass] },
			renderHeaderItem(headerItem)
		);
	});

	if (caret) {
		headerItems.push(_react2.default.createElement(_Icon2.default, {
			key: header.length,
			className: _CollapsiblePanel2.default.caret,
			name: 'talend-caret-down'
		}));
	}

	return headerItems;
}

/**
 * @param {object} props react props
 * @example
 const props = {
	header: [
		{
			displayMode: 'status',
			status: 'failed',
			label: 'Failed',
			icon: 'fa fa-close',
			actions: [
				{
					label: 'cancel',
					icon: 'fa fa-cancel',
					onClick: action('onCancel'),
				}
			],
		},
		{
			label: 'by Charles',
			bsStyle: 'default',
			tooltipPlacement: 'top',
		},
		{
			displayMode: 'action',
			label: 'edit',
			icon: 'fa fa-edit',
			onClick: action('onEdit'),
			tooltipPlacement: 'right',
			hideLabel: true,
			link: true,
		}
	],
	content: [
		{
			label: 'Content1',
			description: 'Description1',
		},
		{
			label: 'Content2',
			description: 'Description2',
		}

	]
};
 <CollapsiblePanel {...props} />
 */
function CollapsiblePanel(_ref3) {
	var header = _ref3.header,
	    content = _ref3.content;

	var hasContent = content && content.length;
	var headerItems = renderHeader({ header: header, caret: hasContent });

	if (hasContent) {
		return _react2.default.createElement(
			_reactBootstrap.Panel,
			{ className: _CollapsiblePanel2.default['tc-collapsible-panel'], collapsible: true, header: headerItems },
			content.map(function (item, index) {
				return _react2.default.createElement(
					'div',
					{ key: index, className: _CollapsiblePanel2.default.content },
					_react2.default.createElement(
						'div',
						{ className: _CollapsiblePanel2.default.label },
						_react2.default.createElement(
							_reactBootstrap.Label,
							null,
							item.label
						)
					),
					_react2.default.createElement(
						'div',
						{ className: _CollapsiblePanel2.default.description },
						item.description
					)
				);
			})
		);
	}
	return _react2.default.createElement(_reactBootstrap.Panel, { className: 'tc-collapsible-panel ' + _CollapsiblePanel2.default['tc-panel'], header: headerItems });
}

CollapsiblePanel.propTypes = {
	header: _react.PropTypes.arrayOf(renderHeaderItem.propTypes).isRequired,
	content: _react.PropTypes.arrayOf(_react.PropTypes.shape({
		label: _react.PropTypes.string,
		description: _react.PropTypes.string
	}))
};

exports.default = CollapsiblePanel;