import { Stream, ProviderContext } from "../types";

export const getStream = async function ({
  link: id,
  // type,
  signal,
  providerContext,
}: {
  link: string;
  type: string;
  signal: AbortSignal;
  providerContext: ProviderContext;
}): Promise<Stream[]> {
  try {
    const { axios, cheerio } = providerContext;
    const stream: Stream[] = [];
    const [, epId] = id.split("&");
    const url = `https://feb.8man.workers.dev/?fid=${epId}`;
    const res = await axios.get(url, { signal });
    const data = res.data;
    const $ = cheerio.load(data.html);
    $(".file_quality").each((i, el) => {
      const server =
        $(el).find("p.name").text() +
        " - " +
        $(el).find("p.size").text() +
        " - " +
        $(el).find("p.speed").text();
      const link = $(el).attr("data-url");
      if (link) {
        stream.push({
          server: server,
          type: "mkv",
          link: link,
        });
      }
    });
    console.log(stream);
    return stream;
  } catch (err) {
    return [];
  }
};
