import {
  getFeed,
  getIndex,
  getSources,
  getStats,
  searchSkills
} from "./chunk-HBXXTYX3.js";
import {
  jsonOk,
  routeWrap
} from "./chunk-ZMMFMEXX.js";

// src/handlers/skills.ts
async function handleGetSkillsCatalog(request) {
  return routeWrap(request, async ({ request: req }) => {
    const url = new URL(req.url);
    const leaderboard = url.searchParams.get("leaderboard") || "trending";
    const feed = await getFeed();
    const skills = leaderboard === "hot" ? feed.topHot : leaderboard === "allTime" ? feed.topAllTime : feed.topTrending;
    const limit = Number(url.searchParams.get("limit") || "50");
    return jsonOk({ skills: skills.slice(0, limit), total: skills.length });
  });
}
async function handleSearchSkills(request) {
  return routeWrap(request, async ({ request: req }) => {
    const url = new URL(req.url);
    const opts = {
      q: url.searchParams.get("q") || void 0,
      owner: url.searchParams.get("owner") || void 0,
      source: url.searchParams.get("source") || url.searchParams.get("repo") || void 0,
      sort: url.searchParams.get("sort") || void 0,
      limit: Number(url.searchParams.get("limit") || "50"),
      offset: Number(url.searchParams.get("offset") || "0")
    };
    const result = await searchSkills(opts);
    return jsonOk(result);
  });
}
async function handleGetSkillsStats(request) {
  return routeWrap(request, async () => {
    const stats = await getStats();
    return jsonOk(stats);
  });
}
async function handleGetSkillsSources(request) {
  return routeWrap(request, async ({ request: req }) => {
    const url = new URL(req.url);
    const limit = Number(url.searchParams.get("limit") || "100");
    const offset = Number(url.searchParams.get("offset") || "0");
    const sources = await getSources();
    return jsonOk({
      data: sources.slice(offset, offset + limit),
      total: sources.length
    });
  });
}
async function handleGetSkillsIndex(request) {
  return routeWrap(request, async () => {
    const index = await getIndex();
    return jsonOk(index);
  });
}
async function handleGetSkillsFeed(request) {
  return routeWrap(request, async () => {
    const feed = await getFeed();
    return jsonOk(feed);
  });
}

export {
  handleGetSkillsCatalog,
  handleSearchSkills,
  handleGetSkillsStats,
  handleGetSkillsSources,
  handleGetSkillsIndex,
  handleGetSkillsFeed
};
