import { DEFAULT_MUSIC_SOURCE } from './chunk-ZCLAF3XN.mjs';
import { useState, useMemo, useCallback } from 'react';
import useSWR from 'swr';

// src/music/adapters/kugou.ts
var kugouAdapter = {
  parseSearchResult(data) {
    const root = typeof data === "string" ? JSON.parse(data) : data;
    const info = root.data?.data?.info || root.data?.info || root.info || [];
    const total = root.data?.data?.total || root.data?.total || info.length;
    return {
      tracks: info.map((item) => {
        let pic = item.pic || "";
        if (item.trans_param?.union_cover) {
          pic = item.trans_param.union_cover.replace("{size}", "400");
        }
        return {
          id: item.hash || item.id,
          name: item.songname || item.filename || "Unknown",
          artist: item.singername || "Unknown Artist",
          album: item.album_name || item.album || "",
          pic,
          url: item.url,
          lrc: item.lrc,
          source: "kugou",
          isVip: item.privilege >= 8,
          playable: item.status !== 0
        };
      }),
      total
    };
  },
  parseGetSongUrl(data) {
    return data.url?.url || data.url?.backup_url?.[0] || null;
  },
  parseGetLyric(data) {
    const root = typeof data === "string" ? JSON.parse(data) : data;
    return root.lyric || root.lrc || root.data?.lyric || root.data || "";
  }
};

// src/music/adapters/netease.ts
var neteaseAdapter = {
  parseSearchResult(data) {
    const root = typeof data === "string" ? JSON.parse(data) : data;
    const songs = root.result?.songs || root.songs || (Array.isArray(root) ? root : []);
    return {
      tracks: songs.map((item) => ({
        id: item.id,
        name: item.name,
        artist: Array.isArray(item.artist) ? item.artist.join(", ") : item.artist,
        album: item.album?.name || item.album,
        pic: item.pic || item.album?.picUrl,
        url: item.url,
        lrc: item.lrc,
        source: "netease",
        isVip: item.fee === 1 || item.fee === 4,
        playable: item.noCopyrightRcmd === null
      })),
      total: root.result?.songCount || songs.length
    };
  },
  parseGetSongUrl(data) {
    const root = typeof data === "string" ? JSON.parse(data) : data;
    const item = root.data?.[0] || root[0] || root;
    return item.url || null;
  },
  parseGetLyric(data) {
    const root = typeof data === "string" ? JSON.parse(data) : data;
    return root.lyric || root.lrc || root.data?.lyric || "";
  }
};

// src/music/adapters/tencent.ts
var tencentAdapter = {
  parseSearchResult(data) {
    const root = typeof data === "string" ? JSON.parse(data) : data;
    const songData = root.data?.data?.song || root.data?.song || root.data || root;
    const list = songData.list || root.songs || [];
    const total = songData.totalnum || root.total || list.length;
    return {
      tracks: list.map((item) => {
        const artist = Array.isArray(item.singer) ? item.singer.map((s) => s.name).join(", ") : item.singer?.[0]?.name || item.artist || "Unknown";
        let pic = item.pic;
        if (!pic && item.album?.mid) {
          pic = `https://y.gtimg.cn/music/photo_new/T002R300x300M000${item.album.mid}.jpg`;
        }
        return {
          id: item.mid || item.id || item.songid,
          name: item.name || item.title || item.songname,
          artist,
          album: item.album?.name || item.albumname || item.album,
          pic: pic || "",
          url: item.url,
          lrc: item.lrc,
          source: "tencent",
          isVip: item.pay?.pay_play === 1,
          playable: item.action?.switch !== 0
        };
      }),
      total
    };
  },
  parseGetSongUrl(data) {
    const root = data;
    const urlData = root.url.url;
    let finalUrl = Object.values(urlData)[0];
    console.log("finalUrl2", finalUrl);
    return finalUrl.startsWith("http") ? finalUrl : `http://${finalUrl}`;
  },
  parseGetLyric(data) {
    const root = typeof data === "string" ? JSON.parse(data) : data;
    return root.lyric || root.lrc || root.data?.lyric || "";
  }
};

