import { VxeUI } from "../../../@vxe-ui/core/es/index.esm.mjs";
import XEUtils from "xe-utils";
import VxeDrawerComponent, { allActiveDrawers } from "./src/drawer.mjs";
import { dynamicApp, checkDynamic, dynamicStore } from "../dynamics/index.mjs";
function handleDrawer(options) {
  checkDynamic();
  return new Promise((resolve) => {
    if (options && options.id && allActiveDrawers.some((comp) => comp.props.id === options.id)) {
      resolve("exist");
    } else {
      const _onHide = options.onHide;
      const drawerOpts = Object.assign(options, {
        key: XEUtils.uniqueId(),
        modelValue: true,
        onHide(params) {
          const drawerList = dynamicStore.drawers;
          if (_onHide) {
            _onHide(params);
          }
          dynamicStore.drawers = drawerList.filter((item) => item.key !== drawerOpts.key);
          resolve(params.type);
        }
      });
      dynamicStore.drawers.push(drawerOpts);
    }
  });
}
function getDrawer(id) {
  return XEUtils.find(allActiveDrawers, ($drawer) => $drawer.props.id === id);
}
function closeDrawer(id) {
  const drawers = id ? [getDrawer(id)] : allActiveDrawers;
  const restPromises = [];
  drawers.forEach(($drawer) => {
    if ($drawer) {
      restPromises.push($drawer.close());
    }
  });
  return Promise.all(restPromises);
}
function openDrawer(options) {
  return handleDrawer(Object.assign({}, options));
}
const DrawerController = {
  get: getDrawer,
  close: closeDrawer,
  open: openDrawer
};
const VxeDrawer = Object.assign(VxeDrawerComponent, {
  install: function(app) {
    app.component(VxeDrawerComponent.name, VxeDrawerComponent);
  }
});
VxeUI.drawer = DrawerController;
dynamicApp.use(VxeDrawer);
VxeUI.component(VxeDrawerComponent);
const Drawer = VxeDrawer;
export {
  Drawer,
  DrawerController,
  VxeDrawer,
  VxeDrawer as default
};
