"use strict";
var https = require('https');
var rules_1 = require('./rules');
function camelCaseToDash(str) {
    return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
}
function arrayDiff(source, target) {
    return source.filter(function (item) { return target.indexOf(item) === -1; });
}
function requestFromGithub(path, callback) {
    var options = {
        path: path,
        host: 'api.github.com',
        headers: {
            'User-Agent': 'tslint-eslint-rules'
        }
    };
    https.get(options, function (resp) {
        resp.setEncoding('utf8');
        var buffer = [];
        resp.on('data', function (chunk) {
            buffer.push(chunk);
        });
        resp.on('end', function () {
            var data = JSON.parse(buffer.join(''));
            callback(data);
        });
    }).on('error', function (e) {
        console.error(e);
    });
}
function compareToESLint() {
    return new Promise(function (fulfill, reject) {
        requestFromGithub('/repos/eslint/eslint/contents/lib/rules', function (data) {
            var rules = data
                .filter(function (obj) { return obj.name.endsWith('.js'); })
                .map(function (obj) { return obj.name.substring(0, obj.name.length - 3); });
            var esRules = Object.keys(rules_1.ruleESMap);
            var missing = arrayDiff(rules.map(function (x) { return rules_1.toCamelCase(x); }), esRules);
            var deprecated = arrayDiff(esRules, rules.map(function (x) { return rules_1.toCamelCase(x); }));
            var buffer = [];
            if (missing.length) {
                buffer.push('Missing ESLint rules (http://eslint.org/docs/rules):');
                missing.forEach(function (rule) {
                    var name = camelCaseToDash(rule);
                    buffer.push("- " + name);
                });
            }
            if (deprecated.length) {
                buffer.push('Deprecated ESLint rules:');
                deprecated.forEach(function (rule) {
                    var name = camelCaseToDash(rule);
                    buffer.push("- " + name);
                });
            }
            if (missing.length + deprecated.length === 0) {
                buffer.push('ESLint rules are in sync!');
            }
            console.log(buffer.join('\n'), '\n');
            fulfill();
        });
    });
}
exports.compareToESLint = compareToESLint;
function compareToTSLint() {
    return new Promise(function (fulfill, reject) {
        requestFromGithub('/repos/palantir/tslint/contents/src/rules', function (data) {
            var rules = data
                .filter(function (obj) { return obj.name.endsWith('.ts'); })
                .map(function (obj) { return obj.name.substring(0, obj.name.length - 7); });
            var notInUse = require('../../src/readme/unusedTSLintRules.json');
            notInUse.forEach(function (name) {
                var camel = rules_1.toCamelCase(name);
                var index = rules.indexOf(camel);
                if (index > -1) {
                    rules.splice(index, 1);
                }
            });
            var tsRules = Object.keys(rules_1.ruleTSMap);
            var missing = arrayDiff(rules, tsRules);
            var buffer = [];
            if (missing.length) {
                buffer.push('Missing TSLint rules (http://palantir.github.io/tslint/rules):');
                missing.forEach(function (rule) {
                    var name = camelCaseToDash(rule);
                    buffer.push("- " + name);
                });
            }
            else {
                buffer.push('TSLint rules are in sync!');
            }
            console.log(buffer.join('\n'), '\n');
            fulfill();
        });
    });
}
exports.compareToTSLint = compareToTSLint;
