"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.runFetcher = void 0;
const execute_fetcher_1 = require("../utils/execute-fetcher");
const extract_json_1 = require("../utils/extract-json");
const fetchers_1 = require("../../fetchers");
const resolve_fetcher_params_env_1 = require("./resolve-fetcher-params-env");
const parse_fetcher_exception_1 = require("../../exceptions/parse-fetcher.exception");
const internal_fetcher_exception_1 = require("../../exceptions/internal-fetcher.exception");
const runFetcher = (fetcher, fetcherParams, envPath) => __awaiter(void 0, void 0, void 0, function* () {
    if (fetchers_1.builtInFetchers[fetcher]) {
        const fetcherParamsWithResolvedEnv = yield (0, resolve_fetcher_params_env_1.resolveFetcherParamsWithEnv)(fetcherParams, envPath);
        try {
            return fetchers_1.builtInFetchers[fetcher](fetcherParamsWithResolvedEnv);
        }
        catch (e) {
            throw new internal_fetcher_exception_1.InternalFetcherException(e.message);
        }
    }
    else {
        const fetcherCommandRawOutput = yield (0, execute_fetcher_1.executeFetcher)(fetcher);
        const extracted = (0, extract_json_1.extractJson)(fetcherCommandRawOutput);
        if (extracted) {
            return extracted;
        }
    }
    throw new parse_fetcher_exception_1.ParseFetcherException();
});
exports.runFetcher = runFetcher;
