'use strict';

Object.defineProperty(exports, "__esModule", {
	value: true
});
exports.onClick = exports.onMouseEnter = exports.Registry = exports.timerRegistry = 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; };

exports.onMouseOut = onMouseOut;
exports.onManuallyClose = onManuallyClose;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _reactAddonsCssTransitionGroup2 = _interopRequireDefault(_reactAddonsCssTransitionGroup);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _Actions = require('../Actions');

var _Notification = require('./Notification.scss');

var _Notification2 = _interopRequireDefault(_Notification);

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

var timerRegistry = exports.timerRegistry = {};

var Registry = exports.Registry = {
	register: function register(notification, timer) {
		timerRegistry[notification.id] = timer;
	},
	cancel: function cancel(notification) {
		if (this.isRegistered(notification)) {
			clearTimeout(timerRegistry[notification.id]);
		}
	},

	isRegistered: function isRegistered(notification) {
		return !!timerRegistry[notification.id];
	}
};

function _onMouseEnter(notification) {
	if (notification.type !== 'error') {
		Registry.cancel(notification);
	}
}

exports.onMouseEnter = _onMouseEnter;
function onMouseOut(event, notification, leaveFn, autoLeaveTimeout) {
	if (notification.type === 'error' || event.currentTarget.getAttribute('pin') === 'true') {
		return;
	}
	Registry.register(notification, setTimeout(function () {
		return leaveFn(notification);
	}, autoLeaveTimeout));
}

function _onClick(event, notification) {
	if (notification.type !== 'error') {
		if (event.currentTarget.getAttribute('pin') !== 'true') {
			event.currentTarget.setAttribute('pin', 'true');
		} else {
			event.currentTarget.setAttribute('pin', 'false');
		}
	}
}

exports.onClick = _onClick;
function onManuallyClose(event, notification, leaveFn) {
	event.stopPropagation();
	Registry.cancel(notification);
	leaveFn(notification);
}

function CloseButton(_ref) {
	var notification = _ref.notification,
	    leaveFn = _ref.leaveFn;

	return _react2.default.createElement(_Actions.Action, {
		onClick: function onClick() {
			return leaveFn(notification);
		},
		label: '',
		icon: 'talend-cross',
		bsClass: (0, _classnames2.default)(_Notification2.default['tc-notification-action'], 'tc-notification-action', _Notification2.default['tc-notification-close'], '.tc-notification-close')
	});
}

function MessageAction(_ref2) {
	var action = _ref2.action;

	return !!action && _react2.default.createElement(_Actions.Action, _extends({}, action, {
		bsClass: (0, _classnames2.default)(_Notification2.default['tc-notification-action'], 'tc-notification-action', _Notification2.default['tc-notification-message-action'], 'tc-notification-message-action')
	}));
}

function Message(_ref3) {
	var notification = _ref3.notification;
	var message = notification.message,
	    action = notification.action;

	var messageClass = (0, _classnames2.default)(_Notification2.default['tc-notification-message'], 'tc-notification-message');
	return Array.isArray(message) ? _react2.default.createElement(
		'article',
		null,
		message.map(function (paragraph, index) {
			return _react2.default.createElement(
				'p',
				{ key: index, className: messageClass },
				paragraph,
				index === message.length - 1 && _react2.default.createElement(MessageAction, { action: action })
			);
		})
	) : _react2.default.createElement(
		'p',
		{ className: messageClass },
		message,
		_react2.default.createElement(MessageAction, { action: action })
	);
}

function TimerBar(_ref4) {
	var type = _ref4.type;

	return type !== 'error' && _react2.default.createElement('div', {
		className: (0, _classnames2.default)(_Notification2.default['tc-notification-timer-bar'], 'tc-notification-timer-bar')
	});
}

