var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
  default: () => stdin_default,
  getMatchMenu: () => getMatchMenu,
  getMenuMatches: () => getMenuMatches
});
module.exports = __toCommonJS(stdin_exports);
var import_path_to_regexp = __toESM(require("path-to-regexp"));
var import_getFlatMenus = __toESM(require("./getFlatMenus"));
var import_transformRoute = require("./transformRoute");
function getMenuMatches(flatMenuKeys = [], path, exact) {
  return flatMenuKeys.filter((item) => {
    if (item === "/" && path === "/") {
      return true;
    }
    if (item !== "/" && item !== "/*" && item && !(0, import_transformRoute.isUrl)(item)) {
      const pathKey = (0, import_transformRoute.stripQueryStringAndHashFromPath)(item);
      try {
        if (exact) {
          if ((0, import_path_to_regexp.default)(`${pathKey}`).test(path)) {
            return true;
          }
        }
        if ((0, import_path_to_regexp.default)(`${pathKey}`, []).test(path)) {
          return true;
        }
        if ((0, import_path_to_regexp.default)(`${pathKey}/(.*)`).test(path)) {
          return true;
        }
      } catch (error) {
      }
    }
    return false;
  }).sort((a, b) => {
    if (a === path) {
      return 10;
    }
    if (b === path) {
      return -10;
    }
    return a.substr(1).split("/").length - b.substr(1).split("/").length;
  });
}
function getMatchMenu(pathname, menuData, fullKeys, exact) {
  const flatMenus = (0, import_getFlatMenus.default)(menuData);
  const flatMenuKeys = Object.keys(flatMenus);
  let menuPathKeys = getMenuMatches(flatMenuKeys, pathname || "/", exact);
  if (!menuPathKeys || menuPathKeys.length < 1) {
    return [];
  }
  if (!fullKeys) {
    menuPathKeys = [menuPathKeys[menuPathKeys.length - 1]];
  }
  return menuPathKeys.map((menuPathKey) => {
    const menuItem = flatMenus[menuPathKey] || {
      pro_layout_parentKeys: "",
      key: ""
    };
    const map = /* @__PURE__ */ new Map();
    const parentItems = (menuItem.pro_layout_parentKeys || []).map((key) => {
      if (map.has(key)) {
        return null;
      }
      map.set(key, true);
      return flatMenus[key];
    }).filter((item) => item);
    if (menuItem.key) {
      parentItems.push(menuItem);
    }
    return parentItems;
  }).flat(1);
}
var stdin_default = getMatchMenu;
