import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _regeneratorRuntime from "@babel/runtime/regenerator";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

import { createBEM } from '@zarm-design/bem';
import { getRuntimeConfig } from '../config-provider';
import { show } from './methods';
export var confirm = function confirm(props) {
  var className = props.className,
      _props$animationType = props.animationType,
      animationType = _props$animationType === void 0 ? 'zoom' : _props$animationType,
      confirmText = props.confirmText,
      cancelText = props.cancelText,
      onConfirm = props.onConfirm,
      onCancel = props.onCancel,
      rest = _objectWithoutProperties(props, ["className", "animationType", "confirmText", "cancelText", "onConfirm", "onCancel"]);

  var _getRuntimeConfig = getRuntimeConfig(),
      prefixCls = _getRuntimeConfig.prefixCls,
      locale = _getRuntimeConfig.locale;

  var bem = createBEM('confirm', {
    prefixCls: prefixCls
  });
  return new Promise(function (resolve) {
    var _show = show(_objectSpread(_objectSpread({}, rest), {}, {
      className: bem([className]),
      animationType: animationType,
      actions: [[{
        theme: 'default',
        text: cancelText || (locale === null || locale === void 0 ? void 0 : locale.Modal.cancelText),
        onClick: function () {
          var _onClick = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
            var result;
            return _regeneratorRuntime.wrap(function _callee$(_context) {
              while (1) {
                switch (_context.prev = _context.next) {
                  case 0:
                    result = onCancel === null || onCancel === void 0 ? void 0 : onCancel();

                    if (!(result === false)) {
                      _context.next = 3;
                      break;
                    }

                    return _context.abrupt("return");

                  case 3:
                    close();
                    resolve(false);

                  case 5:
                  case "end":
                    return _context.stop();
                }
              }
            }, _callee);
          }));

          function onClick() {
            return _onClick.apply(this, arguments);
          }

          return onClick;
        }()
      }, {
        text: confirmText || (locale === null || locale === void 0 ? void 0 : locale.Modal.confirmText),
        onClick: function () {
          var _onClick2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
            var result;
            return _regeneratorRuntime.wrap(function _callee2$(_context2) {
              while (1) {
                switch (_context2.prev = _context2.next) {
                  case 0:
                    _context2.next = 2;
                    return onConfirm === null || onConfirm === void 0 ? void 0 : onConfirm();

                  case 2:
                    result = _context2.sent;

                    if (!(result === false)) {
                      _context2.next = 5;
                      break;
                    }

                    return _context2.abrupt("return");

                  case 5:
                    close();
                    resolve(true);

                  case 7:
                  case "end":
                    return _context2.stop();
                }
              }
            }, _callee2);
          }));

          function onClick() {
            return _onClick2.apply(this, arguments);
          }

          return onClick;
        }()
      }]]
    })),
        close = _show.close;
  });
};