import { IMovieSearchInfo } from "@autoplex-api/torrent-search";
import Provider, { MediaType } from "../Provider";
import Torrent from "../../Torrent";
import { search, Sort } from "./search";

/**
 * Throttle the torrent search
 */
const THROTTLE_SEARCH = 3000;

export default class TorrentGalaxy extends Provider
{
	/**
	 * Indicate that this provider provides movies
	 */
	public readonly PROVIDES = MediaType.Movies;

	/**
	 * Search for a movie
	 */
	public async searchMovie(movie: IMovieSearchInfo) {
		if (movie.imdbId === null) {
			return [];
		}
		await this.lock();
		let torrents = await search(movie.imdbId, undefined, Sort.Seeders);
		this.unlock(THROTTLE_SEARCH);
		return torrents.torrents.map(torrent => new Torrent(
			movie,
			torrent.name,
			torrent.size,
			torrent.seeders,
			torrent.magnet
		));
	}
}
