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

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

import React, { forwardRef, useRef, useImperativeHandle, useState } from 'react';
import Swipe from './Swipe';
import VideoFull from './../VideoFull';

var Preview = forwardRef(function (_ref, ref) {
  var list = _ref.list,
      current = _ref.current,
      onHide = _ref.onHide,
      onChange = _ref.onChange,
      children = _ref.children,
      others = _objectWithoutProperties(_ref, ['list', 'current', 'onHide', 'onChange', 'children']);

  var refEl = useRef(null);
  useImperativeHandle(ref, function () {
    return refEl.current;
  });

  if (!list || !list.length || !list[0].src) return null;

  var _useState = useState(new Array(list.length)),
      pauseList = _useState[0],
      setPauseList = _useState[1];

  // 当前选中项


  var activeIndex = 0;
  if (typeof current === 'number') {
    activeIndex = current;
  } else if (typeof current === 'string') {
    for (var _iterator = list.entries(), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
      var _ref3;

      if (_isArray) {
        if (_i >= _iterator.length) break;
        _ref3 = _iterator[_i++];
      } else {
        _i = _iterator.next();
        if (_i.done) break;
        _ref3 = _i.value;
      }

      var _ref2 = _ref3;
      var index = _ref2[0];
      var source = _ref2[1];

      if (source.src === current) activeIndex = index;
    }
  }
  // 图片单击隐藏, 视频单击无反应
  function hideHandler(s) {
    if (list[s.activeIndex].type !== 'video') {
      if (onHide) onHide();
    }
  }
  // 滑动视频需要暂停其它视频
  function changeHandler(s) {
    s.target = refEl.current;
    // 暂停所有视频
    if (list[s.activeIndex].type === 'video') {
      var newPauseList = list.map(function () {
        return true;
      });
      setPauseList(newPauseList);
      setTimeout(function () {
        setPauseList(new Array(list.length));
      }, 500);
    }
    if (onChange) onChange(s, list[s.activeIndex], s.activeIndex);
  }
  return React.createElement(
    Swipe,
    _extends({ ref: refEl, containerChildren: children, defaultIndex: activeIndex, onHide: hideHandler, onChange: changeHandler }, others),
    list.map(function (source, index) {
      return React.createElement(
        'div',
        { className: 'swiper-slide', key: index },
        React.createElement(
          'div',
          { className: 'swiper-zoom-container' },
          source.type !== 'video' && React.createElement('img', { className: 'swiper-zoom-target', src: source.src }),
          source.type === 'video' && React.createElement(VideoFull, {
            pause: pauseList[index],
            poster: source.thumb,
            src: source.src,
            autoPlay: false,
            bar: React.createElement('div', { className: 'videofull-close', onClick: onHide })
          }),
          source.children
        )
      );
    })
  );
});

export default Preview;