function Notification(_ref5) {
	var _notificationClasses;

	var notification = _ref5.notification,
	    leaveFn = _ref5.leaveFn,
	    autoLeaveTimeout = _ref5.autoLeaveTimeout;

	var notificationClasses = (_notificationClasses = {}, _defineProperty(_notificationClasses, _Notification2.default['tc-notification'], true), _defineProperty(_notificationClasses, 'tc-notification', true), _defineProperty(_notificationClasses, _Notification2.default['tc-notification-info'], !notification.type || notification.type === 'info'), _defineProperty(_notificationClasses, 'tc-notification-info', !notification.type || notification.type === 'info'), _defineProperty(_notificationClasses, _Notification2.default['tc-notification-warning'], notification.type === 'warning'), _defineProperty(_notificationClasses, 'tc-notification-warning', notification.type === 'warning'), _defineProperty(_notificationClasses, _Notification2.default['tc-notification-error'], notification.type === 'error'), _defineProperty(_notificationClasses, 'tc-notification-error', notification.type === 'error'), _notificationClasses);
	var classes = (0, _classnames2.default)(notificationClasses);
	return _react2.default.createElement(
		'div',
		{ // eslint-disable-line jsx-a11y/no-static-element-interactions
			role: 'status',
			className: classes,
			onMouseEnter: function onMouseEnter() {
				return _onMouseEnter(notification);
			},
			onMouseLeave: function onMouseLeave(event) {
				return onMouseOut(event, notification, leaveFn, autoLeaveTimeout);
			},
			onClick: function onClick(event) {
				return _onClick(event, notification);
			}
		},
		_react2.default.createElement(CloseButton, { notification: notification, leaveFn: leaveFn }),
		_react2.default.createElement(Message, { notification: notification }),
		_react2.default.createElement(TimerBar, { type: notification.type })
	);
}

function renderNotifications(_ref6) {
	var notifications = _ref6.notifications,
	    leaveFn = _ref6.leaveFn,
	    autoLeaveTimeout = _ref6.autoLeaveTimeout;

	return notifications.map(function (notification) {
		if (!Registry.isRegistered(notification) && notification.type !== 'error') {
			Registry.register(notification, setTimeout(function () {
				return leaveFn(notification);
			}, autoLeaveTimeout));
		}
		return _react2.default.createElement(Notification, _extends({
			key: notification.id
		}, { notification: notification, leaveFn: leaveFn, autoLeaveTimeout: autoLeaveTimeout }));
	});
}

function NotificationsContainer(_ref7) {
	var enterTimeout = _ref7.enterTimeout,
	    leaveTimeout = _ref7.leaveTimeout,
	    notifications = _ref7.notifications,
	    leaveFn = _ref7.leaveFn,
	    autoLeaveTimeout = _ref7.autoLeaveTimeout;

	return _react2.default.createElement(
		'div',
		{ className: (0, _classnames2.default)(_Notification2.default['tc-notification-container'], 'tc-notification-container') },
		_react2.default.createElement(
			_reactAddonsCssTransitionGroup2.default,
			{
				transitionName: 'tc-notification',
				transitionEnterTimeout: enterTimeout,
				transitionLeaveTimeout: leaveTimeout
			},
			renderNotifications({ notifications: notifications, leaveFn: leaveFn, autoLeaveTimeout: autoLeaveTimeout })
		)
	);
}

var notificationShape = {
	id: _react.PropTypes.any.isRequired,
	type: _react.PropTypes.oneOf(['info', 'warning', 'error']),
	message: _react.PropTypes.oneOfType([_react.PropTypes.string, _react.PropTypes.arrayOf(_react.PropTypes.string)]).isRequired,
	action: _react.PropTypes.shape(_Actions.Action.propTypes)
};

CloseButton.propTypes = {
	notification: _react.PropTypes.shape(notificationShape).isRequired,
	leaveFn: _react.PropTypes.func.isRequired
};

MessageAction.propTypes = {
	action: _react.PropTypes.shape(_Actions.Action.propTypes)
};

Message.propTypes = {
	notification: _react.PropTypes.shape(notificationShape).isRequired
};

TimerBar.propTypes = {
	type: _react.PropTypes.oneOf(['info', 'warning', 'error'])
};

Notification.propTypes = {
	notification: _react.PropTypes.shape(notificationShape).isRequired,
	leaveFn: _react.PropTypes.func.isRequired,
	autoLeaveTimeout: _react.PropTypes.number
};

NotificationsContainer.propTypes = {
	enterTimeout: _react.PropTypes.number,
	leaveTimeout: _react.PropTypes.number,
	autoLeaveTimeout: _react.PropTypes.number,
	notifications: _react.PropTypes.arrayOf(_react.PropTypes.shape(notificationShape)).isRequired,
	leaveFn: _react.PropTypes.func.isRequired
};

NotificationsContainer.defaultProps = {
	enterTimeout: 300,
	leaveTimeout: 280,
	autoLeaveTimeout: 4000
};

exports.default = NotificationsContainer;