import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _extends from "@babel/runtime/helpers/extends";
import React, { useCallback, useContext } from 'react';
import ReactDOM from 'react-dom';
import classNames from 'classnames';
import Icon from '../icon';
import Dialog from './Modal';
import ActionButton from './ActionButton';
import { getConfirmLocale } from './locale';
import ConfigContext from '../config-provider/ConfigContext';
var IS_REACT_16 = !!ReactDOM.createPortal;

var ConfirmDialog = function ConfirmDialog(props) {
  var customizePrefixCls = props.prefixCls,
      modalPrefixCls = props.modalPrefixCls,
      onCancel = props.onCancel,
      onOk = props.onOk,
      close = props.close,
      zIndex = props.zIndex,
      width = props.width,
      style = props.style,
      type = props.type,
      className = props.className,
      afterClose = props.afterClose,
      visible = props.visible,
      keyboard = props.keyboard,
      okText = props.okText,
      cancelText = props.cancelText,
      _props$okCancel = props.okCancel,
      okCancel = _props$okCancel === void 0 ? true : _props$okCancel,
      iconType = props.iconType,
      okType = props.okType,
      _props$maskClosable = props.maskClosable,
      maskClosable = _props$maskClosable === void 0 ? false : _props$maskClosable,
      title = props.title,
      content = props.content,
      okButtonProps = props.okButtonProps,
      cancelButtonProps = props.cancelButtonProps;

  var _useContext = useContext(ConfigContext),
      getPrefixCls = _useContext.getPrefixCls;

  var propOkType = okType || 'primary';
  var prefixCls = getPrefixCls('confirm', customizePrefixCls);
  var propWidth = width || 416;
  var propStyle = style || {};
  var runtimeLocale = getConfirmLocale();
  var propOkText = okText || (okCancel ? runtimeLocale.okText : runtimeLocale.justOkText);
  var propCancelText = cancelText || runtimeLocale.cancelText;
  var classString = classNames(prefixCls, "".concat(prefixCls, "-").concat(type), className);
  var actionButtonProps = {
    okProps: {
      buttonProps: okButtonProps,
      text: propOkText,
      type: propOkType,
      actionFn: onOk,
      closeModal: close
    }
  };

  if (okCancel) {
    actionButtonProps.cancelProps = {
      buttonProps: cancelButtonProps,
      text: propCancelText,
      actionFn: onCancel,
      closeModal: close
    };
  }

  var handleCancel = useCallback(function () {
    close({
      triggerCancel: true
    });
  }, [close]);
  return /*#__PURE__*/React.createElement(Dialog, {
    prefixCls: modalPrefixCls,
    className: classString,
    onCancel: handleCancel,
    visible: visible,
    title: "",
    transitionName: "zoom",
    footer: "",
    maskTransitionName: "fade",
    maskClosable: maskClosable,
    style: propStyle,
    width: propWidth,
    zIndex: zIndex,
    afterClose: afterClose,
    keyboard: keyboard
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-body-wrapper")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-body")
  }, iconType ? /*#__PURE__*/React.createElement(Icon, {
    type: iconType
  }) : null, /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-title")
  }, title), /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-content")
  }, content)), /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-btns")
  }, /*#__PURE__*/React.createElement(ActionButton, _extends({}, actionButtonProps)))));
};

export default function confirm(config) {
  var div = document.createElement('div');
  document.body.appendChild(div);

  function destroy() {
    var unmountResult = ReactDOM.unmountComponentAtNode(div);

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

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    var triggerCancel = args && args.length && args.some(function (param) {
      return param && param.triggerCancel;
    });

    if (config.onCancel && triggerCancel) {
      config.onCancel.apply(config, args);
    }
  }

  function render(props) {
    ReactDOM.render( /*#__PURE__*/React.createElement(ConfirmDialog, _extends({}, props)), div);
  }

  function close() {
    for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
      args[_key2] = arguments[_key2];
    }

    if (IS_REACT_16) {
      render(_objectSpread(_objectSpread({}, config), {}, {
        close: close,
        visible: false,
        afterClose: destroy.bind.apply(destroy, [this].concat(args))
      }));
    } else {
      destroy.apply(void 0, args);
    }
  }

  render(_objectSpread(_objectSpread({}, config), {}, {
    visible: true,
    close: close
  }));
  return {
    destroy: close
  };
}
//# sourceMappingURL=confirm.js.map
