{"size": 1425, "ext": "js", "max_stars_repo_path": "lib/episodeIdIterator.js", "max_stars_repo_name": "SeekTheError/tbb-transmission", "max_stars_repo_stars_event_min_datetime": "2019-09-08T12:13:23.000Z", "max_stars_repo_stars_event_max_datetime": "2019-09-08T12:13:23.000Z", "max_issues_repo_path": "lib/episodeIdIterator.js", "max_issues_repo_name": "SeekTheError/tbb-transmission", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/episodeIdIterator.js", "max_forks_repo_name": "SeekTheError/tbb-transmission", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.1525423729, "max_line_length": 110, "alphanum_fraction": 0.6849122807}
'use strict';

var _ = require('lodash');

var internals = {
  prefix: function(n) {
    if ((n + '').length === 1) {
      return  '0' + n;
    }
    return n + '';
  }
};

var EpisodeIdIterator = function EpisodeIdIterator(torrentName, fromSeason, fromEpisode, download, finalize) {
  if (!torrentName) {
    throw new Error('missing parameter for new EpisodeIdIterator: torrentName');
  }

  if (!_.isFunction(finalize)) {
    throw new Error('EpisodeIteratorId: finalize must be a function');
  }

  this.listSuccess = [];
  this.download = download || false;
  this.previousFail = false;
  this.torrentName = torrentName;
  this.season = fromSeason || 1;
  this.episode = fromEpisode || 1;
  this.newSeason = false;
  this.$finalize = finalize;
};

EpisodeIdIterator.prototype.nextEpisode = function() {
  this.episode++;
  this.newSeason = false;
  return this;
};

EpisodeIdIterator.prototype.nextSeason = function() {
  this.season++;
  this.episode = 1;
  this.newSeason = true;
  return this;
};

EpisodeIdIterator.prototype.toString = function() {
  return this.torrentName + ' s' + internals.prefix(this.season) + 'e' + internals.prefix(this.episode);
};

EpisodeIdIterator.prototype.registerSuccess = function(torrent) {
  this.listSuccess.push( { name: this.toString(), torrent: torrent });
};

EpisodeIdIterator.prototype.finalize = function() {
  this.$finalize(this);
};

module.exports = EpisodeIdIterator;
