import * as color from '@heroku/heroku-cli-util/color';
import { ux } from '@oclif/core/ux';
import { getTeam } from '../api.js';
export function getOwner(heroku, apps, pipeline) {
    let owner;
    let ownerPromise;
    if (pipeline.owner.type === 'team') {
        ownerPromise = getTeam(heroku, pipeline.owner.id).then(response => response.body);
    }
    else {
        const app = apps.find(app => (app.owner ? app.owner.id === pipeline.owner.id : false));
        // If pipeline owner doesn't own any application and type is user (unlikely)
        // We return the uuid as default
        owner = app ? app.owner && app.owner.email : pipeline.owner.id;
        ownerPromise = Promise.resolve(owner);
    }
    return ownerPromise.then(owner => (owner.name ? `${color.team(owner.name)} (team)` : color.user(owner)));
}
export function warnMixedOwnership(pipelineApps, pipeline, owner) {
    const hasMixedOwnership = pipelineApps.some(app => (app.owner && app.owner.id) !== pipeline.owner.id);
    if (hasMixedOwnership) {
        ux.stdout();
        let message = `Some apps in this pipeline do not belong to ${color.team(owner)}.`;
        message += '\n\nAll apps in a pipeline must have the same owner as the pipeline owner.';
        message += '\nTransfer these apps or change the pipeline owner in pipeline settings.';
        message += `\nSee ${color.info('https://devcenter.heroku.com/articles/pipelines')} for more info.`;
        ux.warn(message);
    }
}
