"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

function _jsxRuntime() {
  const data = require("react/jsx-runtime");

  _jsxRuntime = function () {
    return data;
  };

  return data;
}

function _react() {
  const data = _interopRequireWildcard(require("react"));

  _react = function () {
    return data;
  };

  return data;
}

function _clsx() {
  const data = _interopRequireDefault(require("clsx"));

  _clsx = function () {
    return data;
  };

  return data;
}

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

const ImagePreview = props => {
  const {
    prefixCls,
    createNamespace
  } = (0, _react().useContext)(_ConfigProviderContext.default);
  const [bem] = createNamespace('image-preview', prefixCls);
  const swiperRef = (0, _react().useRef)(null);
  const [active, setActive] = (0, _react().useState)(() => props.startPosition);

  const onSwipeChange = idx => {
    var _a;

    if (active !== idx) {
      setActive(idx);
      (_a = props.onChange) === null || _a === void 0 ? void 0 : _a.call(props, idx);
    }
  };

  const onClose = (image, i) => {
    var _a;

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

  const renderImages = () => (0, _jsxRuntime().jsx)(_swiper.default, Object.assign({
    ref: swiperRef,
    className: (0, _clsx().default)(bem('swipe')),
    loop: props.loop,
    duration: props.swipeDuration,
    initialSwipe: active,
    onChange: onSwipeChange,
    indicator: props.showIndicators
  }, {
    children: props.images.map((image, i) => (0, _jsxRuntime().jsx)(_swiper.default.Item, Object.assign({
      className: (0, _clsx().default)(bem('item')),
      onClick: () => {
        onClose(image, i);
      }
    }, {
      children: (0, _jsxRuntime().jsx)(_ImagePreviewItem.default, {
        maxZoom: props.maxZoom,
        image: image,
        lazyload: props.lazyload,
        onTap: () => {
          onClose(image, i);
        },
        onZoomChange: zoom => {
          var _a, _b;

          if (zoom !== 1) {
            (_a = swiperRef.current) === null || _a === void 0 ? void 0 : _a.disable();
          } else {
            (_b = swiperRef.current) === null || _b === void 0 ? void 0 : _b.enable();
          }
        }
      })
    }), image))
  }));

  const renderClose = () => {
    if (props.closeable) {
      return _react().default.cloneElement(props.closeIcon, {
        className: (0, _clsx().default)(bem('close-icon', props.closeIconPosition)),
        onClick: props.onClose
      });
    }

    return null;
  };

  const renderIndex = () => {
    if (props.showIndex) {
      return (0, _jsxRuntime().jsx)("div", Object.assign({
        className: (0, _clsx().default)(bem('index'))
      }, {
        children: props.indexRender ? props.indexRender({
          index: active,
          len: props.images.length
        }) : `${active + 1} / ${props.images.length}`
      }));
    }

    return null;
  };

  return (0, _jsxRuntime().jsxs)(_popup.default, Object.assign({
    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']), {
    children: [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;