import { defineEventHandler, useQuery } from 'h3';
import { u as useApiQuery, q as queryContent } from '../storage.mjs';
import { g as generateTitle } from '../index.mjs';
import { u as useRuntimeConfig } from '../nitro/node-server.mjs';
import 'unstorage';
import 'ohash';
import 'destr';
import 'pathe';
import 'defu';
import 'remark-emoji';
import 'rehype-slug';
import 'remark-squeeze-paragraphs';
import 'rehype-external-links';
import 'remark-gfm';
import 'rehype-sort-attribute-values';
import 'rehype-sort-attributes';
import 'rehype-raw';
import 'gray-matter';
import 'flat';
import 'unified';
import 'remark-parse';
import 'remark-rehype';
import 'scule';
import 'unist-util-visit';
import 'stringify-entities';
import 'parse-entities';
import 'micromark-util-character';
import 'micromark-factory-space';
import 'micromark-factory-whitespace';
import 'micromark-core-commonmark';
import 'mdast-util-to-hast';
import 'detab';
import 'unist-builder';
import 'mdurl';
import 'unist-util-position';
import 'html-tags';
import 'slugify';
import 'ufo';
import 'unenv/runtime/polyfill/fetch.node';
import 'http';
import 'https';
import 'ohmyfetch';
import 'radix3';
import 'unenv/runtime/fetch/index';
import 'hookable';
import 'unstorage/drivers/overlay';
import 'unstorage/drivers/memory';

function createNav(contents, configs) {
  const { navigation } = useRuntimeConfig().content;
  const pickNavigationFields = pick(["title", ...navigation.fields]);
  const nav = contents.sort((a, b) => a.slug.localeCompare(b.slug)).reduce((nav2, content) => {
    const parts = content.slug.substring(1).split("/");
    const idParts = content.id.split(":").slice(1);
    const isIndex = !!idParts[idParts.length - 1].match(/([1-9][0-9]*\.)?index.md/g);
    const getNavItem = (content2) => {
      return {
        id: content2.id,
        title: content2.title,
        slug: content2.slug,
        path: content2.path,
        children: [],
        ...pickNavigationFields(content2),
        ...content2.draft ? { draft: true } : {}
      };
    };
    const navItem = getNavItem(content);
    if (isIndex) {
      const indexItem = getNavItem(content);
      navItem.children.push(indexItem);
      const conf = configs[indexItem.path.split("/").slice(0, -1).join("/")];
      Object.assign(indexItem, pickNavigationFields(conf));
      delete navItem.id;
    }
    if (parts.length === 1) {
      nav2.push(navItem);
      return nav2;
    }
    const siblings = parts.slice(0, -1).reduce((nodes, part, i) => {
      const currentPathSlug = "/" + parts.slice(0, i + 1).join("/");
      let parent = nodes.find((n) => n.slug === currentPathSlug);
      if (!parent) {
        const conf = configs[idParts.slice(0, i + 1).join("/")];
        parent = {
          slug: currentPathSlug,
          title: generateTitle(part),
          path: idParts.slice(0, i + 1).join("/"),
          children: [],
          ...pickNavigationFields(conf)
        };
        nodes.push(parent);
      }
      return parent.children;
    }, nav2);
    siblings.push(navItem);
    return nav2;
  }, []);
  return sortAndClear(nav);
}
function sortAndClear(nav) {
  const sorted = nav.sort((a, b) => a.path.localeCompare(b.path));
  for (const item of sorted) {
    if (item.children.length) {
      sortAndClear(item.children);
    } else {
      delete item.children;
    }
    delete item.path;
  }
  return nav;
}
function pick(keys) {
  return (obj = {}) => {
    if (keys && keys.length) {
      return keys.filter((key) => typeof obj[key] !== "undefined").reduce((newObj, key) => Object.assign(newObj, { [key]: obj[key] }), {});
    }
    return obj;
  };
}

const navigation = defineEventHandler(async (event) => {
  const { query: qid } = event.context.params;
  const query = useApiQuery(qid, useQuery(event)?.params || void 0);
  const contents = await queryContent(query).where({
    partial: false
  }).find();
  const dirConfigs = await queryContent().where({ path: /\/_dir$/i, partial: true }).find();
  const configs = dirConfigs.reduce((configs2, conf) => {
    if (conf.title.toLowerCase() === "dir") {
      conf.title = void 0;
    }
    const key = conf.path.split("/").slice(0, -1).join("/");
    configs2[key] = {
      ...conf,
      ...conf.body
    };
    return configs2;
  }, {});
  return createNav(contents, configs);
});

export { navigation as default };
//# sourceMappingURL=navigation.mjs.map
