"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = render;

var _regenerator = _interopRequireDefault(require("@babel/runtime-corejs2/regenerator"));

require("regenerator-runtime/runtime");

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/asyncToGenerator"));

require("core-js/modules/web.dom.iterable");

var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/objectWithoutPropertiesLoose"));

var _promise = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/promise"));

var _keys = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/keys"));

var _assign = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/assign"));

var _vue = _interopRequireDefault(require("vue"));

/*
 * @Author: lijianzhang
 * @Date: 2019-02-18 17:40:00
 * @Last Modified by: lijianzhang
 * @Last Modified time: 2019-02-27 00:56:50
 * 将vue组建建改成Promise方法渲染的形式
 */
function render(component, defaultData) {
  var VueComponent = _vue.default.extend(component);

  return function (data) {
    if (data === void 0) {
      data = {};
    }

    var vm = new VueComponent();
    var _data = data,
        on = _data.on,
        onOk = _data.onOk,
        onCancel = _data.onCancel,
        other = (0, _objectWithoutPropertiesLoose2.default)(_data, ["on", "onOk", "onCancel"]);
    (0, _assign.default)(vm, defaultData, other);

    if (on) {
      (0, _keys.default)(on).forEach(function (key) {
        vm.$on(key, on[key]);
      });
    }

    vm.run = function () {
      vm.$mount();
      vm.visible = true;
      var done = false;
      return new _promise.default(function (resolve) {
        vm.onOk =
        /*#__PURE__*/
        function () {
          var _ref = (0, _asyncToGenerator2.default)(
          /*#__PURE__*/
          _regenerator.default.mark(function _callee(data) {
            var res;
            return _regenerator.default.wrap(function _callee$(_context) {
              while (1) {
                switch (_context.prev = _context.next) {
                  case 0:
                    if (!onOk) {
                      _context.next = 6;
                      break;
                    }

                    _context.next = 3;
                    return onOk(data);

                  case 3:
                    res = _context.sent;

                    if (!(res === false)) {
                      _context.next = 6;
                      break;
                    }

                    return _context.abrupt("return");

                  case 6:
                    done = true;
                    resolve(data || true);

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

          return function (_x) {
            return _ref.apply(this, arguments);
          };
        }();

        vm.onCancel =
        /*#__PURE__*/
        (0, _asyncToGenerator2.default)(
        /*#__PURE__*/
        _regenerator.default.mark(function _callee2() {
          var res;
          return _regenerator.default.wrap(function _callee2$(_context2) {
            while (1) {
              switch (_context2.prev = _context2.next) {
                case 0:
                  if (!onCancel) {
                    _context2.next = 6;
                    break;
                  }

                  _context2.next = 3;
                  return onCancel();

                case 3:
                  res = _context2.sent;

                  if (!(res === false)) {
                    _context2.next = 6;
                    break;
                  }

                  return _context2.abrupt("return");

                case 6:
                  done = true;
                  resolve(false);

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

        vm.afterClose = function () {
          if (done) vm.$destroy();
        };
      }).then(function (v) {
        if (!vm.visible) vm.$destroy();
        vm.visible = false;
        return v;
      });
    };

    vm.update = function (data) {
      if (data === void 0) {
        data = {};
      }

      (0, _assign.default)(vm, data);
    };

    return vm;
  };
}