import { AmbiguousError } from '../../errors/ambiguous.js';
import { NotFound } from '../../errors/not-found.js';
export default class AddonAttachmentResolver {
    heroku;
    attachmentHeaders = {
        Accept: 'application/vnd.heroku+json; version=3.sdk',
        'Accept-Inclusion': 'addon:plan,config_vars',
    };
    constructor(heroku) {
        this.heroku = heroku;
    }
    async resolve(appId, attachmentId, options = {}) {
        const { addonService, namespace } = options;
        let attachments = [];
        /* eslint-disable camelcase */
        ({ body: attachments } = await this.heroku.post('/actions/addon-attachments/resolve', {
            body: {
                addon_attachment: attachmentId,
                // We would pass the add-on service slug here for Platform API to filter the attachments by
                // add-on service, but the resolvers won't allow alpha add-ons to be found.
                // addon_service: addonService,
                app: appId,
            },
            headers: this.attachmentHeaders,
        }));
        /* eslint-enable camelcase */
        // We implement the add-on service filtering logic here if the parameter is provided.
        if (addonService) {
            attachments = attachments.filter(attachment => attachment.addon.plan.name.split(':', 2)[0] === addonService);
        }
        return this.singularize(attachments, namespace);
    }
    singularize(attachments, namespace) {
        let matches;
        if (namespace) {
            matches = attachments.filter(m => m.namespace === namespace);
        }
        else if (attachments.length > 1) {
            // In cases that aren't specific enough, keep only attachments without a namespace
            matches = attachments.filter(m => !Reflect.has(m, 'namespace') || m.namespace === null);
        }
        else {
            matches = attachments;
        }
        switch (matches.length) {
            case 0: {
                throw new NotFound();
            }
            case 1: {
                return matches[0];
            }
            default: {
                throw new AmbiguousError(matches, 'addon_attachment');
            }
        }
    }
}
