"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 });
const electron_1 = require("electron");
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const config_loader_1 = require("./config_loader");
const api_client_1 = require("./api_client");
const shell_handlers_1 = require("./shell_handlers");
const file_handlers_1 = require("./file_handlers");
const system_handlers_1 = require("./system_handlers");
let mainWindow = null;
let cachedSeed = null;
let cachedFullConfig = null;
async function createWindow() {
    cachedSeed = await (0, config_loader_1.loadSeedConfig)();
    if (cachedSeed) {
        cachedFullConfig = await (0, api_client_1.fetchFullConfig)(cachedSeed);
    }
    mainWindow = new electron_1.BrowserWindow({
        width: 1600,
        height: 900,
        title: 'OSIT Aether Launcher (Native)',
        webPreferences: {
            preload: path.join(__dirname, '../preload/index.js'),
            contextIsolation: true,
            nodeIntegration: false,
        },
    });
    let targetUrl = 'http://demo.localhost:5173';
    if (cachedFullConfig && cachedFullConfig.native_device) {
        const device = cachedFullConfig.native_device;
        const eventId = device.event_id_random || device.event_id;
        const locationId = device.event_location_id_random || device.event_location_id || '';
        // Use app_base_url from the device record (e.g. bgh.oneskyit.com).
        // Fall back to localhost only if nothing is configured — never override a real domain.
        const host = device.app_base_url || 'demo.localhost:5173';
        // Use https for real domains; localhost dev URLs stay on http
        const protocol = host.includes('localhost') ? 'http' : 'https';
        targetUrl = `${protocol}://${host}/events/${eventId}/launcher/${locationId}`;
    }
    // Only open DevTools in development (not in a packaged .app build)
    if (!electron_1.app.isPackaged)
        mainWindow.webContents.openDevTools();
    mainWindow.loadURL(targetUrl).catch(() => {
        mainWindow?.loadURL('https://dev-demo.oneskyit.com/');
    });
    mainWindow.on('closed', () => { mainWindow = null; });
}
(0, shell_handlers_1.registerShellHandlers)();
(0, file_handlers_1.registerFileHandlers)();
(0, system_handlers_1.registerSystemHandlers)();
electron_1.app.on('ready', createWindow);
electron_1.app.on('window-all-closed', () => {
    if (process.platform !== 'darwin')
        electron_1.app.quit();
});
electron_1.app.on('activate', () => {
    if (mainWindow === null)
        createWindow();
});
electron_1.ipcMain.handle('get-seed-config', async () => cachedSeed || await (0, config_loader_1.loadSeedConfig)());
electron_1.ipcMain.handle('get-device-config', async () => cachedFullConfig);
electron_1.ipcMain.handle('get-jwt', async () => null);
electron_1.ipcMain.handle('get-device-info', async () => {
    const interfaces = os.networkInterfaces();
    const addresses = [];
    for (const name of Object.keys(interfaces)) {
        for (const net of interfaces[name]) {
            if (net.family === 'IPv4' && !net.internal) {
                addresses.push(net.address);
            }
        }
    }
    return {
        platform: os.platform(),
        release: os.release(),
        arch: os.arch(),
        hostname: os.hostname(),
        cpus: os.cpus().length,
        total_mem: os.totalmem(),
        free_mem: os.freemem(),
        ip_addresses: addresses,
        home_directory: os.homedir(),
        tmp_directory: os.tmpdir()
    };
});
//# sourceMappingURL=index.js.map