import React, { useEffect } from 'react';
import classNames from 'classnames';
import Omit from 'omit.js';
import { getFlatMenus } from '@umijs/route-utils';
import Drawer from '../../../drawer';
import SiderMenu from './SiderMenu';
import MenuCounter from './Counter';
import { jsx as _jsx } from "react/jsx-runtime";
const SiderMenuWrapper = props => {
  const {
    isMobile,
    menuData,
    siderWidth,
    collapsed,
    onCollapse,
    style,
    className,
    hide,
    getContainer,
    prefixCls,
    matchMenuKeys
  } = props;
  const {
    setFlatMenuKeys
  } = MenuCounter.useContainer();
  useEffect(() => {
    if (!menuData || menuData.length < 1) {
      return () => null;
    }
    // 当 menu data 改变的时候重新计算这两个参数
    const newFlatMenus = getFlatMenus(menuData);
    const animationFrameId = requestAnimationFrame(() => {
      setFlatMenuKeys(Object.keys(newFlatMenus));
    });
    return () => window.cancelAnimationFrame && window.cancelAnimationFrame(animationFrameId);
  }, [matchMenuKeys.join('-')]);
  useEffect(() => {
    if (isMobile === true) {
      if (onCollapse) {
        onCollapse(true);
      }
    }
  }, [isMobile]);
  const omitProps = Omit(props, ['className', 'style']);
  if (hide) {
    return null;
  }
  return isMobile ? /*#__PURE__*/_jsx(Drawer, {
    visible: !collapsed,
    placement: "left",
    className: classNames(`${prefixCls}-drawer-sider`, className),
    onClose: () => onCollapse && onCollapse(true),
    style: {
      padding: 0,
      height: '100vh',
      ...style
    },
    getContainer: getContainer,
    width: siderWidth,
    bodyStyle: {
      height: '100vh',
      padding: 0,
      display: 'flex',
      flexDirection: 'row'
    },
    children: /*#__PURE__*/_jsx(SiderMenu, {
      ...omitProps,
      className: classNames(`${prefixCls}-sider`, className),
      collapsed: isMobile ? false : collapsed,
      splitMenus: false
    })
  }) : /*#__PURE__*/_jsx(SiderMenu, {
    className: classNames(`${prefixCls}-sider`, className),
    ...omitProps,
    style: style
  });
};
SiderMenuWrapper.defaultProps = {
  onCollapse: () => undefined
};
export default SiderMenuWrapper;