// The bootstrap provides protos.
import * as protos from '@devvit/protos';
import { Actor } from '@devvit/shared-types/Actor.js';
/**
 * Dangerously executes untrusted code and returns the Actor class. An Error is
 * thrown if the code does not adhere to the BundleModule interface.
 */
export async function dangerouslyGetBundleActor(code) {
    const ActorClass = await dangerouslyGetBundleDefaultExport(code);
    if (ActorClass == null)
        throw Error(`Missing default export: ${code}`);
    if (typeof ActorClass !== 'function')
        throw Error('Default export is not a function; expected Actor subclass.');
    // Actor is multiply defined so `instanceof` and `isPrototypeOf()` tests fail.
    return ActorClass;
}
/**
 * Dangerously executes untrusted code and returns the default export, if any.
 * The function is asynchronous because we don't know if there are top-level
 * awaits in the code.
 */
async function dangerouslyGetBundleDefaultExport(code) {
    const module = {};
    // The bootstrap provides protos.
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    const require = function (path) {
        if (path === '@devvit/protos')
            return protos;
        // play does not implement a bundler and expects @devvit/public-api to exist
        // in the environment. When running in compute-go, the library is injected
        // as a global.
        if (path === '@devvit/public-api' && 'devvitPublicAPI' in globalThis)
            return globalThis.devvitPublicAPI;
        return globalThis.require(path);
    };
    // JS please forgive us
    // TODO DX-63
    // eslint-disable-next-line security/detect-eval-with-expression
    eval(code);
    return module.exports?.default;
}
class ConcreteActor extends Actor {
    constructor(cfg) {
        super(cfg);
    }
}
