"use strict";

exports.__esModule = true;
exports.default = void 0;

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

var _clsx = _interopRequireDefault(require("clsx"));

var _utils = require("../utils");

var _swiper = _interopRequireDefault(require("../swiper"));

var _popup = _interopRequireDefault(require("../popup"));

var _ImagePreviewItem = _interopRequireDefault(require("./ImagePreviewItem"));

var _ConfigProviderContext = _interopRequireDefault(require("../config-provider/ConfigProviderContext"));

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

var ImagePreview = function ImagePreview(props) {
  var _useContext = (0, _react.useContext)(_ConfigProviderContext.default),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

  var _createNamespace = createNamespace('image-preview', prefixCls),
      bem = _createNamespace[0];

  var swiperRef = (0, _react.useRef)(null);

  var _useState = (0, _react.useState)(function () {
    return props.startPosition;
  }),
      active = _useState[0],
      setActive = _useState[1];

  var onSwipeChange = function onSwipeChange(idx) {
    if (active !== idx) {
      setActive(idx);
      props.onChange == null ? void 0 : props.onChange(idx);
    }
  };

  var onClose = function onClose(image, i) {
    props.onClose && (props.onClose == null ? void 0 : props.onClose({
      url: image,
      index: i
    }));
  };

  var renderImages = function renderImages() {
    return /*#__PURE__*/_react.default.createElement(_swiper.default, {
      ref: swiperRef,
      className: (0, _clsx.default)(bem('swipe')),
      loop: props.loop,
      duration: props.swipeDuration,
      initialSwipe: active,
      onChange: onSwipeChange,
      indicator: props.showIndicators
    }, props.images.map(function (image, i) {
      return /*#__PURE__*/_react.default.createElement(_swiper.default.Item, {
        className: (0, _clsx.default)(bem('item')),
        key: image,
        onClick: function onClick() {
          onClose(image, i);
        }
      }, /*#__PURE__*/_react.default.createElement(_ImagePreviewItem.default, {
        maxZoom: props.maxZoom,
        image: image,
        lazyload: props.lazyload,
        onTap: function onTap() {
          onClose(image, i);
        },
        onZoomChange: function onZoomChange(zoom) {
          if (zoom !== 1) {
            var _swiperRef$current;

            (_swiperRef$current = swiperRef.current) == null ? void 0 : _swiperRef$current.disable();
          } else {
            var _swiperRef$current2;

            (_swiperRef$current2 = swiperRef.current) == null ? void 0 : _swiperRef$current2.enable();
          }
        }
      }));
    }));
  };

  var renderClose = function renderClose() {
    if (props.closeable) {
      return /*#__PURE__*/_react.default.cloneElement(props.closeIcon, {
        className: (0, _clsx.default)(bem('close-icon', props.closeIconPosition)),
        onClick: props.onClose
      });
    }

    return null;
  };

  var renderIndex = function renderIndex() {
    if (props.showIndex) {
      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _clsx.default)(bem('index'))
      }, props.indexRender ? props.indexRender({
        index: active,
        len: props.images.length
      }) : active + 1 + " / " + props.images.length);
    }

    return null;
  };

  return /*#__PURE__*/_react.default.createElement(_popup.default, _extends({
    className: (0, _clsx.default)(bem(), props.className),
    overlayClass: (0, _clsx.default)(bem('overlay')),
    beforeClose: props.beforeClose
  }, (0, _utils.pick)(props, ['visible', 'overlayStyle', 'closeOnPopstate', 'onClose', 'onClosed'])), renderClose(), renderImages(), renderIndex());
};

ImagePreview.defaultProps = {
  loop: true,
  overlay: true,
  showIndex: true,
  images: [],
  swipeDuration: 300,
  startPosition: 0,
  closeIconPosition: 'top-right',
  showIndicators: false,
  maxZoom: 3
};
var _default = ImagePreview;
exports.default = _default;