"use strict";

require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.defaultRenderLogoAndTitle = exports.defaultRenderLogo = exports.defaultRenderCollapsedButton = exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _icons = require("@wii/icons");
var _menu = _interopRequireDefault(require("../../../menu"));
var _layout = _interopRequireDefault(require("../../../layout"));
var _BaseMenu = _interopRequireDefault(require("./BaseMenu"));
var _Counter = _interopRequireDefault(require("./Counter"));
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const {
  Sider
} = _layout.default;
const defaultRenderLogo = logo => {
  if (typeof logo === 'string') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)("img", {
      src: logo,
      alt: "logo"
    });
  }
  if (typeof logo === 'function') {
    return logo();
  }
  return logo;
};
exports.defaultRenderLogo = defaultRenderLogo;
const defaultRenderLogoAndTitle = (props, renderKey = 'menuHeaderRender') => {
  const {
    logo,
    title,
    layout
  } = props;
  const renderFunction = props[renderKey || ''];
  if (renderFunction === false) {
    return null;
  }
  const logoDom = defaultRenderLogo(logo);
  const titleDom = /*#__PURE__*/(0, _jsxRuntime.jsx)("h1", {
    children: title
  });
  if (renderFunction) {
    // when collapsed, no render title
    return renderFunction(logoDom, props.collapsed ? null : titleDom, props);
  }
  if (layout === 'mix' && renderKey === 'menuHeaderRender') {
    return null;
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("a", {
    children: [logoDom, props.collapsed ? null : titleDom]
  });
};
exports.defaultRenderLogoAndTitle = defaultRenderLogoAndTitle;
const defaultRenderCollapsedButton = collapsed => collapsed ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.MenuUnfoldOutlined, {}) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.MenuFoldOutlined, {});
exports.defaultRenderCollapsedButton = defaultRenderCollapsedButton;
const SiderMenu = props => {
  const {
    collapsed,
    fixSiderbar,
    menuFooterRender,
    onCollapse,
    theme,
    siderWidth,
    isMobile,
    onMenuHeaderClick,
    breakpoint = 'lg',
    style,
    layout,
    menuExtraRender = false,
    collapsedButtonRender = defaultRenderCollapsedButton,
    links,
    menuContentRender,
    prefixCls,
    onOpenChange,
    headerHeight,
    logoStyle
  } = props;
  const baseClassName = `${prefixCls}-sider`;
  const {
    flatMenuKeys
  } = _Counter.default.useContainer();
  const siderClassName = (0, _classnames.default)(`${baseClassName}`, {
    [`${baseClassName}-fixed`]: fixSiderbar,
    [`${baseClassName}-layout-${layout}`]: layout && !isMobile,
    [`${baseClassName}-light`]: theme === 'light'
  });
  const headerDom = defaultRenderLogoAndTitle(props);
  const extraDom = menuExtraRender && menuExtraRender(props);
  const menuDom = menuContentRender !== false && flatMenuKeys && /*#__PURE__*/(0, _jsxRuntime.jsx)(_BaseMenu.default, {
    ...props,
    mode: "inline",
    handleOpenChange: onOpenChange,
    style: {
      width: '100%'
    },
    className: `${baseClassName}-menu`
  });
  const linkMenuItems = (0, _react.useMemo)(() => {
    const items = (links || []).map((node, index) => ({
      label: node,
      key: index,
      className: `${baseClassName}-link`
    }));
    if (collapsedButtonRender && !isMobile) {
      items.push({
        className: `${baseClassName}-collapsed-button`,
        // @ts-ignore
        title: false,
        onClick: () => {
          if (onCollapse) {
            onCollapse(!collapsed);
          }
        },
        key: '_collapsedBtn',
        label: collapsedButtonRender(collapsed)
      });
    }
    return items;
  }, [baseClassName, collapsed, collapsedButtonRender, isMobile, links, onCollapse]);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [fixSiderbar && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      style: {
        width: collapsed ? 64 : siderWidth,
        overflow: 'hidden',
        flex: `0 0 ${collapsed ? 64 : siderWidth}px`,
        maxWidth: collapsed ? 64 : siderWidth,
        minWidth: collapsed ? 64 : siderWidth,
        transition: 'background-color 0.3s, min-width 0.3s, max-width 0.3s cubic-bezier(0.645, 0.045, 0.355, 1)',
        ...style
      }
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(Sider, {
      collapsible: true,
      trigger: null,
      collapsed: collapsed,
      breakpoint: breakpoint === false ? undefined : breakpoint,
      onCollapse: collapse => {
        if (!isMobile) {
          if (onCollapse) {
            onCollapse(collapse);
          }
        }
      },
      collapsedWidth: 64,
      style: {
        overflow: 'hidden',
        paddingTop: layout === 'mix' && !isMobile ? headerHeight : undefined,
        ...style
      },
      width: siderWidth,
      theme: theme,
      className: siderClassName,
      children: [headerDom && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: (0, _classnames.default)(`${baseClassName}-logo`, {
          [`${baseClassName}-collapsed`]: collapsed
        }),
        onClick: layout !== 'mix' ? onMenuHeaderClick : undefined,
        id: "logo",
        style: logoStyle,
        children: headerDom
      }), extraDom && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: `${baseClassName}-extra ${!headerDom && `${baseClassName}-extra-no-logo`}`,
        children: extraDom
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        style: {
          flex: 1,
          overflowY: 'auto',
          overflowX: 'hidden'
        },
        children: menuContentRender ? menuContentRender(props, menuDom) : menuDom
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: `${baseClassName}-links`,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_menu.default, {
          theme: theme,
          className: `${baseClassName}-link-menu`,
          selectedKeys: [],
          openKeys: [],
          mode: "inline",
          items: linkMenuItems
        })
      }), menuFooterRender && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: (0, _classnames.default)(`${baseClassName}-footer`, {
          [`${baseClassName}-footer-collapsed`]: !collapsed
        }),
        children: menuFooterRender(props)
      })]
    })]
  });
};
var _default = SiderMenu;
exports.default = _default;