"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireDefault(require("react"));

var _reactDom = _interopRequireDefault(require("react-dom"));

var _context = _interopRequireDefault(require("./context"));

var _drawerWrapper = _interopRequireDefault(require("./drawerWrapper"));

var _dom = require("./utils/dom");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); 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 = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

var DrawerManager = /*#__PURE__*/function () {
  function DrawerManager() {
    var _this = this;

    _classCallCheck(this, DrawerManager);

    this.history = [];
    this.uid = 0;
    this.typeMap = {};

    this.open = function (type) {
      var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
      var drawerProps = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
      if (!type) throw new Error("need provide drawer type");
      var Component = typeof type === "string" ? _this.typeMap[type] : type;
      if (!Component) throw new Error("the type is not regisited");

      if (window.titaTracker) {
        window.titaTracker("action").record({
          actionName: type,
          actionGroup: "打开推屏",
          productName: "OKR"
        });
      }

      return new Promise(function (resolve) {
        clearTimeout(_this.timer);
        _this.timer = setTimeout(function () {
          var uid = _this.uid;
          _this.uid += 1;
          var extendedDrawerProps = drawerProps;

          if (_this.history.length > 0) {
            extendedDrawerProps = _objectSpread(_objectSpread({}, _this.history[_this.history.length - 1].extendedDrawerProps), extendedDrawerProps);
          }

          _this.history.push({
            Component: Component,
            props: props,
            extendedDrawerProps: extendedDrawerProps,
            resolve: resolve,
            key: uid
          });

          _this.drawer.open( /*#__PURE__*/_react["default"].createElement(Component, _extends({
            key: uid
          }, props)), extendedDrawerProps);

          if (_this.history.length === 1) {
            document.addEventListener("mousedown", _this.autoClose);
          }
        }, 200);
      });
    };

    this.goBack = function () {
      if (_this.history.length > 1) {
        var current = _this.history.pop();

        current.resolve();
        var _this$history = _this.history[_this.history.length - 1],
            Component = _this$history.Component,
            props = _this$history.props,
            extendedDrawerProps = _this$history.extendedDrawerProps,
            key = _this$history.key;

        _this.drawer.open( /*#__PURE__*/_react["default"].createElement(Component, _extends({
          key: key
        }, props)), extendedDrawerProps);
      } else {
        _this.close();
      }
    };

    this.close = function () {
      _this.history.reverse().forEach(function (_ref) {
        var resolve = _ref.resolve;
        return resolve();
      });

      _this.history = [];

      _this.drawer.close();

      document.removeEventListener("mousedown", _this.autoClose);
    };

    this.autoClose = function (e) {
      // if (!e.target.clientHeight) return;
      if (_this.el && _this.el.contains(e.target)) {
        return;
      }

      if ((0, _dom.hasAncestorElement)(e.target, "document-mouse-event-ignore,rc-tooltip,titaui-popup,rc-dialog-root,common-modal,in-modal,task-hot-add")) {
        return;
      }

      _this.close();
    };

    this.regist = function (type, component) {
      _this.typeMap[type] = component;
    };
  }

  _createClass(DrawerManager, [{
    key: "drawer",
    get: function get() {
      var _this2 = this;

      if (this._drawer) return this._drawer;

      _reactDom["default"].render( /*#__PURE__*/_react["default"].createElement(_context["default"].Provider, {
        value: {
          getCanBack: function getCanBack() {
            return _this2.history.length > 1;
          },
          goBack: function goBack() {
            return _this2.goBack();
          },
          close: function close() {
            return _this2.close();
          },
          saveProps: function saveProps() {
            var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
            var current = _this2.history[_this2.history.length - 1];

            if (current) {
              current.props = _objectSpread(_objectSpread({}, current.props), props);
            } else {
              current.props = _objectSpread({}, props);
            }
          }
        }
      }, /*#__PURE__*/_react["default"].createElement(_drawerWrapper["default"], {
        ref: function ref(r) {
          return _this2._drawer = r;
        },
        getContainer: function getContainer() {
          return _this2.el;
        }
      })), this.getEl());

      return this._drawer;
    }
  }, {
    key: "getEl",
    value: function getEl() {
      if (!this.el || !this.el.parentNode) {
        this.el = document.createElement("div");
        document.body.appendChild(this.el);
      }

      return this.el;
    }
  }]);

  return DrawerManager;
}();

var rootManager = new DrawerManager();
var _default = rootManager;
exports["default"] = _default;