"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.startProdServer = exports.printServerURLs = exports.getServerOptions = void 0;
const prod_server_1 = __importDefault(require("@modern-js/prod-server"));
const logger_1 = require("./logger");
const constants_1 = require("./constants");
const getServerOptions = (builderConfig) => {
    var _a, _b, _c, _d;
    return {
        output: {
            path: (_b = (_a = builderConfig.output) === null || _a === void 0 ? void 0 : _a.distPath) === null || _b === void 0 ? void 0 : _b.root,
            assetPrefix: (_c = builderConfig.output) === null || _c === void 0 ? void 0 : _c.assetPrefix,
            distPath: (_d = builderConfig.output) === null || _d === void 0 ? void 0 : _d.distPath,
        },
        source: {
            alias: {},
        },
        html: {},
        tools: {
            babel: {},
        },
        server: {},
        runtime: {},
        bff: {},
    };
};
exports.getServerOptions = getServerOptions;
async function printServerURLs(urls, name = 'Server') {
    const { chalk } = await Promise.resolve().then(() => __importStar(require('@modern-js/utils')));
    let message = `${name} running at:\n\n`;
    message += urls
        .map(({ label, url }) => `  ${`> ${label.padEnd(10)}`}${chalk.cyanBright(url)}\n`)
        .join('');
    logger_1.logger.info(message);
}
exports.printServerURLs = printServerURLs;
async function startProdServer(context, builderConfig) {
    var _a;
    if (!process.env.NODE_ENV) {
        process.env.NODE_ENV = 'production';
    }
    const { getPort, DEFAULT_DEV_HOST } = await Promise.resolve().then(() => __importStar(require('@modern-js/utils')));
    const port = await getPort(((_a = builderConfig.dev) === null || _a === void 0 ? void 0 : _a.port) || constants_1.DEFAULT_PORT);
    const server = await (0, prod_server_1.default)({
        pwd: context.rootPath,
        config: (0, exports.getServerOptions)(builderConfig),
    });
    await server.init();
    return new Promise(resolve => {
        server.listen({
            host: DEFAULT_DEV_HOST,
            port,
        }, async (err) => {
            if (err) {
                throw err;
            }
            const { getAddressUrls } = await Promise.resolve().then(() => __importStar(require('@modern-js/utils')));
            const urls = getAddressUrls('http', port);
            await printServerURLs(urls);
            resolve({
                port,
                urls: urls.map(item => item.url),
                server,
            });
        });
    });
}
exports.startProdServer = startProdServer;
