var __assign = this && this.__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

import { jsx as _jsx } from "react/jsx-runtime";
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import Drawer from "./Drawer";
import DragDrawer from "./DragDrawer";
var drawerApi = {
  open: function open(children, drawerConfig, drag) {
    var overflow = document.body.style.overflow;
    var View = drag ? DragDrawer : Drawer;

    var Draw = function Draw(props) {
      var _a = React.useState(false),
          open = _a[0],
          setOpen = _a[1];

      React.useEffect(function () {
        setTimeout(function () {
          setOpen(true);
        });
      }, []);

      var onClose = function onClose(e) {
        props.onClose && props.onClose(e); // hack drawer会将overflow清空;

        setTimeout(function () {
          document.body.style.overflow = overflow;
        }, (parseFloat((drawerConfig === null || drawerConfig === void 0 ? void 0 : drawerConfig.duration) || '') * 1000 || 300) + 200);
        setOpen(false);
      };

      return _jsx(View, __assign({}, props, {
        open: open,
        level: null,
        onClose: onClose
      }, {
        children: props.children(onClose)
      }), void 0);
    };

    var dom = document.createElement('div');
    document.body.append(dom);

    var _afterVisibleChange = function _afterVisibleChange(visible) {
      var _a;

      if (!visible) {
        ReactDOM.unmountComponentAtNode(dom);
        dom.remove(); // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore

        dom = undefined;
      }

      (_a = drawerConfig === null || drawerConfig === void 0 ? void 0 : drawerConfig.afterVisibleChange) === null || _a === void 0 ? void 0 : _a.call(drawerConfig, visible);
    };

    ReactDOM.render(_jsx(Draw, __assign({}, drawerConfig || {}, {
      children: children,
      afterVisibleChange: _afterVisibleChange
    }), void 0), dom);
  }
};
export default drawerApi;

