"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
/**
 * penguins-eggs
 * command: stat.ts
 * author: Piero Proietti
 * email: piero.proietti@gmail.com
 * license: MIT
 */
const core_1 = require("@oclif/core");
const utils_1 = tslib_1.__importDefault(require("../../classes/utils"));
const axios_1 = tslib_1.__importDefault(require("axios"));
const node_https_1 = tslib_1.__importDefault(require("node:https"));
const agent = new node_https_1.default.Agent({
    rejectUnauthorized: false,
});
/**
 *
 */
class ToolsStat extends core_1.Command {
    /**
     *
     */
    async run() {
        const { args, flags } = await this.parse(ToolsStat);
        utils_1.default.titles(this.id + ' ' + this.argv);
        const yesterday = new Date();
        yesterday.setDate(yesterday.getDate() - 1);
        // current date
        // adjust 0 before single digit date
        const day = ('0' + yesterday.getDate()).slice(-2);
        // current month
        const month = ('0' + (yesterday.getMonth() + 1)).slice(-2);
        // current year
        const year = yesterday.getFullYear();
        const end = year + '-' + month + '-' + day;
        let start = year + '-' + month + '-' + day;
        if (flags.month) {
            start = year + '-' + month + '-01';
        }
        if (flags.year) {
            start = year + '-01-01';
        }
        this.log('start: ' + start + ', end: ' + end + '\n');
        await this.show(start, end, 'DEBS');
        console.log();
        await this.show(start, end, 'ISOS');
    }
    /**
     *
     * @param start
     * @param end
     * @param type
     */
    async show(start, end, type = 'DEBS') {
        let url = `https://sourceforge.net/projects/penguins-eggs/files/${type}/stats/json`;
        const request = '?start_date=' + start + '&end_date=' + end;
        url += request;
        const res = await axios_1.default.get(url, { httpsAgent: agent });
        console.log(type);
        for (const country of res.data.countries) {
            console.log('- ' + country[0] + ': ' + country[1]);
        }
    }
}
ToolsStat.flags = {
    help: core_1.Flags.help({ char: 'h' }),
    month: core_1.Flags.boolean({ char: 'm', description: 'current month' }),
    year: core_1.Flags.boolean({ char: 'y', description: 'current year' }),
};
ToolsStat.description = 'get statistics from sourceforge';
ToolsStat.examples = [
    'eggs tools stat',
    'eggs tools stat --month',
    'eggs tools stat --year',
];
exports.default = ToolsStat;
