'use strict';

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

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

exports.cancelActionComponent = cancelActionComponent;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactAddonsCssTransitionGroup = require('react-addons-css-transition-group');

var _reactAddonsCssTransitionGroup2 = _interopRequireDefault(_reactAddonsCssTransitionGroup);

var _classnames2 = require('classnames');

var _classnames3 = _interopRequireDefault(_classnames2);

var _ActionBar = require('../ActionBar');

var _ActionBar2 = _interopRequireDefault(_ActionBar);

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

var _Action2 = _interopRequireDefault(_Action);

var _Drawer = require('./Drawer.scss');

var _Drawer2 = _interopRequireDefault(_Drawer);

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

function DrawerAnimation(_ref) {
	var children = _ref.children;

	return _react2.default.createElement(
		_reactAddonsCssTransitionGroup2.default,
		{
			transitionName: 'tc-drawer',
			transitionAppearTimeout: 230,
			transitionEnterTimeout: 230,
			transitionLeaveTimeout: 230
		},
		children
	);
}

DrawerAnimation.propTypes = {
	children: _react.PropTypes.node
};

function DrawerContainer(_ref2) {
	var _classnames;

	var stacked = _ref2.stacked,
	    className = _ref2.className,
	    children = _ref2.children,
	    rest = _objectWithoutProperties(_ref2, ['stacked', 'className', 'children']);

	var drawerContainerClasses = (0, _classnames3.default)(_Drawer2.default['tc-drawer'], className, 'tc-drawer', (_classnames = {}, _defineProperty(_classnames, _Drawer2.default['drawer-stacked'], stacked), _defineProperty(_classnames, 'stacked', stacked), _classnames));
	return _react2.default.createElement(
		'div',
		_extends({ className: drawerContainerClasses }, rest),
		_react2.default.createElement(
			'div',
			{ className: _Drawer2.default['tc-drawer-container'] },
			children
		)
	);
}

DrawerContainer.propTypes = {
	stacked: _react.PropTypes.bool,
	className: _react.PropTypes.string,
	children: _react.PropTypes.node.isRequired
};

function cancelActionComponent(onCancelAction) {
	if (!onCancelAction) {
		return null;
	}
	var enhancedCancelAction = _extends({
		icon: 'talend-cross',
		hideLabel: true,
		link: true
	}, onCancelAction);
	return _react2.default.createElement(_Action2.default, _extends({ className: _Drawer2.default['tc-drawer-close-action'] }, enhancedCancelAction));
}

function DrawerTitle(_ref3) {
	var title = _ref3.title,
	    children = _ref3.children,
	    onCancelAction = _ref3.onCancelAction;

	if (!title) {
		return null;
	}
	return _react2.default.createElement(
		'div',
		null,
		_react2.default.createElement(
			'div',
			{ className: _Drawer2.default['tc-drawer-header'] },
			_react2.default.createElement(
				'h1',
				null,
				title
			),
			cancelActionComponent(onCancelAction)
		),
		_react2.default.createElement(
			'div',
			{ className: _Drawer2.default['tc-drawer-header-with-tabs'] },
			children
		)
	);
}

DrawerTitle.propTypes = {
	title: _react.PropTypes.string.isRequired,
	onCancelAction: _react.PropTypes.shape(_Action2.default.propTypes),
	children: _react.PropTypes.node
};

function DrawerContent(_ref4) {
	var children = _ref4.children,
	    rest = _objectWithoutProperties(_ref4, ['children']);

	return _react2.default.createElement(
		'div',
		_extends({ className: _Drawer2.default['tc-drawer-content'] }, rest),
		children
	);
}

DrawerContent.propTypes = {
	children: _react.PropTypes.node
};

function DrawerFooter(_ref5) {
	var children = _ref5.children;

	return _react2.default.createElement(
		'div',
		{ className: _Drawer2.default['tc-drawer-footer'] },
		children
	);
}

DrawerFooter.propTypes = {
	children: _react.PropTypes.node
};

function combinedFooterActions(onCancelAction, footerActions) {
	if (!onCancelAction) {
		return footerActions;
	}
	var enhancedFooterActions = _extends({}, footerActions);
	if (footerActions && footerActions.actions && footerActions.actions.left) {
		enhancedFooterActions.actions.left.push(onCancelAction);
	} else {
		enhancedFooterActions.actions.left = [].push(onCancelAction);
	}
	return enhancedFooterActions;
}

function Drawer(_ref6) {
	var stacked = _ref6.stacked,
	    title = _ref6.title,
	    className = _ref6.className,
	    style = _ref6.style,
	    children = _ref6.children,
	    footerActions = _ref6.footerActions,
	    onCancelAction = _ref6.onCancelAction;

	if (!children) {
		return null;
	}
	return _react2.default.createElement(
		DrawerContainer,
		{ stacked: stacked, className: className, style: style },
		_react2.default.createElement(DrawerTitle, { title: title, onCancelAction: onCancelAction }),
		_react2.default.createElement(
			'div',
			{ style: { display: 'flex', flexDirection: 'column', flexGrow: 1, overflow: 'auto' } },
			_react2.default.createElement(
				DrawerContent,
				null,
				children
			),
			_react2.default.createElement(_ActionBar2.default, combinedFooterActions(onCancelAction, footerActions))
		)
	);
}

Drawer.propTypes = {
	stacked: _react.PropTypes.bool,
	title: _react.PropTypes.string,
	children: _react.PropTypes.node,
	style: _react.PropTypes.object, // eslint-disable-line react/forbid-prop-types
	className: _react.PropTypes.string,
	// footer action, see action bar for api
	footerActions: _react.PropTypes.shape(_ActionBar2.default.propTypes).isRequired,
	onCancelAction: _react.PropTypes.shape(_Action2.default.propTypes)
};

Drawer.Animation = DrawerAnimation;
Drawer.Container = DrawerContainer;
Drawer.Title = DrawerTitle;
Drawer.Content = DrawerContent;
Drawer.Footer = DrawerFooter;
Drawer.FooterStyle = _Drawer2.default['tc-drawer-footer'];

exports.default = Drawer;