"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OrbDatasource = void 0;
const tslib_1 = require("tslib");
const logger_1 = require("../../../logger");
const decorator_1 = require("../../../util/cache/package/decorator");
const datasource_1 = require("../datasource");
const query = `
query($packageName: String!) {
  orb(name: $packageName) {
    name,
    homeUrl,
    isPrivate,
    versions {
      version,
      createdAt
    }
  }
}
`;
class OrbDatasource extends datasource_1.Datasource {
    constructor() {
        super(OrbDatasource.id);
        this.customRegistrySupport = false;
        this.defaultRegistryUrls = ['https://circleci.com/'];
    }
    async getReleases({ packageName, registryUrl, }) {
        // istanbul ignore if
        if (!registryUrl) {
            return null;
        }
        const url = `${registryUrl}graphql-unstable`;
        const body = {
            query,
            variables: { packageName },
        };
        const res = (await this.http.postJson(url, {
            body,
        })).body;
        if (!res?.data?.orb) {
            logger_1.logger.debug({ res }, `Failed to look up orb ${packageName}`);
            return null;
        }
        const { orb } = res.data;
        // Simplify response before caching and returning
        const homepage = orb.homeUrl?.length
            ? orb.homeUrl
            : `https://circleci.com/developer/orbs/orb/${packageName}`;
        const releases = orb.versions.map(({ version, createdAt }) => ({
            version,
            releaseTimestamp: createdAt ?? null,
        }));
        const dep = { homepage, isPrivate: !!orb.isPrivate, releases };
        logger_1.logger.trace({ dep }, 'dep');
        return dep;
    }
}
OrbDatasource.id = 'orb';
tslib_1.__decorate([
    (0, decorator_1.cache)({
        namespace: `datasource-${OrbDatasource.id}`,
        key: ({ packageName }) => packageName,
    })
], OrbDatasource.prototype, "getReleases", null);
exports.OrbDatasource = OrbDatasource;
//# sourceMappingURL=index.js.map