import Meting from '@meting/core';

// src/music/server/meting.ts

// src/music/constants.ts
var DEFAULT_MUSIC_SOURCE = "kugou";

// src/music/server/meting.ts
var MetingService = class {
  constructor() {
    this.instances = /* @__PURE__ */ new Map();
  }
  getInstance(source) {
    if (!this.instances.has(source)) {
      this.instances.set(source, new Meting(source));
    }
    return this.instances.get(source);
  }
  /**
   * 搜索歌曲
   */
  async search(options) {
    const { keyword, source = DEFAULT_MUSIC_SOURCE, limit = 20, offset = 0 } = options;
    const meting = this.getInstance(source);
    const response = await meting.search(keyword, {
      limit,
      page: offset + 1
    });
    try {
      return typeof response === "string" ? JSON.parse(response) : response;
    } catch (e) {
      return response;
    }
  }
  /**
   * 获取歌曲详情（包含播放链接）
   */
  async getSongUrl(id, source = DEFAULT_MUSIC_SOURCE) {
    const meting = this.getInstance(source);
    const response = await meting.song(id);
    try {
      return typeof response === "string" ? JSON.parse(response) : response;
    } catch (e) {
      return response;
    }
  }
  /**
   * 获取歌词
   */
  async getLyric(id, source = DEFAULT_MUSIC_SOURCE) {
    const meting = this.getInstance(source);
    const response = await meting.lyric(id);
    try {
      return typeof response === "string" ? JSON.parse(response) : response;
    } catch (e) {
      return response;
    }
  }
};
var musicService = new MetingService();

// src/music/server/miku.ts
var MikuMusicService = class {
  constructor(baseService = musicService) {
    this.mikuKeywords = ["\u521D\u97F3\u672A\u6765", "Miku", "Vocaloid", "MIKU"];
    this.baseService = baseService;
  }
  /**
   * 搜索歌曲，自动增加 Miku 相关关键词
   */
  async search(options) {
    const { keyword, source = DEFAULT_MUSIC_SOURCE, limit = 20, offset = 0 } = options;
    const trimmedKeyword = keyword.trim();
    const hasMikuKeyword = this.mikuKeywords.some(
      (k) => trimmedKeyword.toLowerCase().includes(k.toLowerCase())
    );
    const mikuKeyword = hasMikuKeyword ? trimmedKeyword : `${trimmedKeyword} \u521D\u97F3\u672A\u6765`.trim();
    return this.baseService.search({
      ...options,
      keyword: mikuKeyword || "\u521D\u97F3\u672A\u6765"
      // 如果输入为空，默认搜初音未来
    });
  }
  /**
   * 获取歌曲详情（包含播放链接）- 直接透传
   */
  async getSongUrl(id, source = DEFAULT_MUSIC_SOURCE) {
    return this.baseService.getSongUrl(id, source);
  }
  /**
   * 获取歌词 - 直接透传
   */
  async getLyric(id, source = DEFAULT_MUSIC_SOURCE) {
    return this.baseService.getLyric(id, source);
  }
};
var mikuMusicService = new MikuMusicService();

// src/music/server/handlers.ts
var createSearchHandler = () => {
  return async (req) => {
    try {
      const { searchParams } = new URL(req.url);
      const keyword = searchParams.get("keyword");
      const source = searchParams.get("source") || DEFAULT_MUSIC_SOURCE;
      const limit = parseInt(searchParams.get("limit") || "20");
      const offset = parseInt(searchParams.get("offset") || "0");
      const miku = searchParams.get("miku") === "true";
      if (!keyword && !miku) {
        return Response.json({ code: 400, message: "Keyword is required" }, { status: 400 });
      }
      const service = miku ? mikuMusicService : musicService;
      const result = await service.search({
        keyword: keyword || "",
        source,
        limit,
        offset,
        miku
      });
      return Response.json({
        code: 200,
        data: result
      });
    } catch (error) {
      console.error("[Music] Search error:", error);
      return Response.json({
        code: 500,
        message: error.message || "Internal Server Error"
      }, { status: 500 });
    }
  };
};
var createSongUrlHandler = () => {
  return async (req) => {
    try {
      const { searchParams } = new URL(req.url);
      const id = searchParams.get("id");
      const source = searchParams.get("source") || DEFAULT_MUSIC_SOURCE;
      if (!id) {
        return Response.json({ code: 400, message: "ID is required" }, { status: 400 });
      }
      const url = await musicService.getSongUrl(id, source);
      return Response.json({
        code: 200,
        data: { url }
      });
    } catch (error) {
      console.error("[Music] Get URL error:", error);
      return Response.json({
        code: 500,
        message: error.message || "Internal Server Error"
      }, { status: 500 });
    }
  };
};
var createLyricHandler = () => {
  return async (req) => {
    try {
      const { searchParams } = new URL(req.url);
      const id = searchParams.get("id");
      const source = searchParams.get("source") || DEFAULT_MUSIC_SOURCE;
      if (!id) {
        return Response.json({ code: 400, message: "ID is required" }, { status: 400 });
      }
      const lyric = await musicService.getLyric(id, source);
      return Response.json({
        code: 200,
        data: { lyric }
      });
    } catch (error) {
      console.error("[Music] Get lyric error:", error);
      return Response.json({
        code: 500,
        message: error.message || "Internal Server Error"
      }, { status: 500 });
    }
  };
};

export { DEFAULT_MUSIC_SOURCE, MetingService, MikuMusicService, createLyricHandler, createSearchHandler, createSongUrlHandler, mikuMusicService, musicService };
//# sourceMappingURL=chunk-ZCLAF3XN.mjs.map
//# sourceMappingURL=chunk-ZCLAF3XN.mjs.map