'use strict';

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

var _typeof2 = require('babel-runtime/helpers/typeof');

var _typeof3 = _interopRequireDefault(_typeof2);

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');

var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _class, _temp;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _lodash = require('lodash');

var _Core = require('./Core.js');

var _Core2 = _interopRequireDefault(_Core);

var _events = require('./events');

var _events2 = _interopRequireDefault(_events);

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

var ImgsPreview = (_temp = _class = function (_React$Component) {
	(0, _inherits3.default)(ImgsPreview, _React$Component);

	function ImgsPreview() {
		var _ref;

		(0, _classCallCheck3.default)(this, ImgsPreview);

		for (var _len = arguments.length, params = Array(_len), _key = 0; _key < _len; _key++) {
			params[_key] = arguments[_key];
		}

		var _this = (0, _possibleConstructorReturn3.default)(this, (_ref = ImgsPreview.__proto__ || Object.getPrototypeOf(ImgsPreview)).call.apply(_ref, [this].concat(params)));

		_this.setRef = function (node, index) {
			_this.thumbnails[index] = _this.thumbnails[index] || node;
		};

		_this.handleShow = function (e, index) {
			e.persist();
			// e.preventDefault();
			var getThumbBoundsFn = function getThumbBoundsFn(index) {
				var target = _this.thumbnails[index] || e.target;
				var pageYScroll = window.pageYOffset || document.documentElement.scrollTop;
				var rect = target.getBoundingClientRect();
				return { x: rect.left, y: rect.top + pageYScroll, w: rect.width };
			};
			var opts = _this.state.opts;

			opts.index = index;
			opts.getThumbBoundsFn = opts.getThumbBoundsFn || getThumbBoundsFn;
			_this.setState({
				show: true,
				opts: opts
			});
		};

		_this.handleClose = function () {
			_this.setState({
				show: false
			});
			_this.props.onClose();
		};

		_this.state = {
			show: _this.props.show,
			opts: _this.props.opts
		};

		_this.imgs = [];

		_this.thumbnails = [];
		return _this;
	}

	(0, _createClass3.default)(ImgsPreview, [{
		key: 'componentWillReceiveProps',
		value: function componentWillReceiveProps(nextProps) {
			var show = this.state.show;

			if (nextProps.show) {
				if (!show) {
					this.setState({ show: true });
				}
			} else if (show) {
				this.setState({ show: false });
			}
		}
	}, {
		key: 'render',
		value: function render() {
			var _this2 = this;

			var _props = this.props,
			    dataSource = _props.dataSource,
			    renderRow = _props.renderRow,
			    style = _props.style,
			    className = _props.className,
			    portal = _props.portal,
			    id = _props.id,
			    other = (0, _objectWithoutProperties3.default)(_props, ['dataSource', 'renderRow', 'style', 'className', 'portal', 'id']);
			var _state = this.state,
			    show = _state.show,
			    opts = _state.opts;

			var Target = portal ? _Core2.default.Component : _Core2.default;
			return _react2.default.createElement(
				_react.Fragment,
				null,
				_react2.default.createElement(
					'div',
					{
						className: (0, _classnames2.default)(['rc-imgs-preview', className]).trim(),
						style: (0, _extends3.default)({ display: 'flex', flexWrap: 'wrap' }, style)
					},
					dataSource.map(function (item, index) {
						return _react2.default.createElement(
							'div',
							{
								key: index,
								ref: function ref(node) {
									return _this2.setRef(node, index);
								},
								onClick: function onClick(e) {
									return _this2.handleShow(e, index);
								}

							},
							renderRow(item)
						);
					})
				),
				_react2.default.createElement(Target, (0, _extends3.default)({
					ref: 'core',
					id: id
				}, (0, _lodash.pick)(other, _events2.default), {
					setInstance: function setInstance(instance) {
						return _this2.instance = instance;
					},
					show: show,
					dataSource: dataSource,
					opts: opts,
					onClose: this.handleClose
				}))
			);
		}
	}]);
	return ImgsPreview;
}(_react2.default.Component), _class.PhotoSwipe = _Core2.default, _class.Component = _Core2.default.Component, _class.Func = _Core2.default.Func, _temp);


ImgsPreview.propTypes = {
	dataSource: _propTypes2.default.array.isRequired,
	opts: _propTypes2.default.object,
	renderRow: _propTypes2.default.func,
	id: _propTypes2.default.string,
	className: _propTypes2.default.string,
	show: _propTypes2.default.bool,
	onClose: _propTypes2.default.func
};

ImgsPreview.defaultProps = {
	opts: {},
	renderRow: function renderRow(item) {
		var image = (typeof item === 'undefined' ? 'undefined' : (0, _typeof3.default)(item)) === 'object' ? item.thumbnail || item.msrc || item.src : image;
		return _react2.default.createElement('img', {
			src: image,
			width: '100',
			height: '100'
		});
	},
	className: '',
	show: false,
	onClose: function onClose() {}
};

exports.default = ImgsPreview;