import os from 'node:os';
import AppConfig from '../appConfig.js';
import constants from '../constants.js';
import InfoDisplayer from '../infoDisplayer.js';
import OutputManager from '../outputManager.js';
import ImageAccessDetailsRetriever from './imageAccessDetailsRetriever.js';
export default class ImageAccessDetailsDisplayer {
    imageId;
    imageName;
    constructor(name, id) {
        this.imageName = name;
        this.imageId = id;
    }
    async display(force = false) {
        const imageAccessDetailsRetriever = new ImageAccessDetailsRetriever(this.imageName, this.imageId);
        const { expiresAt, imageUrl, password, readOnly, username } = await imageAccessDetailsRetriever.get(force);
        const registryHost = imageUrl.split('/')[0];
        const type = readOnly ? 'Pull' : 'Push/Pull';
        /* eslint-disable perfectionist/sort-maps */
        const overviewInfoDisplayer = new InfoDisplayer(new Map([
            ['URL', imageUrl],
            ['Username', username],
            ['Password', password],
            ['Access Type', type],
            ['Expires', new Date(expiresAt).toLocaleString()]
        ]));
        /* eslint-enable perfectionist/sort-maps */
        overviewInfoDisplayer.displayAsTable();
        if (os.platform() === 'win32') {
            OutputManager.warn('The information below targets a Unix-like environment and may need to be adapted for use on Windows.');
            OutputManager.newline();
        }
        OutputManager.info('You can use the `docker` CLI to work with this repository using commands like the following:');
        OutputManager.newline();
        const commands = [
            ['Login', `docker login -u ${username} -p ${password} ${registryHost}`],
            ['Pull', `docker pull ${imageUrl}`],
        ];
        if (!readOnly) {
            commands.push(['Build', `docker build -t ${imageUrl} .`], ['Tag', `docker tag <source-tag> ${imageUrl}`], ['Push', `docker push ${imageUrl}`]);
        }
        const detailsDisplayer = new InfoDisplayer(new Map(commands));
        detailsDisplayer.displayAsDetails();
        if (!readOnly) {
            OutputManager.info('After pushing your image, you can reference it as the build image name in config/package.yml and/or as the execution image name in config/run/*.yml.');
        }
        // Display platform warning if CLI is not running on amd64 AND no Build Host is provisioned
        const appConfig = await AppConfig.getInstance();
        const buildHostEnvironmentId = appConfig.getConfig('environmentId', 'cloud');
        const osArch = os.arch();
        if (osArch !== 'x64' && !buildHostEnvironmentId) {
            OutputManager.newline();
            OutputManager.warn(OutputManager.bold(`WARNING: Only images built for the ${constants.RUNTIME_ARCH} platform are supported, but your system architecture is ${osArch}. Be sure to specify \`--platform ${constants.RUNTIME_ARCH}\` platform when pulling/building your image.`));
        }
    }
}
