"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.showPublicPage = showPublicPage;
const commandContext_1 = require("../commandContext");
const messages_1 = require("../messages");
function buildPublicPageUrl(base, relativePath) {
    return `${base.replace(/\/$/, '')}/${relativePath}`;
}
async function showPublicPage(command, relativePath) {
    await (0, commandContext_1.withPublicEnvironment)(command, async ({ envConfig }) => {
        if (!envConfig.cdnBase) {
            (0, messages_1.printErrorWithHelp)(`Environment "${envConfig.name}" does not include a public content base URL.`, ['Set PLAYDROP_CDN_BASE or use an environment that publishes public content assets.'], { command });
            process.exitCode = 1;
            return;
        }
        let response;
        try {
            response = await fetch(buildPublicPageUrl(envConfig.cdnBase, relativePath));
        }
        catch (error) {
            (0, messages_1.printErrorWithHelp)(`Fetching ${command} failed.`, [error instanceof Error ? error.message : String(error)], { command });
            process.exitCode = 1;
            return;
        }
        if (!response.ok) {
            (0, messages_1.printErrorWithHelp)(`Failed to fetch ${command}.`, [`HTTP ${response.status} ${response.statusText}`], { command });
            process.exitCode = 1;
            return;
        }
        process.stdout.write(await response.text());
    });
}
