"use strict";
/**
 * Main entry point for the AI Interactive Fiction application
 */
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 });
const dotenv = __importStar(require("dotenv"));
const game_runner_1 = require("./cli/game-runner");
// Import the server module and the startServer function for the web interface
const server_1 = require("./server");
const game_config_1 = require("./config/game-config");
// Load environment variables
console.log('Loading environment variables...');
try {
    const result = dotenv.config();
    if (result.error) {
        console.error('Error loading .env file:', result.error);
    }
    else {
        console.log('Environment variables loaded successfully');
    }
}
catch (error) {
    console.error('Exception when loading env:', error);
}
async function main() {
    try {
        console.log('=== AI Interactive Fiction ===');
        console.log('A modern take on classic text adventures with LLM-powered interactions');
        console.log('');
        // Get the world file path from the YAML engine config, with environment override.
        const engineConfig = (0, game_config_1.loadGameConfig)(process.env.YAML_CONFIG_FILE || './config/engines/yaml.json', 'yaml');
        const worldFile = (0, game_config_1.projectPath)(process.env.DEFAULT_WORLD_FILE || engineConfig.paths.mainGameFile);
        console.log(`Using world file: ${worldFile}`);
        console.log(`OpenRouter API Key: ${process.env.OPENROUTER_API_KEY ? '✓ Found' : '✗ Missing'}`);
        console.log(`OpenRouter Model: ${process.env.OPENROUTER_MODEL || '✗ Not specified'}`);
        // Check if we should run in CLI mode
        const args = process.argv.slice(2);
        const cliMode = args.includes('--cli') || args.includes('-c');
        if (cliMode) {
            // CLI mode
            console.log('Starting in CLI mode...');
            // Create game runner and initialize
            console.log('Creating game runner...');
            const gameRunner = new game_runner_1.GameRunner();
            console.log('Initializing game...');
            await gameRunner.initialize(worldFile);
            // Start the CLI game
            console.log('Starting CLI game...');
            await gameRunner.start();
        }
        else {
            // Web interface mode - explicitly start the server with port fallback
            console.log('Starting in web interface mode...');
            // Get port configuration
            const DEFAULT_PORT = 3000;
            const PORT = process.env.PORT ? parseInt(process.env.PORT) : DEFAULT_PORT;
            const PORT_RANGE = 300;
            // Start the web server with port fallback
            console.log('Starting web server...');
            await (0, server_1.startServer)(PORT, PORT_RANGE);
        }
    }
    catch (error) {
        console.error('Failed to start:', error);
        if (error instanceof Error) {
            console.error('Error name:', error.name);
            console.error('Error message:', error.message);
            console.error('Error stack:', error.stack);
        }
        process.exit(1);
    }
}
// Start the application
console.log('Starting application...');
main().catch(error => {
    console.error('Unhandled error in main:', error);
    process.exit(1);
});
//# sourceMappingURL=index.js.map