/**
 * Serves as an Actor MCP SSE server entry point.
 * This file needs to be named `main.ts` to be recognized by the Apify platform.
 */

!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="9985651f-f1cb-5f8a-8b1c-87e0af1d2b27")}catch(e){}}();
var _a;
import { Actor } from 'apify';
import log from '@apify/log';
import { createExpressApp } from './actor/server.js';
import { ApifyClient } from './apify_client.js';
import { processInput } from './input.js';
import { callActorGetDataset } from './tools/index.js';
const STANDBY_MODE = Actor.getEnv().metaOrigin === 'STANDBY';
await Actor.init();
const HOST = Actor.isAtHome() ? process.env.ACTOR_STANDBY_URL : 'http://localhost';
const PORT = Actor.isAtHome() ? Number(process.env.ACTOR_STANDBY_PORT) : 3001;
if (!process.env.APIFY_TOKEN) {
    log.error('APIFY_TOKEN is required but not set in the environment variables.');
    process.exit(1);
}
const input = processInput((_a = (await Actor.getInput())) !== null && _a !== void 0 ? _a : {});
log.info('Loaded input', { input: JSON.stringify(input) });
if (STANDBY_MODE) {
    // In standby mode, actors and tools are provided via URL query params per request
    // Start express app
    const app = createExpressApp(HOST);
    log.info('Actor is running in the STANDBY mode.');
    app.listen(PORT, () => {
        log.info('Actor web server listening', { host: HOST, port: PORT });
    });
}
else {
    log.info('Actor is not designed to run in the NORMAL model (use this mode only for debugging purposes)');
    if (input && !input.debugActor && !input.debugActorInput) {
        await Actor.fail('If you need to debug a specific Actor, please provide the debugActor and debugActorInput fields in the input');
    }
    const options = { memory: input.maxActorMemoryBytes };
    const apifyClient = new ApifyClient({ token: process.env.APIFY_TOKEN });
    const callResult = await callActorGetDataset({ actorName: input.debugActor, input: input.debugActorInput, apifyClient, callOptions: options });
    if (callResult && callResult.previewItems.length > 0) {
        await Actor.pushData(callResult.previewItems);
        log.info('Pushed items to dataset', { itemCount: callResult.previewItems.length });
    }
    await Actor.exit();
}
// So Ctrl+C works locally
process.on('SIGINT', async () => {
    log.info('Received SIGINT, shutting down gracefully...');
    await Actor.exit();
});
//# sourceMappingURL=main.js.map
//# debugId=9985651f-f1cb-5f8a-8b1c-87e0af1d2b27
