function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useMemo } from "react";
import { Menu, Divider } from "antd";
import { CheckOutlined } from "@ant-design/icons";
import { useControllableValue } from "ahooks";
import "./index.less";
var prefix = "pisell-lowcode-";
/**
 * 内嵌展示的菜单 只支持两级
 * @param props
 * @constructor
 */
var InlineMenu = function InlineMenu(props) {
  var items = props.items;
  var _useControllableValue = useControllableValue(props),
    _useControllableValue2 = _slicedToArray(_useControllableValue, 2),
    state = _useControllableValue2[0],
    setState = _useControllableValue2[1];
  var value = useMemo(function () {
    return (state || "").split(':');
  }, [state]);
  var childItems = useMemo(function () {
    var _items$find;
    return ((_items$find = items.find(function (item) {
      var _item$key;
      return ((_item$key = item.key) === null || _item$key === void 0 ? void 0 : _item$key.toString()) === (value === null || value === void 0 ? void 0 : value[0]);
    })) === null || _items$find === void 0 ? void 0 : _items$find.children) || items[0].children;
  }, [value, items]);
  var handleParentClick = function handleParentClick(detail) {
    var _detail$key2;
    var children = items.find(function (item) {
      var _item$key2, _detail$key;
      return ((_item$key2 = item.key) === null || _item$key2 === void 0 ? void 0 : _item$key2.toString()) === ((_detail$key = detail.key) === null || _detail$key === void 0 ? void 0 : _detail$key.toString());
    });
    var defaultKey = children.children[0].key;
    setState(((_detail$key2 = detail.key) === null || _detail$key2 === void 0 ? void 0 : _detail$key2.toString()) === value[0] ? "" : defaultKey, {
      key: defaultKey,
      item: children.children[0],
      keyPath: [defaultKey, detail.key]
    });
  };
  var handleChildrenClick = function handleChildrenClick(detail) {
    var _detail$key3;
    var parent = items.find(function (item) {
      return item.children.some(function (c) {
        return c.key === detail.key;
      });
    });
    setState((_detail$key3 = detail.key) === null || _detail$key3 === void 0 ? void 0 : _detail$key3.toString(), {
      key: detail.key,
      item: detail.item,
      keyPath: [detail.key, parent.key]
    });
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefix, "inline-menu-wrap")
  }, /*#__PURE__*/React.createElement(Menu, {
    items: items.map(function (item) {
      return {
        key: item.key,
        label: /*#__PURE__*/React.createElement("span", {
          className: "".concat(prefix, "menu-item-label-wrap")
        }, /*#__PURE__*/React.createElement("span", {
          className: "".concat(prefix, "menu-item-label")
        }, item.label), item.key === (value === null || value === void 0 ? void 0 : value[0]) && /*#__PURE__*/React.createElement(CheckOutlined, null))
      };
    }),
    onClick: handleParentClick,
    selectedKeys: [value === null || value === void 0 ? void 0 : value[0]]
  }), /*#__PURE__*/React.createElement(Divider, {
    style: {
      margin: "6px 0"
    }
  }), /*#__PURE__*/React.createElement(Menu, {
    className: "".concat(prefix, "sort-sub-menu-popup-wrap"),
    items: childItems,
    onClick: handleChildrenClick,
    selectedKeys: [state]
  }));
};
export default InlineMenu;