var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
  default: () => stdin_default
});
module.exports = __toCommonJS(stdin_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_utils = require("../utils");
var import_icon = __toESM(require("../icon"));
var import_swiper = __toESM(require("../swiper"));
var import_popup = __toESM(require("../popup"));
var import_ImagePreviewItem = __toESM(require("./ImagePreviewItem"));
var import_ConfigProviderContext = __toESM(require("../config-provider/ConfigProviderContext"));
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);
}
const ImagePreview = (props) => {
  const {
    prefixCls,
    createNamespace
  } = (0, import_react.useContext)(import_ConfigProviderContext.default);
  const [bem] = createNamespace("image-preview", prefixCls);
  const swiperRef = (0, import_react.useRef)(null);
  const [active, setActive] = (0, import_react.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__ */ import_react.default.createElement(import_swiper.default, {
    ref: swiperRef,
    className: (0, import_classnames.default)(bem("swipe")),
    loop: props.loop,
    autoplayInterval: props.swipeDuration,
    defaultIndex: active,
    onIndexChange: onSwipeChange,
    indicator: props.showIndicators
  }, props.images.map((image, i) => /* @__PURE__ */ import_react.default.createElement(import_swiper.default.Item, {
    className: (0, import_classnames.default)(bem("item")),
    key: image
  }, /* @__PURE__ */ import_react.default.createElement(import_ImagePreviewItem.default, {
    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__ */ import_react.default.createElement(import_icon.default, {
        name: props.closeIcon,
        className: (0, import_classnames.default)(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__ */ import_react.default.createElement("div", {
        className: (0, import_classnames.default)(bem("index"))
      }, props.indexRender ? props.indexRender({
        index: active,
        len: props.images.length
      }) : `${active + 1} / ${props.images.length}`);
    }
    return null;
  };
  return /* @__PURE__ */ import_react.default.createElement(import_popup.default, _extends({
    className: (0, import_classnames.default)(bem(), props.className),
    overlayClass: (0, import_classnames.default)(bem("overlay")),
    beforeClose: props.beforeClose
  }, (0, import_utils.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;
