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 ReactDOM from 'react-dom';
import { Cross } from '@react-vant/icons';
import { noop } from '../utils';
import BaseDialog from './Dialog';
import { resolveContainer } from '../utils/dom/getContainer';
import "./style/index.css";
const Dialog = BaseDialog; // 可返回用于销毁此弹窗的方法

Dialog.show = props => {
  const defaultOptions = {
    overlay: true,
    closeable: false,
    closeIcon: _jsx(Cross, {}),
    lockScroll: true,
    transition: 'rv-dialog-bounce',
    showConfirmButton: true,
    showCancelButton: false,
    closeOnClickOverlay: false
  };

  const {
    onClosed,
    onCancel = noop,
    onConfirm = noop,
    onClose = noop,
    cancelProps,
    confirmProps
  } = props,
        restProps = __rest(props, ["onClosed", "onCancel", "onConfirm", "onClose", "cancelProps", "confirmProps"]);

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

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

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

    const _afterClose = () => {
      if (onClosed) {
        onClosed();
      }

      const unmountResult = ReactDOM.unmountComponentAtNode(container);

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

    const _onConfirm = e => __awaiter(void 0, void 0, void 0, function* () {
      const i = setTimeout(() => setOkLoading(true));

      if ((yield onConfirm(e)) !== false) {
        clearTimeout(i);
        destroy();
      } else {
        clearTimeout(i);
        setOkLoading(false);
      }
    });

    const _onCancel = (e, clickOverlay) => __awaiter(void 0, void 0, void 0, function* () {
      if (clickOverlay) {
        destroy();
        return;
      }

      const i = setTimeout(() => setCancelLoading(true));

      if ((yield onCancel(e)) !== false) {
        clearTimeout(i);
        destroy();
      } else {
        clearTimeout(i);
        setCancelLoading(false);
      }
    });

    return _jsx(BaseDialog, Object.assign({}, defaultOptions, restProps, {
      visible: visible,
      teleport: () => container,
      cancelProps: Object.assign({
        loading: cancelLoading
      }, cancelProps),
      confirmProps: Object.assign({
        loading: okLoading
      }, confirmProps),
      onClose: destroy,
      onCancel: _onCancel,
      onConfirm: _onConfirm,
      onClosed: _afterClose
    }));
  };

  ReactDOM.render(_jsx(TempDialog, {}), container);
  return destroy;
}; // 可使用 async/await 的方式


Dialog.alert = props => {
  const {
    onConfirm = noop
  } = props;
  return new Promise(resolve => {
    Dialog.show(Object.assign(Object.assign({}, props), {
      onConfirm: e => {
        onConfirm(e);
        resolve(e);
      }
    }));
  });
};

Dialog.confirm = props => {
  const {
    onCancel = noop,
    onConfirm = noop
  } = props;
  return new Promise((resolve, reject) => {
    Dialog.show(Object.assign(Object.assign({
      // 强制显示 OK Btn
      showCancelButton: true
    }, props), {
      onCancel: e => {
        onCancel(e);
        reject();
      },
      onConfirm: e => {
        onConfirm(e);
        resolve(true);
      }
    }));
  });
};

export default Dialog;