function _extends() {
  _extends = Object.assign ? Object.assign.bind() : 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);
}
import React, { useContext, useRef, useState } from "react";
import classnames from "classnames";
import { pick } from "../utils";
import Icon from "../icon";
import Swiper from "../swiper";
import Popup from "../popup";
import ImagePreviewItem from "./ImagePreviewItem";
import ConfigProviderContext from "../config-provider/ConfigProviderContext";
const ImagePreview = (props) => {
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace("image-preview", prefixCls);
  const swiperRef = useRef(null);
  const [active, setActive] = useState(() => props.startPosition);
  const onSwipeChange = (idx) => {
    var _a;
    if (active !== idx) {
      setActive(idx);
      (_a = props.onChange) == null ? void 0 : _a.call(props, idx);
    }
  };
  const renderImages = () => /* @__PURE__ */ React.createElement(Swiper, {
    ref: swiperRef,
    className: classnames(bem("swipe")),
    loop: props.loop,
    autoplayInterval: props.swipeDuration,
    defaultIndex: active,
    onIndexChange: onSwipeChange,
    indicator: props.showIndicators
  }, props.images.map((image, i) => /* @__PURE__ */ React.createElement(Swiper.Item, {
    className: classnames(bem("item")),
    key: image
  }, /* @__PURE__ */ React.createElement(ImagePreviewItem, {
    maxZoom: props.maxZoom,
    image,
    onTap: () => {
      var _a;
      (_a = props.onClose) == null ? void 0 : _a.call(props, {
        url: image,
        index: i
      });
    },
    onZoomChange: (zoom) => {
      var _a, _b;
      if (zoom !== 1) {
        (_a = swiperRef.current) == null ? void 0 : _a.lock();
      } else {
        (_b = swiperRef.current) == null ? void 0 : _b.unlock();
      }
    }
  }))));
  const renderClose = () => {
    if (props.closeable) {
      return /* @__PURE__ */ React.createElement(Icon, {
        name: props.closeIcon,
        className: classnames(bem("close-icon", props.closeIconPosition)),
        onClick: () => {
          var _a;
          return (_a = props.onClose) == null ? void 0 : _a.call(props);
        }
      });
    }
    return null;
  };
  const renderIndex = () => {
    if (props.showIndex) {
      return /* @__PURE__ */ React.createElement("div", {
        className: classnames(bem("index"))
      }, props.indexRender ? props.indexRender({
        index: active,
        len: props.images.length
      }) : `${active + 1} / ${props.images.length}`);
    }
    return null;
  };
  return /* @__PURE__ */ React.createElement(Popup, _extends({
    className: classnames(bem(), props.className),
    overlayClass: classnames(bem("overlay")),
    beforeClose: props.beforeClose
  }, pick(props, ["visible", "overlayStyle", "closeOnPopstate", "onClose", "onClosed"])), renderClose(), renderImages(), renderIndex());
};
ImagePreview.defaultProps = {
  loop: true,
  overlay: true,
  showIndex: true,
  images: [],
  swipeDuration: 300,
  startPosition: 0,
  closeIcon: "clear",
  closeIconPosition: "top-right",
  showIndicators: false,
  maxZoom: 3
};
var stdin_default = ImagePreview;
export {
  stdin_default as default
};
