"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CdnJsDatasource = void 0;
const tslib_1 = require("tslib");
const zod_1 = require("zod");
const logger_1 = require("../../../logger");
const external_host_error_1 = require("../../../types/errors/external-host-error");
const decorator_1 = require("../../../util/cache/package/decorator");
const result_1 = require("../../../util/result");
const datasource_1 = require("../datasource");
const schema_1 = require("../schema");
const schema_2 = require("./schema");
class CdnJsDatasource extends datasource_1.Datasource {
    static id = 'cdnjs';
    constructor() {
        super(CdnJsDatasource.id);
    }
    customRegistrySupport = false;
    defaultRegistryUrls = ['https://api.cdnjs.com/'];
    async getReleases(config) {
        const result = result_1.Result.parse(config, schema_1.ReleasesConfig)
            .transform(({ packageName, registryUrl }) => {
            const [library] = packageName.split('/');
            const url = `${registryUrl}libraries/${library}?fields=homepage,repository,versions`;
            return this.http.getJsonSafe(url, schema_2.CdnjsAPIVersionResponseSchema);
        })
            .transform(({ versions, homepage, repository }) => {
            const releases = versions;
            const res = { releases };
            if (homepage) {
                res.homepage = homepage;
            }
            if (repository) {
                res.sourceUrl = repository;
            }
            return res;
        });
        const { val, err } = await result.unwrap();
        if (err instanceof zod_1.ZodError) {
            logger_1.logger.debug({ err }, 'cdnjs: validation error');
            return null;
        }
        if (err) {
            this.handleGenericErrors(err);
        }
        return val;
    }
    async getDigest(config, newValue) {
        const { packageName } = config;
        const [library] = packageName.split('/');
        const assetName = packageName.replace(`${library}/`, '');
        const result = result_1.Result.parse(config, schema_1.DigestsConfig)
            .transform(({ registryUrl }) => {
            const url = `${registryUrl}libraries/${library}/${newValue}?fields=sri`;
            return this.http.getJsonSafe(url, schema_2.CdnjsAPISriResponseSchema);
        })
            .transform(({ sri }) => {
            return sri?.[assetName];
        });
        const { val = null, err } = await result.unwrap();
        if (err instanceof zod_1.ZodError) {
            logger_1.logger.debug({ err }, 'cdnjs: validation error');
            return null;
        }
        if (err) {
            this.handleGenericErrors(err);
        }
        return val;
    }
    handleHttpErrors(err) {
        if (err.response?.statusCode !== 404) {
            throw new external_host_error_1.ExternalHostError(err);
        }
    }
}
exports.CdnJsDatasource = CdnJsDatasource;
tslib_1.__decorate([
    (0, decorator_1.cache)({
        namespace: `datasource-${CdnJsDatasource.id}`,
        key: ({ packageName }) => packageName.split('/')[0],
    })
], CdnJsDatasource.prototype, "getReleases", null);
tslib_1.__decorate([
    (0, decorator_1.cache)({
        namespace: `datasource-${CdnJsDatasource.id}-digest`,
        key: ({ registryUrl, packageName }, newValue) => `${registryUrl}:${packageName}:${newValue}}`,
    })
], CdnJsDatasource.prototype, "getDigest", null);
//# sourceMappingURL=index.js.map