import { __awaiter, __rest } from "tslib";
import { jsx as _jsx } from "react/jsx-runtime";
/* eslint-disable @typescript-eslint/naming-convention */

import { useEffect, useState } from 'react';
import { Clear } from '@react-vant/icons';
import { noop } from '../utils';
import BaseImagePreview from './ImagePreview';
import { resolveContainer } from '../utils/dom/getContainer';
import { render, unmount } from '../utils/dom/render';
import "./style/index.css";
const defaultConfig = {
  loop: true,
  images: [],
  className: '',
  showIndex: true,
  closeable: false,
  closeIcon: _jsx(Clear, {}),
  startPosition: 0,
  swipeDuration: 300,
  closeOnPopstate: true,
  closeIconPosition: 'top-right'
}; // 可返回用于销毁此弹窗的方法

const open = props => {
  const {
    onClose = noop,
    beforeClose
  } = props,
        restProps = __rest(props, ["onClose", "beforeClose"]);

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

  const TempDialog = () => {
    const [visible, setVisible] = useState(false);
    useEffect(() => {
      setVisible(true);
    }, []);

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

    const _afterClose = p => __awaiter(void 0, void 0, void 0, function* () {
      if ((yield beforeClose === null || beforeClose === void 0 ? void 0 : beforeClose(0)) !== false) {
        destroy(p);
        const unmountResult = unmount(container);

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

        return true;
      }

      return false;
    });

    return _jsx(BaseImagePreview, Object.assign({}, defaultConfig, restProps, {
      visible: visible,
      teleport: () => container,
      onClose: destroy,
      beforeClose: _afterClose
    }));
  };

  render(_jsx(TempDialog, {}), container);
  return destroy;
};

const ImagePreview = Object.assign(BaseImagePreview, {
  open
});
export default ImagePreview;
export { ImagePreview };