"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 () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApplicationScope = exports.MissingDefaultFilesOptionError = void 0;
const harper_logger_js_1 = require("../utility/logging/harper_logger.js");
const jsLoader_ts_1 = require("../security/jsLoader.js");
const env = __importStar(require("../utility/environment/environmentManager.js"));
const hdbTerms_ts_1 = require("../utility/hdbTerms.js");
class MissingDefaultFilesOptionError extends Error {
    constructor() {
        super('No default files option exists. Ensure `files` is specified in config.yaml');
        this.name = 'MissingDefaultFilesOptionError';
    }
}
exports.MissingDefaultFilesOptionError = MissingDefaultFilesOptionError;
/**
 * This class is used to represent the application scope for the VM context used for loading modules within an application
 */
class ApplicationScope {
    logger;
    resources;
    server;
    mode; // option to set this from the scope
    dependencyLoader; // option to set this from the scope
    allowedPath;
    config;
    moduleCache; // used by the loader to retain a cache of modules, type is an internal detail of the loader
    constructor(name, resources, server, isInternal = false) {
        this.logger = (0, harper_logger_js_1.forComponent)(name, !isInternal);
        this.resources = resources;
        this.server = server;
        this.mode = env.get(hdbTerms_ts_1.CONFIG_PARAMS.APPLICATIONS_MODULELOADER) ?? 'vm';
        this.dependencyLoader = env.get(hdbTerms_ts_1.CONFIG_PARAMS.APPLICATIONS_DEPENDENCYLOADER);
        if (env.get(hdbTerms_ts_1.CONFIG_PARAMS.APPLICATIONS_ALLOWEDDIRECTORY) !== 'app')
            this.allowedPath = ''; // this is used to match paths by startsWith, so empty string matches everything
    }
    /**
     * The compartment that is used for this scope and any imports that it makes
     */
    compartment;
    /**
     * Import a file into the scope's sandbox.
     * @param filePath - The path of the file to import.
     * @returns A promise that resolves with the imported module or value.
     */
    async import(filePath) {
        return (0, jsLoader_ts_1.scopedImport)(filePath, this);
    }
}
exports.ApplicationScope = ApplicationScope;
//# sourceMappingURL=ApplicationScope.js.map