import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
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; }
import "core-js/modules/es.object.to-string.js";
import "core-js/modules/es.promise.js";
import "core-js/modules/es.object.keys.js";
import "core-js/modules/es.symbol.js";
import "core-js/modules/es.array.filter.js";
import "core-js/modules/es.object.get-own-property-descriptor.js";
import "core-js/modules/web.dom-collections.for-each.js";
import "core-js/modules/es.object.get-own-property-descriptors.js";
import { createRoot } from 'react-dom/client';
import CMSModal from './modal';
import { createRef } from 'react';
import { jsx as _jsx } from "react/jsx-runtime";
/**
 * 单例模式使用方式
 *
 * new SDK().api
 * 或
 * SDK.getInstance().api
 */
var SDK = /*#__PURE__*/function () {
  function SDK(_opts) {
    var _this = this;
    _classCallCheck(this, SDK);
    _defineProperty(this, "div", void 0);
    _defineProperty(this, "root", void 0);
    _defineProperty(this, "opts", void 0);
    _defineProperty(this, "modalRef", void 0);
    // 显示
    _defineProperty(this, "show", function (opts) {
      return new Promise(function (resolve) {
        var _this$modalRef;
        var options = _objectSpread(_objectSpread(_objectSpread({}, _this.opts), opts), {}, {
          callback: function callback(data) {
            var _opts$callback;
            opts === null || opts === void 0 ? void 0 : (_opts$callback = opts.callback) === null || _opts$callback === void 0 ? void 0 : _opts$callback.call(opts, data);
            resolve(data);
          }
        });
        _this.div = document.createElement('div');
        document.body.appendChild(_this.div);
        if (!((_this$modalRef = _this.modalRef) !== null && _this$modalRef !== void 0 && _this$modalRef.current)) {
          _this.root = createRoot(_this.div);
          _this.modalRef = /*#__PURE__*/createRef();
          // render
          _this.root.render( /*#__PURE__*/_jsx(CMSModal, {
            options: options,
            defaultVisible: true,
            ref: _this.modalRef
          }));
        } else {
          var _this$modalRef2;
          (_this$modalRef2 = _this.modalRef) === null || _this$modalRef2 === void 0 ? void 0 : _this$modalRef2.current.show(options);
        }
      });
    });
    this.opts = _opts;
  }

  // 销毁
  _createClass(SDK, [{
    key: "destroy",
    value: function destroy() {
      var _this$root, _this$div;
      (_this$root = this.root) === null || _this$root === void 0 ? void 0 : _this$root.unmount();
      (_this$div = this.div) === null || _this$div === void 0 ? void 0 : _this$div.remove();
    }

    // 设置全局参数
  }, {
    key: "setConfig",
    value: function setConfig(opts) {
      this.opts = opts;
    }
  }, {
    key: "hide",
    value: function hide() {
      var _this$modalRef3, _this$modalRef3$curre;
      (_this$modalRef3 = this.modalRef) === null || _this$modalRef3 === void 0 ? void 0 : (_this$modalRef3$curre = _this$modalRef3.current) === null || _this$modalRef3$curre === void 0 ? void 0 : _this$modalRef3$curre.hide();
    }
  }]);
  return SDK;
}();
export { SDK as default };