"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPublicContext = exports.createContextByConfig = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
const fs_2 = require("./fs");
const logger_1 = require("@modern-js/utils/logger");
/**
 * Create context by config.
 */
function createContextByConfig(options, outputConfig, bundlerType) {
    const { cwd, target, configPath, framework } = options;
    const rootPath = cwd;
    const srcPath = (0, path_1.join)(rootPath, 'src');
    const distPath = (0, fs_2.getAbsoluteDistPath)(cwd, outputConfig);
    const cachePath = (0, path_1.join)(rootPath, 'node_modules', '.cache');
    const context = {
        entry: options.entry,
        target,
        srcPath,
        rootPath,
        distPath,
        cachePath,
        framework,
        bundlerType,
    };
    if (configPath && (0, fs_1.existsSync)(configPath)) {
        context.configPath = configPath;
    }
    return context;
}
exports.createContextByConfig = createContextByConfig;
function createPublicContext(context) {
    const exposedKeys = [
        'entry',
        'target',
        'srcPath',
        'rootPath',
        'distPath',
        'devServer',
        'framework',
        'cachePath',
        'configPath',
        'tsconfigPath',
        'bundlerType',
    ];
    // Using Proxy to get the current value of context.
    return new Proxy(context, {
        get(target, prop) {
            if (exposedKeys.includes(prop)) {
                return target[prop];
            }
            return undefined;
        },
        set(target, prop) {
            logger_1.logger.error(`Context is readonly, you can not assign to the "context.${prop}" prop.`);
            return true;
        },
    });
}
exports.createPublicContext = createPublicContext;