// src/music/adapters/xiami.ts
var xiamiAdapter = {
  parseSearchResult(data) {
    const root = typeof data === "string" ? JSON.parse(data) : data;
    const result = root.data?.result || root.result || root;
    const songs = result.songs || (Array.isArray(root) ? root : []);
    return {
      tracks: songs.map((item) => ({
        id: item.id?.toString() || "",
        name: item.name || "Unknown",
        artist: Array.isArray(item.ar) ? item.ar.map((a) => a.name).join(", ") : item.artist || "Unknown Artist",
        album: item.al?.name || item.album || "",
        pic: item.al?.picUrl || item.pic || "",
        url: item.url,
        lrc: item.lrc,
        source: "xiami",
        // 这里的逻辑参考提供的数据结构
        isVip: item.fee === 1 || item.fee === 8,
        playable: item.copyright !== 0
      })),
      total: result.songCount || songs.length
    };
  },
  parseGetSongUrl(data) {
    const root = typeof data === "string" ? JSON.parse(data) : data;
    const item = root.data?.[0] || root.data || root[0] || root;
    return item.url || null;
  },
  parseGetLyric(data) {
    const root = typeof data === "string" ? JSON.parse(data) : data;
    return root.lyric || root.lrc || root.data?.lyric || root.data || "";
  }
};

// src/music/hooks/useMusic.ts
var fetcher = (url) => fetch(url).then((res) => res.json());
var ADAPTERS = {
  kugou: kugouAdapter,
  netease: neteaseAdapter,
  tencent: tencentAdapter,
  xiami: xiamiAdapter
};
function useMusic() {
  const [searchOptions, setSearchOptions] = useState(null);
  const { data: rawData, error: searchError, isLoading: isSearching } = useSWR(
    searchOptions ? `/api/music/search?keyword=${encodeURIComponent(searchOptions.keyword)}&source=${searchOptions.source || DEFAULT_MUSIC_SOURCE}&limit=${searchOptions.limit || 20}&offset=${searchOptions.offset || 0}${searchOptions.miku ? "&miku=true" : ""}` : null,
    fetcher
  );
  const searchResult = useMemo(() => {
    if (!rawData?.data || !searchOptions) return void 0;
    const adapter = ADAPTERS[searchOptions.source || DEFAULT_MUSIC_SOURCE];
    if (adapter) {
      return adapter.parseSearchResult(rawData.data);
    }
    return void 0;
  }, [rawData, searchOptions]);
  const search = useCallback((options) => {
    setSearchOptions(options);
  }, []);
  const getSongUrl = useCallback(async (id, source = DEFAULT_MUSIC_SOURCE) => {
    try {
      const res = await fetch(`/api/music/url?id=${id}&source=${source}`);
      const json = await res.json();
      const adapter = ADAPTERS[source];
      console.log("json2", json.data, source, adapter);
      if (adapter && json.data) {
        console.log("getSongUrl2", json.data);
        return adapter.parseGetSongUrl(json.data) || void 0;
      }
      return json.data?.url;
    } catch (err) {
      console.error("[Music] Failed to get song URL:", err);
      return void 0;
    }
  }, []);
  const getLyric = useCallback(async (id, source = DEFAULT_MUSIC_SOURCE) => {
    try {
      const res = await fetch(`/api/music/lyric?id=${id}&source=${source}`);
      const json = await res.json();
      const adapter = ADAPTERS[source];
      if (adapter && json.data) {
        return adapter.parseGetLyric(json.data);
      }
      return json.data?.lyric;
    } catch (err) {
      console.error("[Music] Failed to get lyric:", err);
      return void 0;
    }
  }, []);
  return {
    search,
    searchResult,
    isSearching,
    searchError,
    getSongUrl,
    getLyric
  };
}

export { kugouAdapter, neteaseAdapter, tencentAdapter, useMusic, xiamiAdapter };
//# sourceMappingURL=chunk-T6TE7GTY.mjs.map
//# sourceMappingURL=chunk-T6TE7GTY.mjs.map