"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.MultiImageViewer = exports.ImageViewer = void 0;
var _classnames = _interopRequireDefault(require("classnames"));
var _react = _interopRequireWildcard(require("react"));
var _renderToContainer = require("../../utils/render-to-container");
var _withDefaultProps = require("../../utils/with-default-props");
var _mask = _interopRequireDefault(require("../mask"));
var _safeArea = _interopRequireDefault(require("../safe-area"));
var _slide = require("./slide");
var _slides = require("./slides");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const classPrefix = `adm-image-viewer`;
const defaultProps = {
  maxZoom: 3,
  getContainer: null,
  visible: false
};
const ImageViewer = p => {
  var _a, _b, _c, _d;
  const props = (0, _withDefaultProps.mergeProps)(defaultProps, p);
  const node = _react.default.createElement(_mask.default, {
    visible: props.visible,
    afterClose: props === null || props === void 0 ? void 0 : props.afterClose,
    className: (_a = props === null || props === void 0 ? void 0 : props.classNames) === null || _a === void 0 ? void 0 : _a.mask,
    onMaskClick: (_b = props.mask) === null || _b === void 0 ? void 0 : _b.onClick,
    disableBodyScroll: false,
    opacity: 'thick',
    destroyOnClose: true
  }, _react.default.createElement("div", {
    className: (0, _classnames.default)(`${classPrefix}-content`, (_c = props === null || props === void 0 ? void 0 : props.classNames) === null || _c === void 0 ? void 0 : _c.body)
  }, (props.image || typeof props.imageRender === 'function') && _react.default.createElement(_slide.Slide, {
    image: props.image,
    onTap: props.onClose,
    maxZoom: props.maxZoom,
    imageRender: props.imageRender
  })), props.image && _react.default.createElement("div", {
    className: `${classPrefix}-footer`
  }, (_d = props.renderFooter) === null || _d === void 0 ? void 0 : _d.call(props, props.image), _react.default.createElement(_safeArea.default, {
    position: 'bottom'
  })));
  return (0, _renderToContainer.renderToContainer)(props.getContainer, node);
};
exports.ImageViewer = ImageViewer;
const multiDefaultProps = Object.assign(Object.assign({}, defaultProps), {
  defaultIndex: 0
});
const MultiImageViewer = exports.MultiImageViewer = (0, _react.forwardRef)((p, ref) => {
  var _a, _b, _c, _d;
  const props = (0, _withDefaultProps.mergeProps)(multiDefaultProps, p);
  const [index, setIndex] = (0, _react.useState)(props.defaultIndex);
  const slidesRef = (0, _react.useRef)(null);
  (0, _react.useImperativeHandle)(ref, () => ({
    swipeTo: (index, immediate) => {
      var _a;
      setIndex(index);
      (_a = slidesRef.current) === null || _a === void 0 ? void 0 : _a.swipeTo(index, immediate);
    }
  }));
  const onSlideChange = (0, _react.useCallback)(newIndex => {
    var _a;
    if (newIndex === index) return;
    setIndex(newIndex);
    (_a = props.onIndexChange) === null || _a === void 0 ? void 0 : _a.call(props, newIndex);
  }, [props.onIndexChange, index]);
  const node = _react.default.createElement(_mask.default, {
    visible: props.visible,
    afterClose: props === null || props === void 0 ? void 0 : props.afterClose,
    className: (_a = props === null || props === void 0 ? void 0 : props.classNames) === null || _a === void 0 ? void 0 : _a.mask,
    onMaskClick: (_b = props.mask) === null || _b === void 0 ? void 0 : _b.onClick,
    disableBodyScroll: false,
    opacity: 'thick',
    destroyOnClose: true
  }, _react.default.createElement("div", {
    className: (0, _classnames.default)(`${classPrefix}-content`, (_c = props === null || props === void 0 ? void 0 : props.classNames) === null || _c === void 0 ? void 0 : _c.body)
  }, props.images && _react.default.createElement(_slides.Slides, {
    ref: slidesRef,
    defaultIndex: index,
    onIndexChange: onSlideChange,
    images: props.images,
    onTap: props.onClose,
    maxZoom: props.maxZoom,
    imageRender: props.imageRender
  })), props.images && _react.default.createElement("div", {
    className: `${classPrefix}-footer`
  }, (_d = props.renderFooter) === null || _d === void 0 ? void 0 : _d.call(props, props.images[index], index), _react.default.createElement(_safeArea.default, {
    position: 'bottom'
  })));
  return (0, _renderToContainer.renderToContainer)(props.getContainer, node);
});