"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const React = require("react");
const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
const React__default = /* @__PURE__ */ _interopDefaultLegacy(React);
var __assign = globalThis && globalThis.__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);
};
var __read = globalThis && globalThis.__read || function(o, n) {
  var m = typeof Symbol === "function" && o[Symbol.iterator];
  if (!m)
    return o;
  var i = m.call(o), r, ar = [], e;
  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
      ar.push(r.value);
  } catch (error) {
    e = { error };
  } finally {
    try {
      if (r && !r.done && (m = i["return"]))
        m.call(i);
    } finally {
      if (e)
        throw e.error;
    }
  }
  return ar;
};
var __spreadArray = globalThis && globalThis.__spreadArray || function(to, from, pack) {
  if (pack || arguments.length === 2)
    for (var i = 0, l = from.length, ar; i < l; i++) {
      if (ar || !(i in from)) {
        if (!ar)
          ar = Array.prototype.slice.call(from, 0, i);
        ar[i] = from[i];
      }
    }
  return to.concat(ar || Array.prototype.slice.call(from));
};
var PROPS_NEED_TO_BE_PASSED_IN_SUBMENU = ["popup", "triggerProps", "selectable"];
var flatMenuGroup = function(children) {
  var validMenuItems = [];
  React__default.default.Children.forEach(children, function(item) {
    var _a;
    var menuType = (_a = item === null || item === void 0 ? void 0 : item.type) === null || _a === void 0 ? void 0 : _a.menuType;
    if (menuType === "MenuItem" || menuType === "SubMenu") {
      validMenuItems.push(item);
    } else if (menuType === "MenuGroup") {
      validMenuItems = validMenuItems.concat(flatMenuGroup(item.props.children));
    }
  });
  return validMenuItems;
};
var generateInfoMap = function(children, keyPath, result) {
  if (keyPath === void 0) {
    keyPath = [];
  }
  if (result === void 0) {
    result = {};
  }
  var validChildrenList = flatMenuGroup(children);
  validChildrenList.forEach(function(item, index) {
    var _a;
    var _b, _c;
    var key = item.key;
    var menuType = item.type.menuType;
    var _keyPath = __spreadArray([key], __read(keyPath), false);
    var info = {
      keyPath: [],
      prev: ((_b = validChildrenList[index - 1]) === null || _b === void 0 ? void 0 : _b.key) || null,
      next: ((_c = validChildrenList[index + 1]) === null || _c === void 0 ? void 0 : _c.key) || null
    };
    if (index === 0 || index === validChildrenList.length - 1) {
      var parentKey = _keyPath[1];
      var propertyName = index === 0 ? "firstChild" : "lastChild";
      if (parentKey) {
        result[parentKey] = __assign(__assign({}, result[parentKey]), (_a = {}, _a[propertyName] = key, _a));
      }
    }
    switch (menuType) {
      case "SubMenu":
        info.keyPath = _keyPath;
        generateInfoMap(item.props.children, _keyPath, result);
        break;
      case "MenuItem":
        info.keyPath = _keyPath;
        info.disabled = item.props.disabled;
        break;
    }
    result[key] = __assign(__assign({}, result[key]), info);
  });
  return result;
};
var processChildren = function(children, props) {
  return React__default.default.Children.map(children, function(item, index) {
    if (!item || !item.props) {
      return item;
    }
    var isHTMLElement = typeof item.type === "string";
    var isMenuSubComponent = item.type && item.type.menuType;
    if (!isMenuSubComponent && item.props.children) {
      var _props = isHTMLElement ? {} : props;
      return React__default.default.cloneElement(item, __assign(__assign({}, _props), { _key: item.key, children: processChildren(item.props.children, props) }));
    }
    return isHTMLElement ? item : React__default.default.cloneElement(item, __assign(__assign(__assign({}, props), item.props), { _key: item.key || "$menu-" + index }));
  });
};
function isChildrenSelected(children, keys) {
  var find = false;
  function loop(_children) {
    if (!_children || find) {
      return;
    }
    React__default.default.Children.forEach(_children, function(c) {
      if (c && c.props && c.type && !find) {
        var menuType = c.type.menuType;
        var selectable = c.props.selectable;
        if (menuType === "MenuItem") {
          find = keys.indexOf(c.key) !== -1;
        } else if (menuType === "SubMenu" && selectable) {
          find = keys.indexOf(c.key) !== -1;
        }
        if (!find && c.props.children) {
          loop(c.props.children);
        }
      }
    });
  }
  loop(children);
  return find;
}
exports.PROPS_NEED_TO_BE_PASSED_IN_SUBMENU = PROPS_NEED_TO_BE_PASSED_IN_SUBMENU;
exports.generateInfoMap = generateInfoMap;
exports.isChildrenSelected = isChildrenSelected;
exports.processChildren = processChildren;
//# sourceMappingURL=util.js.map
