"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SyncAction = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
const chalk = require("chalk");
const run_fetcher_1 = require("../lib/run-fetcher");
const create_dictionary_files_1 = require("../lib/utils/file-system/create-dictionary-files");
const abstract_action_1 = require("./abstract.action");
const base_exception_1 = require("../exceptions/base.exception");
const messages_1 = require("../lib/ui/messages");
const missing_param_exception_1 = require("../exceptions/missing-param.exception");
class SyncAction extends abstract_action_1.AbstractAction {
    handle({ options }) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const { fetcher, resolve, fetcherParams, envPath } = yield this.resolveOptions(options);
                this.validateParams({ fetcher, resolve, fetcherParams, envPath });
                const output = yield (0, run_fetcher_1.runFetcher)(fetcher, fetcherParams, envPath);
                yield (0, create_dictionary_files_1.createDictionaryFiles)(resolve, output);
                console.log(chalk.green(messages_1.MESSAGES.SYNC_SUCCESSFUL));
            }
            catch (e) {
                if (e instanceof base_exception_1.BaseException) {
                    console.error(chalk.red(e.getMessage()));
                }
                else {
                    throw e;
                }
            }
        });
    }
    resolveOptions(options) {
        var _a, _b, _c, _d;
        return __awaiter(this, void 0, void 0, function* () {
            const rc = yield this.getRCFile();
            return {
                fetcher: (_a = options === null || options === void 0 ? void 0 : options.fetcher) !== null && _a !== void 0 ? _a : rc === null || rc === void 0 ? void 0 : rc.fetcher,
                resolve: (_b = options === null || options === void 0 ? void 0 : options.resolve) !== null && _b !== void 0 ? _b : rc === null || rc === void 0 ? void 0 : rc.resolvePath,
                envPath: (_c = options === null || options === void 0 ? void 0 : options.env) !== null && _c !== void 0 ? _c : rc === null || rc === void 0 ? void 0 : rc.envPath,
                fetcherParams: (_d = rc === null || rc === void 0 ? void 0 : rc.fetcherParams) !== null && _d !== void 0 ? _d : {},
            };
        });
    }
    validateParams(options) {
        const requiredParams = ['fetcher', 'resolve'];
        const parameterErrors = requiredParams.reduce((paramErrors, currentParam) => {
            if (!options[currentParam]) {
                if (paramErrors) {
                    return (paramErrors += `, ${currentParam}`);
                }
                else {
                    return currentParam;
                }
            }
        }, '');
        if (parameterErrors) {
            throw new missing_param_exception_1.MissingParamException({ parameters: parameterErrors });
        }
    }
    getRCFile() {
        return __awaiter(this, void 0, void 0, function* () {
            const rcPath = (0, path_1.join)(process.cwd(), '.metristsrc');
            if ((0, fs_1.existsSync)(rcPath)) {
                return JSON.parse(yield fs_1.promises.readFile(rcPath, 'utf8'));
            }
            return;
        });
    }
}
exports.SyncAction = SyncAction;
