"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomDatasource = void 0;
const tslib_1 = require("tslib");
const is_1 = tslib_1.__importDefault(require("@sindresorhus/is"));
const jsonata_1 = tslib_1.__importDefault(require("jsonata"));
const logger_1 = require("../../../logger");
const datasource_1 = require("../datasource");
const plain_1 = require("./formats/plain");
const yaml_1 = require("./formats/yaml");
const schema_1 = require("./schema");
const utils_1 = require("./utils");
class CustomDatasource extends datasource_1.Datasource {
    static id = 'custom';
    customRegistrySupport = true;
    constructor() {
        super(CustomDatasource.id);
    }
    async getReleases(getReleasesConfig) {
        const config = (0, utils_1.getCustomConfig)(getReleasesConfig);
        if (is_1.default.nullOrUndefined(config)) {
            return null;
        }
        const { defaultRegistryUrlTemplate, transformTemplates, format } = config;
        let response;
        try {
            switch (format) {
                case 'plain':
                    response = await (0, plain_1.fetch)(this.http, defaultRegistryUrlTemplate);
                    break;
                case 'yaml':
                    response = await (0, yaml_1.fetch)(this.http, defaultRegistryUrlTemplate);
                    break;
                case 'json':
                    response = (await this.http.getJson(defaultRegistryUrlTemplate)).body;
            }
        }
        catch (e) {
            this.handleHttpErrors(e);
            return null;
        }
        let data = response;
        for (const transformTemplate of transformTemplates) {
            const expression = (0, jsonata_1.default)(transformTemplate);
            data = await expression.evaluate(data);
        }
        try {
            const parsed = schema_1.ReleaseResultZodSchema.parse(data);
            return structuredClone(parsed);
        }
        catch (err) {
            logger_1.logger.debug({ err }, `Response has failed validation`);
            logger_1.logger.trace({ data }, 'Response that has failed validation');
            return null;
        }
    }
}
exports.CustomDatasource = CustomDatasource;
//# sourceMappingURL=index.js.map