"use strict";

require("core-js/modules/es.symbol.js");
require("core-js/modules/es.array.filter.js");
require("core-js/modules/es.array.push.js");
require("core-js/modules/es.object.define-properties.js");
require("core-js/modules/es.object.define-property.js");
require("core-js/modules/es.object.get-own-property-descriptor.js");
require("core-js/modules/es.object.get-own-property-descriptors.js");
require("core-js/modules/es.object.keys.js");
require("core-js/modules/es.weak-map.js");
require("core-js/modules/esnext.iterator.filter.js");
require("core-js/modules/esnext.weak-map.delete-all.js");
require("core-js/modules/esnext.weak-map.emplace.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FcrDialogApi = void 0;
require("core-js/modules/es.array.for-each.js");
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/es.map.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/esnext.iterator.constructor.js");
require("core-js/modules/esnext.iterator.for-each.js");
require("core-js/modules/esnext.map.delete-all.js");
require("core-js/modules/esnext.map.emplace.js");
require("core-js/modules/esnext.map.every.js");
require("core-js/modules/esnext.map.filter.js");
require("core-js/modules/esnext.map.find.js");
require("core-js/modules/esnext.map.find-key.js");
require("core-js/modules/esnext.map.includes.js");
require("core-js/modules/esnext.map.key-of.js");
require("core-js/modules/esnext.map.map-keys.js");
require("core-js/modules/esnext.map.map-values.js");
require("core-js/modules/esnext.map.merge.js");
require("core-js/modules/esnext.map.reduce.js");
require("core-js/modules/esnext.map.some.js");
require("core-js/modules/esnext.map.update.js");
require("core-js/modules/web.dom-collections.for-each.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _classnames = _interopRequireDefault(require("classnames"));
var _react = _interopRequireDefault(require("react"));
var _reactDom = _interopRequireWildcard(require("react-dom"));
var _uuid = require("uuid");
var _ = require(".");
var _jsxRuntime = require("react/jsx-runtime");
var _FcrDialogApi;
var _excluded = ["rootClassName", "content", "type", "children"];
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var DialogType = /*#__PURE__*/function (DialogType) {
  DialogType["CONFIRM"] = "confirm";
  DialogType["INFO"] = "info";
  DialogType["ERROR"] = "error";
  DialogType["WARNING"] = "warning";
  DialogType["SUCCESS"] = "success";
  DialogType["CLASS"] = "class";
  DialogType["INJECT"] = "inject";
  return DialogType;
}(DialogType || {});
var FcrDialog = function FcrDialog(props) {
  var rootClassName = props.rootClassName,
    content = props.content,
    type = props.type,
    children = props.children,
    other = (0, _objectWithoutProperties2["default"])(props, _excluded);
  switch (type) {
    case 'confirm':
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_.FcrConfirmDialog, _objectSpread(_objectSpread({
        classNames: (0, _classnames["default"])('confirm-dialog', rootClassName)
      }, other), {}, {
        children: content || children
      }));
    case 'warning':
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_.FcrConfirmDialog, _objectSpread(_objectSpread({
        classNames: (0, _classnames["default"])('warning-dialog', rootClassName)
      }, other), {}, {
        cancelButtonVisible: false,
        children: content || children
      }));
    case 'inject':
      return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: (0, _classnames["default"])('inject-dialog', rootClassName),
        children: content || children
      });
    default:
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_.BaseDialog, _objectSpread(_objectSpread({
        classNames: (0, _classnames["default"])('info-dialog', rootClassName)
      }, other), {}, {
        children: content || children
      }));
  }
};
var FcrDialogApi = exports.FcrDialogApi = /*#__PURE__*/function () {
  function FcrDialogApi() {
    (0, _classCallCheck2["default"])(this, FcrDialogApi);
  }
  return (0, _createClass2["default"])(FcrDialogApi, null, [{
    key: "_createDialog",
    value: function _createDialog(config, type) {
      if (config.id && this._dialogs.has(config.id)) {
        return;
      }
      if (!config.id) config.id = (0, _uuid.v4)();
      var newConfig = _objectSpread(_objectSpread({}, config), {}, {
        dialogProps: _objectSpread(_objectSpread({}, config.dialogProps), {}, {
          type: type,
          visible: true
        })
      });
      this._dialogs.set(newConfig.id || config.id, newConfig);
      this._render(newConfig);
    }
  }, {
    key: "open",
    value: function open(config) {
      this._createDialog(config, DialogType.INFO);
    }
  }, {
    key: "injectOpen",
    value: function injectOpen(config) {
      this._createDialog(config, DialogType.INJECT);
    }
  }, {
    key: "confirm",
    value: function confirm(config) {
      this._createDialog(config, DialogType.CONFIRM);
    }
  }, {
    key: "warning",
    value: function warning(config) {
      this._createDialog(config, DialogType.WARNING);
    }
  }, {
    key: "info",
    value: function info(config) {
      this._createDialog(config, DialogType.INFO);
    }
  }, {
    key: "close",
    value: function close(id) {
      var dialog = this._dialogs.get(id);
      if (dialog) {
        this._unmountDialog(dialog);
        this._dialogs["delete"](id);
      }
    }
  }, {
    key: "closeAll",
    value: function closeAll() {
      this._dialogs.forEach(this._unmountDialog);
      this._dialogs.clear();
    }
  }]);
}();
_FcrDialogApi = FcrDialogApi;
(0, _defineProperty2["default"])(FcrDialogApi, "_dialogs", new Map());
(0, _defineProperty2["default"])(FcrDialogApi, "_dialogContainerCls", 'fcr-dialog-container');
(0, _defineProperty2["default"])(FcrDialogApi, "_unmountDialog", function (dialog) {
  if (dialog.portal) {
    (0, _reactDom.unmountComponentAtNode)(dialog.portal);
    if (dialog.portal.parentNode) {
      dialog.portal.parentNode.removeChild(dialog.portal);
    }
  }
});
(0, _defineProperty2["default"])(FcrDialogApi, "_render", function (dialog) {
  dialog.portal = document.createElement('div');
  dialog.portal.className = _FcrDialogApi._dialogContainerCls;
  document.body.appendChild(dialog.portal);
  _reactDom["default"].render(/*#__PURE__*/(0, _jsxRuntime.jsx)(FcrDialog, _objectSpread(_objectSpread({}, dialog.dialogProps), {}, {
    onClose: function onClose() {
      if (dialog.id) {
        _FcrDialogApi.close(dialog.id);
      } else {
        _FcrDialogApi.closeAll();
      }
    }
  })), dialog.portal);
});