'use strict';
exports.__esModule = true;
var commander = require("commander");
var fs = require("fs");
var path = require("path");
var log = require("./log");
var series_1 = require("./series");
/* correspondances between resolution and value CR excpect */
var resol_table = {
    360: { quality: '60', format: '106' },
    480: { quality: '61', format: '106' },
    720: { quality: '62', format: '106' },
    1080: { quality: '80', format: '108' }
};
/**
 * Streams the batch of series to disk.
 */
function default_1(args, done) {
    var config = parse(args);
    var batchPath = path.join(config.output || process.cwd(), config.batch);
    // set resolution
    if (config.resolution) {
        try {
            config.video_format = resol_table[config.resolution].format;
            config.video_quality = resol_table[config.resolution].quality;
        }
        catch (e) {
            log.warn('Invalid resolution ' + config.resolution + 'p. Setting to 1080p');
            config.video_format = resol_table['1080'].format;
            config.video_quality = resol_table['1080'].quality;
        }
    }
    else {
        /* 1080 by default */
        config.video_format = resol_table['1080'].format;
        config.video_quality = resol_table['1080'].quality;
    }
    tasks(config, batchPath, function (err, tasksArr) {
        if (err) {
            return done(err);
        }
        var i = 0;
        (function next() {
            if (i >= tasksArr.length) {
                return done();
            }
            series_1["default"](tasksArr[i].config, tasksArr[i].address, function (errin) {
                if (errin) {
                    if (tasksArr[i].retry <= 0) {
                        console.error(err);
                        log.error('Cannot get episodes from "' + tasksArr[i].address + '", please rerun later');
                    }
                    else {
                        if (config.verbose) {
                            console.error(err);
                        }
                        log.warn('Retrying to fetch episodes ' + tasksArr[i].retry + ' / ' + config.retry);
                        tasksArr[i].retry -= 1;
                    }
                }
                else {
                    i += 1;
                }
                next();
            });
        })();
    });
}
exports["default"] = default_1;
/**
 * Splits the value into arguments.
 */
function split(value) {
    var inQuote = false;
    var i;
    var pieces = [];
    var previous = 0;
    for (i = 0; i < value.length; i += 1) {
        if (value.charAt(i) === '"') {
            inQuote = !inQuote;
        }
        if (!inQuote && value.charAt(i) === ' ') {
            pieces.push(value.substring(previous, i).match(/^"?(.+?)"?$/)[1]);
            previous = i + 1;
        }
    }
    var lastPiece = value.substring(previous, i).match(/^"?(.+?)"?$/);
    if (lastPiece) {
        pieces.push(lastPiece[1]);
    }
    return pieces;
}
/**
 * Parses the configuration or reads the batch-mode file for tasks.
 */
function tasks(config, batchPath, done) {
    if (config.args.length) {
        var configIn_1 = config;
        return done(null, config.args.map(function (addressIn) {
            return { address: addressIn, config: configIn_1, retry: config.retry };
        }));
    }
    fs.exists(batchPath, function (exists) {
        if (!exists) {
            return done(null, []);
        }
        fs.readFile(batchPath, 'utf8', function (err, data) {
            if (err) {
                return done(err);
            }
            var map = [];
            data.split(/\r?\n/).forEach(function (line) {
                if (/^(\/\/|#)/.test(line)) {
                    return;
                }
                var lineConfig = parse(process.argv.concat(split(line)));
                lineConfig.args.forEach(function (addressIn) {
                    if (!addressIn) {
                        return;
                    }
                    map.push({ address: addressIn, config: lineConfig, retry: config.retry });
                });
            });
            done(null, map);
        });
    });
}
/**
 * Parses the arguments and returns a configuration.
 */
function parse(args) {
    return new commander.Command().version(require('../package').version)
        // Authentication
        .option('-p, --pass <s>', 'The password.')
        .option('-u, --user <s>', 'The e-mail address or username.')
        // Disables
        .option('-c, --cache', 'Disables the cache.')
        .option('-m, --merge', 'Disables merging subtitles and videos.')
        // Filters
        .option('-e, --episode <i>', 'The episode filter.')
        .option('-v, --volume <i>', 'The volume filter.')
        // Settings
        .option('-f, --format <s>', 'The subtitle format. (Default: ass)')
        .option('-o, --output <s>', 'The output path.')
        .option('-s, --series <s>', 'The series override.')
        .option('-n, --filename <s>', 'The name override.')
        .option('-t, --tag <s>', 'The subgroup. (Default: CrunchyRoll)', 'CrunchyRoll')
        .option('-r, --resolution <s>', 'The video resolution. (Default: 1080 (360, 480, 720, 1080))', '1080')
        .option('-g, --rebuildcrp', 'Rebuild the crpersistant file.')
        .option('-b, --batch <s>', 'Batch file', 'CrunchyRoll.txt')
        .option('--verbose', 'Make tool verbose')
        .option('--retry <i>', 'Number or time to retry fetching an episode. Default: 5', 5)
        .parse(args);
}
//# sourceMappingURL=batch.js.map