'use strict';

var server = require('next/server');

// src/qqbot/server/client.ts
var NapCatClient = class {
  constructor(config) {
    this.baseUrl = config.baseUrl.replace(/\/$/, "");
    this.accessToken = config.accessToken;
    this.timeoutMs = config.timeoutMs ?? 12e3;
  }
  async callApi(action, params) {
    const controller = new AbortController();
    const timeout = setTimeout(() => controller.abort(), this.timeoutMs);
    try {
      const response = await fetch(`${this.baseUrl}/${action}`, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          ...this.accessToken ? { Authorization: `Bearer ${this.accessToken}` } : {}
        },
        body: JSON.stringify(params ?? {}),
        signal: controller.signal
      });
      if (!response.ok) {
        return {
          status: "failed",
          retcode: response.status,
          message: `NapCat HTTP ${response.status}`
        };
      }
      const payload = await response.json();
      return payload;
    } catch (error) {
      return {
        status: "failed",
        retcode: -1,
        message: error instanceof Error ? error.message : "Unknown NapCat request error"
      };
    } finally {
      clearTimeout(timeout);
    }
  }
  async sendGroupMessage(payload) {
    return this.callApi("send_group_msg", payload);
  }
  async sendPrivateMessage(payload) {
    return this.callApi("send_private_msg", payload);
  }
  async getGroupList() {
    return this.callApi("get_group_list");
  }
  async getFriendList() {
    return this.callApi("get_friend_list");
  }
};

// src/qqbot/server/webApi.ts
function json(status, body) {
  return {
    status,
    headers: { "Content-Type": "application/json" },
    body
  };
}
function createNapCatWebApi(client, options = {}) {
  return async function handle(request) {
    const method = request.method.toUpperCase();
    if (method === "GET" && request.path === "/health") {
      return json(200, { ok: true, data: { service: "qqbot-napcat-bridge" } });
    }
    if (method === "POST" && request.path === "/send/group") {
      const result = await client.sendGroupMessage(request.body ?? {});
      return json(result.status === "ok" ? 200 : 502, {
        ok: result.status === "ok",
        data: result.data,
        error: result.message
      });
    }
    if (method === "POST" && request.path === "/send/private") {
      const result = await client.sendPrivateMessage(request.body ?? {});
      return json(result.status === "ok" ? 200 : 502, {
        ok: result.status === "ok",
        data: result.data,
        error: result.message
      });
    }
    if (method === "POST" && request.path === "/groups") {
      const result = await client.getGroupList();
      return json(result.status === "ok" ? 200 : 502, {
        ok: result.status === "ok",
        data: result.data,
        error: result.message
      });
    }
    if (method === "POST" && request.path === "/friends") {
      const result = await client.getFriendList();
      return json(result.status === "ok" ? 200 : 502, {
        ok: result.status === "ok",
        data: result.data,
        error: result.message
      });
    }
    if (method === "POST" && request.path.startsWith("/action/")) {
      const action = request.path.replace("/action/", "");
      const result = await client.callApi(action, request.body ?? {});
      return json(result.status === "ok" ? 200 : 502, {
        ok: result.status === "ok",
        data: result.data,
        error: result.message
      });
    }
    if (method === "POST" && request.path === "/webhook/event") {
      const event = request.body ?? {};
      await options.onWebhookEvent?.(event);
      return json(200, { ok: true });
    }
    return json(404, { ok: false, error: `Route not found: ${method} ${request.path}` });
  };
}
function createNextNapCatRouteHandler(config) {
  const basePath = config.basePath ?? "/api/qqbot";
  const handle = createNapCatWebApi(config.client, {
    onWebhookEvent: config.onWebhookEvent
  });
  return async function handler(request) {
    const url = new URL(request.url);
    const path = url.pathname.startsWith(basePath) ? url.pathname.slice(basePath.length) || "/" : url.pathname;
    const body = request.method.toUpperCase() === "GET" ? void 0 : await request.json().catch(() => void 0);
    const payload = {
      method: request.method,
      path,
      body
    };
    const result = await handle(payload);
    return server.NextResponse.json(result.body, { status: result.status, headers: result.headers });
  };
}

exports.NapCatClient = NapCatClient;
exports.createNapCatWebApi = createNapCatWebApi;
exports.createNextNapCatRouteHandler = createNextNapCatRouteHandler;
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map