"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 formats_1 = require("./formats");
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;
        const fetcher = formats_1.fetchers[format];
        const isLocalRegistry = defaultRegistryUrlTemplate.startsWith('file://');
        let data;
        try {
            if (isLocalRegistry) {
                data = await fetcher.readFile(defaultRegistryUrlTemplate);
            }
            else {
                data = await fetcher.fetch(this.http, defaultRegistryUrlTemplate);
            }
        }
        catch (e) {
            this.handleHttpErrors(e);
            return null;
        }
        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