"use strict";

exports.__esModule = true;
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _Swipe = _interopRequireDefault(require("./Swipe"));

var _VideoFull = _interopRequireDefault(require("./../VideoFull"));

var _excluded = ["list", "current", "onHide", "onChange", "children"];

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _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; }; return _extends.apply(this, arguments); }

function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (it) return (it = it.call(o)).next.bind(it); if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var Preview = (0, _react.forwardRef)(function (_ref, ref) {
  var list = _ref.list,
      current = _ref.current,
      onHide = _ref.onHide,
      onChange = _ref.onChange,
      children = _ref.children,
      others = _objectWithoutPropertiesLoose(_ref, _excluded);

  var refEl = (0, _react.useRef)(null);
  (0, _react.useImperativeHandle)(ref, function () {
    return refEl.current;
  });
  if (!list || !list.length || !list[0].src) return null;

  var _useState = (0, _react.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 = _createForOfIteratorHelperLoose(list.entries()), _step; !(_step = _iterator()).done;) {
      var _step$value = _step.value,
          index = _step$value[0],
          source = _step$value[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 /*#__PURE__*/_react["default"].createElement(_Swipe["default"], _extends({
    ref: refEl,
    containerChildren: children,
    defaultIndex: activeIndex,
    onHide: hideHandler,
    onChange: changeHandler
  }, others), list.map(function (source, index) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "swiper-slide",
      key: index
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: "swiper-zoom-container"
    }, source.type !== 'video' && /*#__PURE__*/_react["default"].createElement("img", {
      className: "swiper-zoom-target",
      src: source.src
    }), source.type === 'video' && /*#__PURE__*/_react["default"].createElement(_VideoFull["default"], {
      pause: pauseList[index],
      poster: source.thumb,
      src: source.src,
      autoPlay: false,
      bar: /*#__PURE__*/_react["default"].createElement("div", {
        className: "videofull-close",
        onClick: onHide
      })
    }), source.children));
  }));
});
var _default = Preview;
exports["default"] = _default;
module.exports = exports.default;