var _excluded = ["onClose", "beforeClose"];

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

function _await(value, then, direct) {
  if (direct) {
    return then ? then(value) : value;
  }

  if (!value || !value.then) {
    value = Promise.resolve(value);
  }

  return then ? value.then(then) : value;
}

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

/* eslint-disable @typescript-eslint/naming-convention */
import React, { useEffect, useState } from 'react';
import { Clear } from '@react-vant/icons';
import ReactDOM from 'react-dom';
import { noop } from '../utils';
import BaseImagePreview from './ImagePreview';
import { resolveContainer } from '../utils/dom/getContainer';
import "./style/index.css";
var ImagePreview = BaseImagePreview;
var defaultConfig = {
  loop: true,
  images: [],
  className: '',
  showIndex: true,
  closeable: false,
  closeIcon: /*#__PURE__*/React.createElement(Clear, null),
  startPosition: 0,
  swipeDuration: 300,
  closeOnPopstate: true,
  closeIconPosition: 'top-right'
}; // 可返回用于销毁此弹窗的方法

ImagePreview.open = function (props) {
  var _props$onClose = props.onClose,
      onClose = _props$onClose === void 0 ? noop : _props$onClose,
      beforeClose = props.beforeClose,
      restProps = _objectWithoutPropertiesLoose(props, _excluded);

  var userContainer = resolveContainer(props.teleport);
  var container = document.createElement('div');
  userContainer.appendChild(container);
  var destroy = noop;

  var TempDialog = function TempDialog() {
    var _useState = useState(false),
        visible = _useState[0],
        setVisible = _useState[1];

    useEffect(function () {
      setVisible(true);
    }, []);

    destroy = function destroy(p) {
      setVisible(false);
      if (onClose) onClose(p);
    };

    var _afterClose = function _afterClose(p) {
      var _exit = false;
      return _await(beforeClose == null ? void 0 : beforeClose(0), function (_beforeClose) {
        if (_beforeClose !== false) {
          destroy(p);
          var unmountResult = ReactDOM.unmountComponentAtNode(container);

          if (unmountResult && container.parentNode) {
            container.parentNode.removeChild(container);
          }

          _exit = true;
          return true;
        }

        return false;
      });
    };

    return /*#__PURE__*/React.createElement(BaseImagePreview, _extends({}, defaultConfig, restProps, {
      visible: visible,
      teleport: function teleport() {
        return container;
      },
      onClose: destroy,
      beforeClose: _afterClose
    }));
  };

  ReactDOM.render( /*#__PURE__*/React.createElement(TempDialog, null), container);
  return destroy;
};

export default ImagePreview;