import { confirm, withWarning, withConfirm, withDialog, withError, withInfo } from './config';
import { destroyFns } from './destroyFns';
import { DialogBase } from './dialog';
import type { DialogFuncProps } from './types';

type DialogType = typeof DialogBase & DialogFuncProps;

const Dialog = DialogBase as DialogType;

function showDialog(delegated: DialogFuncProps) {
  return confirm(withDialog(delegated));
}

function showWarning(delegated: DialogFuncProps) {
  return confirm(withWarning(delegated));
}

function showConfirm(delegated: DialogFuncProps) {
  return confirm(withConfirm(delegated));
}

function showError(delegated: DialogFuncProps) {
  return confirm(withError(delegated));
}

function showInfo(delegated: DialogFuncProps) {
  return confirm(withInfo(delegated));
}

function destroyAll() {
  while (destroyFns.length) {
    const close = destroyFns.pop();

    if (close) {
      close();
    }
  }
}

export { Dialog, destroyAll, showWarning, showConfirm, showDialog, showError, showInfo };
