"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ambiguous_1 = require("../../errors/ambiguous");
class AddonResolver {
    constructor(heroku) {
        this.heroku = heroku;
        this.addonHeaders = {
            Accept: 'application/vnd.heroku+json; version=3.sdk',
            'Accept-Expansion': 'addon_service,plan',
        };
    }
    async resolve(addon, app, addonService) {
        var _a, _b;
        const [appPart, addonPart] = (_b = (_a = addon.match(/^(.+)::(.+)$/)) === null || _a === void 0 ? void 0 : _a.slice(1)) !== null && _b !== void 0 ? _b : [app, addon];
        const { body: addons } = await this.heroku.post('/actions/addons/resolve', {
            body: {
                addon: addonPart,
                addon_service: addonService,
                app: appPart,
            },
            headers: this.addonHeaders,
        });
        if (addons.length === 1) {
            return addons[0];
        }
        throw new ambiguous_1.AmbiguousError(addons, 'addon');
    }
}
exports.default = AddonResolver;
