"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.runUploadedHostedLaunchCheck = exports.runLocalHostedLaunchCheck = exports.formatHostedLaunchCheckFailure = exports.runFormatScript = exports.uploadApp = exports.buildApp = exports.validateAppTask = exports.collectAppValidationWarnings = void 0;
exports.runAppPipeline = runAppPipeline;
const validate_1 = require("./validate");
Object.defineProperty(exports, "collectAppValidationWarnings", { enumerable: true, get: function () { return validate_1.collectAppValidationWarnings; } });
Object.defineProperty(exports, "validateAppTask", { enumerable: true, get: function () { return validate_1.validateAppTask; } });
Object.defineProperty(exports, "runFormatScript", { enumerable: true, get: function () { return validate_1.runFormatScript; } });
const build_1 = require("./build");
Object.defineProperty(exports, "buildApp", { enumerable: true, get: function () { return build_1.buildApp; } });
const upload_1 = require("./upload");
Object.defineProperty(exports, "uploadApp", { enumerable: true, get: function () { return upload_1.uploadApp; } });
const launchCheck_1 = require("./launchCheck");
Object.defineProperty(exports, "formatHostedLaunchCheckFailure", { enumerable: true, get: function () { return launchCheck_1.formatHostedLaunchCheckFailure; } });
Object.defineProperty(exports, "runLocalHostedLaunchCheck", { enumerable: true, get: function () { return launchCheck_1.runLocalHostedLaunchCheck; } });
Object.defineProperty(exports, "runUploadedHostedLaunchCheck", { enumerable: true, get: function () { return launchCheck_1.runUploadedHostedLaunchCheck; } });
const registration_1 = require("./registration");
async function runAppPipeline(client, task, options) {
    await (0, validate_1.validateAppTask)(task);
    // External apps don't need to be built - they're hosted elsewhere
    const isExternal = task.hostingMode === 'EXTERNAL' || !!task.externalUrl;
    const requiresRegisteredShell = !isExternal && task.authMode === 'REQUIRED';
    if (requiresRegisteredShell && options?.ensureRegisteredAppShell) {
        const creatorUsername = typeof options.creatorUsername === 'string' ? options.creatorUsername.trim() : '';
        if (!creatorUsername) {
            throw new Error(`Missing creator username for required hosted app registration of "${task.name}".`);
        }
        await (0, registration_1.ensureRegisteredHostedAppShell)({
            client,
            creatorUsername,
            task,
        });
    }
    if (!isExternal && options?.runLocalLaunchCheck) {
        const creatorUsername = typeof options.creatorUsername === 'string' ? options.creatorUsername.trim() : '';
        if (!creatorUsername) {
            throw new Error(`Missing creator username for local hosted launch-check of "${task.name}".`);
        }
        const apiBase = typeof options.apiBase === 'string' ? options.apiBase.trim() : '';
        if (!apiBase) {
            throw new Error(`Missing API base URL for local hosted launch-check of "${task.name}".`);
        }
        const launchCheck = await (0, launchCheck_1.runLocalHostedLaunchCheck)({
            client,
            apiBase,
            webBase: options.webBase,
            creatorUsername,
            task,
            timeoutMs: options.launchCheckTimeoutMs,
            token: options.token ?? null,
            currentUser: options.user ?? null,
        });
        if (launchCheck.status !== 'PASSED') {
            throw new Error((0, launchCheck_1.formatHostedLaunchCheckFailure)(task.name, launchCheck, 'local'));
        }
    }
    const artifacts = isExternal ? null : await (0, build_1.buildApp)(task);
    const warnings = (0, validate_1.collectAppValidationWarnings)(task, isExternal ? 'source' : 'bundle');
    const uploadOptions = {
        skipEcs: options?.skipEcs,
        skipReview: options?.skipReview,
        clearTags: options?.clearTags,
        creatorUsername: options?.creatorUsername,
        token: options?.token,
        user: options?.user ?? null,
        runStagedUploadLaunchCheck: options?.runStagedUploadLaunchCheck,
        launchCheckTimeoutMs: options?.launchCheckTimeoutMs,
    };
    const upload = await (0, upload_1.uploadApp)(client, task, artifacts, uploadOptions);
    return { artifacts, upload, warnings };
}
