"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = (module)=>{
        var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
var __webpack_exports__ = {};
const external_node_child_process_namespaceObject = require("node:child_process");
const external_node_path_namespaceObject = require("node:path");
var external_node_path_default = /*#__PURE__*/ __webpack_require__.n(external_node_path_namespaceObject);
const external_node_util_namespaceObject = require("node:util");
const prompts_namespaceObject = require("@inquirer/prompts");
const android_namespaceObject = require("@midscene/android");
const playground_namespaceObject = require("@midscene/playground");
const constants_namespaceObject = require("@midscene/shared/constants");
const node_namespaceObject = require("@midscene/shared/node");
const external_node_fs_namespaceObject = require("node:fs");
const external_node_http_namespaceObject = require("node:http");
require("node:url");
const logger_namespaceObject = require("@midscene/shared/logger");
const external_cors_namespaceObject = require("cors");
var external_cors_default = /*#__PURE__*/ __webpack_require__.n(external_cors_namespaceObject);
const external_express_namespaceObject = require("express");
var external_express_default = /*#__PURE__*/ __webpack_require__.n(external_express_namespaceObject);
const external_socket_io_namespaceObject = require("socket.io");
function _define_property(obj, key, value) {
    if (key in obj) Object.defineProperty(obj, key, {
        value: value,
        enumerable: true,
        configurable: true,
        writable: true
    });
    else obj[key] = value;
    return obj;
}
const debugPage = (0, logger_namespaceObject.getDebug)('android:playground');
const promiseExec = (0, external_node_util_namespaceObject.promisify)(external_node_child_process_namespaceObject.exec);
const LOOPBACK_HOSTS = new Set([
    'localhost',
    '127.0.0.1',
    '::1',
    '[::1]'
]);
function isLoopbackOrigin(origin) {
    if (!origin) return true;
    try {
        const url = new URL(origin);
        return LOOPBACK_HOSTS.has(url.hostname);
    } catch  {
        return false;
    }
}
class ScrcpyServer {
    setupApiRoutes() {
        this.app.get('/api/devices', async (req, res)=>{
            try {
                const devices = await this.getDevicesList();
                res.json({
                    devices,
                    currentDeviceId: this.currentDeviceId
                });
            } catch (error) {
                res.status(500).json({
                    error: error.message || 'Failed to get devices list'
                });
            }
        });
    }
    async getDevicesList() {
        try {
            debugPage('start to get devices list');
            const client = await this.getAdbClient();
            if (!client) {
                console.warn('failed to get adb client');
                return [];
            }
            debugPage('success to get adb client, start to request devices list');
            let devices;
            try {
                devices = await client.getDevices();
                debugPage('original devices list:', devices);
            } catch (error) {
                console.error('failed to get devices list:', error);
                return [];
            }
            if (!devices || 0 === devices.length) return [];
            const formattedDevices = devices.map((device)=>{
                const result = {
                    id: device.serial,
                    name: device.product || device.model || device.serial,
                    status: device.state || 'device'
                };
                return result;
            });
            return formattedDevices;
        } catch (error) {
            console.error('failed to get devices list:', error);
            return [];
        }
    }
    async getAdbClient() {
        const { AdbServerClient } = await import("@yume-chan/adb");
        const { AdbServerNodeTcpConnector } = await import("@yume-chan/adb-server-node-tcp");
        try {
            if (this.adbClient) debugPage('use existing adb client');
            else {
                await promiseExec('adb start-server');
                debugPage('adb server started');
                debugPage('initialize adb client');
                this.adbClient = new AdbServerClient(new AdbServerNodeTcpConnector({
                    host: '127.0.0.1',
                    port: 5037
                }));
                await debugPage('success to initialize adb client');
            }
            return this.adbClient;
        } catch (error) {
            console.error('failed to get adb client:', error);
            return null;
        }
    }
    async getAdb(deviceId) {
        const { Adb } = await import("@yume-chan/adb");
        try {
            const client = await this.getAdbClient();
            if (!client) return null;
            const targetDeviceId = deviceId || this.currentDeviceId;
            if (targetDeviceId) {
                this.currentDeviceId = targetDeviceId;
                return new Adb(await client.createTransport({
                    serial: targetDeviceId
                }));
            }
            const devices = await client.getDevices();
            if (0 === devices.length) return null;
            this.currentDeviceId = devices[0].serial;
            return new Adb(await client.createTransport(devices[0]));
        } catch (error) {
            console.error('failed to get adb client:', error);
            return null;
        }
    }
    async startScrcpy(adb, options = {}) {
        const { AdbScrcpyClient, AdbScrcpyOptions3_3_3 } = await import("@yume-chan/adb-scrcpy");
        const { ReadableStream } = await import("@yume-chan/stream-extra");
        const { DefaultServerPath } = await import("@yume-chan/scrcpy");
        const currentDir = __dirname;
        const serverBinPath = external_node_path_default().resolve(currentDir, '../../bin/scrcpy-server');
        try {
            await AdbScrcpyClient.pushServer(adb, ReadableStream.from((0, external_node_fs_namespaceObject.createReadStream)(serverBinPath)));
            const scrcpyOptions = new AdbScrcpyOptions3_3_3({
                audio: false,
                control: true,
                maxSize: 1024,
                videoBitRate: 2000000,
                ...options
            });
            return await AdbScrcpyClient.start(adb, DefaultServerPath, scrcpyOptions);
        } catch (error) {
            console.error('failed to start scrcpy:', error);
            throw error;
        }
    }
    setupSocketHandlers() {
        this.io.on('connection', async (socket)=>{
            debugPage('client connected, id: %s, client address: %s', socket.id, socket.handshake.address);
            let scrcpyClient = null;
            let adb = null;
            const sendDevicesList = async ()=>{
                try {
                    debugPage('Socket request to get devices list');
                    const devices = await this.getDevicesList();
                    debugPage('send devices list to client:', devices);
                    socket.emit('devices-list', {
                        devices,
                        currentDeviceId: this.currentDeviceId
                    });
                } catch (error) {
                    console.error('failed to send devices list:', error);
                    socket.emit('error', {
                        message: 'failed to get devices list'
                    });
                }
            };
            await sendDevicesList();
            socket.on('get-devices', async ()=>{
                debugPage('received client request to get devices list');
                await sendDevicesList();
            });
            socket.on('switch-device', async (deviceId)=>{
                debugPage('received client request to switch device:', deviceId);
                try {
                    if (scrcpyClient) {
                        await scrcpyClient.close();
                        scrcpyClient = null;
                    }
                    this.currentDeviceId = deviceId;
                    debugPage('device switched to:', deviceId);
                    socket.emit('device-switched', {
                        deviceId
                    });
                    this.io.emit('global-device-switched', {
                        deviceId,
                        timestamp: Date.now()
                    });
                } catch (error) {
                    console.error('failed to switch device:', error);
                    socket.emit('error', {
                        message: `Failed to switch device: ${error?.message || 'Unknown error'}`
                    });
                }
            });
            socket.on('connect-device', async (options)=>{
                const { ScrcpyVideoCodecId } = await import("@yume-chan/scrcpy");
                try {
                    debugPage('received device connection request, options: %s, client id: %s', options, socket.id);
                    adb = await this.getAdb(this.currentDeviceId || void 0);
                    if (!adb) {
                        console.error('no available device found');
                        socket.emit('error', {
                            message: 'No device found'
                        });
                        return;
                    }
                    debugPage('starting scrcpy service, device id: %s', this.currentDeviceId);
                    scrcpyClient = await this.startScrcpy(adb, options);
                    debugPage('scrcpy service started successfully');
                    debugPage('check scrcpyClient object structure: %s', Object.getOwnPropertyNames(scrcpyClient).map((name)=>{
                        const type = typeof scrcpyClient[name];
                        const isPromise = 'object' === type && scrcpyClient[name] && 'function' == typeof scrcpyClient[name].then;
                        return `${name}: ${type}${isPromise ? ' (Promise)' : ''}`;
                    }));
                    try {
                        if (scrcpyClient.videoStream) {
                            debugPage('videoStream exists, type: %s', typeof scrcpyClient.videoStream);
                            let videoStream;
                            if ('object' == typeof scrcpyClient.videoStream && 'function' == typeof scrcpyClient.videoStream.then) {
                                debugPage('videoStream is a Promise, waiting for resolution...');
                                videoStream = await scrcpyClient.videoStream;
                            } else {
                                debugPage('videoStream is not a Promise, directly use');
                                videoStream = scrcpyClient.videoStream;
                            }
                            debugPage('video stream fetched successfully, metadata: %s', videoStream.metadata);
                            const metadata = videoStream.metadata || {};
                            debugPage('original metadata: %s', metadata);
                            if (!metadata.codec) {
                                debugPage('metadata does not have codec field, use H264 by default');
                                metadata.codec = ScrcpyVideoCodecId.H264;
                            }
                            if (!metadata.width || !metadata.height) {
                                debugPage('metadata does not have width or height field, use default values');
                                metadata.width = metadata.width || 1080;
                                metadata.height = metadata.height || 1920;
                            }
                            debugPage('prepare to send video-metadata event to client, data: %s', JSON.stringify(metadata));
                            socket.emit('video-metadata', metadata);
                            debugPage('video-metadata event sent to client, id: %s', socket.id);
                            const { stream } = videoStream;
                            const reader = stream.getReader();
                            const processStream = async ()=>{
                                try {
                                    while(true){
                                        const { done, value } = await reader.read();
                                        if (done) break;
                                        const frameType = value.type || 'data';
                                        socket.emit('video-data', {
                                            data: Array.from(value.data),
                                            type: frameType,
                                            timestamp: Date.now(),
                                            keyFrame: value.keyFrame
                                        });
                                    }
                                } catch (error) {
                                    console.error('error processing video stream:', error);
                                    socket.emit('error', {
                                        message: 'video stream processing error'
                                    });
                                }
                            };
                            processStream();
                        } else {
                            console.error('scrcpyClient object does not have videoStream property');
                            socket.emit('error', {
                                message: 'Video stream not available in scrcpy client'
                            });
                        }
                    } catch (error) {
                        console.error('error processing video stream:', error);
                        socket.emit('error', {
                            message: `Video stream processing error: ${error.message}`
                        });
                    }
                    if (scrcpyClient?.controller) socket.emit('control-ready');
                } catch (error) {
                    console.error('failed to connect device:', error);
                    socket.emit('error', {
                        message: `Failed to connect device: ${error?.message || 'Unknown error'}`
                    });
                }
            });
            socket.on('disconnect', async (reason)=>{
                debugPage('client disconnected, id: %s, reason: %s', socket.id, reason);
                if (scrcpyClient) {
                    try {
                        debugPage('closing scrcpy client');
                        await scrcpyClient.close();
                    } catch (error) {
                        console.error('failed to close scrcpy client:', error);
                    }
                    scrcpyClient = null;
                }
            });
        });
    }
    async launch(port) {
        this.port = port || this.defaultPort;
        return new Promise((resolve)=>{
            this.httpServer.listen(this.port, ()=>{
                console.log(`Scrcpy server running at: http://localhost:${this.port}`);
                this.startDeviceMonitoring();
                resolve(this);
            });
        });
    }
    startDeviceMonitoring() {
        this.devicePollInterval = setInterval(async ()=>{
            try {
                const devices = await this.getDevicesList();
                const currentDevicesJson = JSON.stringify(devices);
                if (this.lastDeviceList !== currentDevicesJson) {
                    debugPage('devices list changed, push to all connected clients');
                    this.lastDeviceList = currentDevicesJson;
                    if (!this.currentDeviceId && devices.length > 0) {
                        const onlineDevices = devices.filter((device)=>'device' === device.status.toLowerCase());
                        if (onlineDevices.length > 0) {
                            this.currentDeviceId = onlineDevices[0].id;
                            debugPage('auto select the first online device:', this.currentDeviceId);
                        }
                    }
                    this.io.emit('devices-list', {
                        devices,
                        currentDeviceId: this.currentDeviceId
                    });
                }
            } catch (error) {
                console.error('device monitoring error:', error);
            }
        }, 3000);
    }
    close() {
        if (this.devicePollInterval) {
            clearInterval(this.devicePollInterval);
            this.devicePollInterval = null;
        }
        if (this.httpServer) return this.httpServer.close();
    }
    constructor(){
        _define_property(this, "app", void 0);
        _define_property(this, "httpServer", void 0);
        _define_property(this, "io", void 0);
        _define_property(this, "port", void 0);
        _define_property(this, "defaultPort", constants_namespaceObject.SCRCPY_SERVER_PORT);
        _define_property(this, "adbClient", null);
        _define_property(this, "currentDeviceId", null);
        _define_property(this, "devicePollInterval", null);
        _define_property(this, "lastDeviceList", '');
        this.app = external_express_default()();
        this.httpServer = (0, external_node_http_namespaceObject.createServer)(this.app);
        this.io = new external_socket_io_namespaceObject.Server(this.httpServer, {
            cors: {
                origin (origin, callback) {
                    callback(null, isLoopbackOrigin(origin));
                },
                methods: [
                    'GET',
                    'POST'
                ],
                credentials: true
            }
        });
        this.app.use(external_cors_default()({
            origin (origin, callback) {
                callback(null, isLoopbackOrigin(origin));
            },
            credentials: true
        }));
        this.setupSocketHandlers();
        this.setupApiRoutes();
    }
}
(0, external_node_util_namespaceObject.promisify)(external_node_child_process_namespaceObject.exec);
async function getAdbDevices() {
    try {
        const devices = await (0, android_namespaceObject.getConnectedDevices)();
        return devices.filter((device)=>'device' === device.state).map((device)=>({
                id: device.udid,
                status: device.state,
                name: device.udid
            }));
    } catch (error) {
        console.error('Error getting ADB devices:', error);
        return [];
    }
}
async function selectDevice() {
    console.log('🔍 Scanning for Android devices...');
    const devices = await getAdbDevices();
    if (0 === devices.length) {
        console.error('❌ No Android devices found!');
        console.log('📱 Please ensure:');
        console.log('  • Your device is connected via USB');
        console.log('  • USB debugging is enabled');
        console.log('  • Device is authorized for debugging');
        process.exit(1);
    }
    if (1 === devices.length) {
        console.log(`📱 Found device: ${devices[0].name} (${devices[0].id})`);
        return devices[0].id;
    }
    const choices = devices.map((device)=>({
            name: `${device.name} (${device.id})`,
            value: device.id
        }));
    const selectedDevice = await (0, prompts_namespaceObject.select)({
        message: '📱 Multiple devices found. Please select one:',
        choices
    });
    return selectedDevice;
}
const staticDir = external_node_path_default().join(__dirname, '../../static');
const main = async ()=>{
    const { default: open } = await import("open");
    try {
        const selectedDeviceId = await selectDevice();
        console.log(`✅ Selected device: ${selectedDeviceId}`);
        const scrcpyServer = new ScrcpyServer();
        scrcpyServer.currentDeviceId = selectedDeviceId;
        console.log('🚀 Starting servers...');
        const availablePlaygroundPort = await (0, node_namespaceObject.findAvailablePort)(constants_namespaceObject.PLAYGROUND_SERVER_PORT);
        const availableScrcpyPort = await (0, node_namespaceObject.findAvailablePort)(constants_namespaceObject.SCRCPY_SERVER_PORT);
        if (availablePlaygroundPort !== constants_namespaceObject.PLAYGROUND_SERVER_PORT) console.log(`⚠️  Port ${constants_namespaceObject.PLAYGROUND_SERVER_PORT} is busy, using port ${availablePlaygroundPort} instead`);
        if (availableScrcpyPort !== constants_namespaceObject.SCRCPY_SERVER_PORT) console.log(`⚠️  Port ${constants_namespaceObject.SCRCPY_SERVER_PORT} is busy, using port ${availableScrcpyPort} instead`);
        const [playgroundResult] = await Promise.all([
            (0, playground_namespaceObject.playgroundForAgentFactory)(async ()=>{
                const device = new android_namespaceObject.AndroidDevice(selectedDeviceId);
                await device.connect();
                return new android_namespaceObject.AndroidAgent(device);
            }).launch({
                port: availablePlaygroundPort,
                openBrowser: false,
                verbose: false,
                staticPath: staticDir,
                configureServer (server) {
                    server.scrcpyPort = availableScrcpyPort;
                }
            }),
            scrcpyServer.launch(availableScrcpyPort)
        ]);
        const playgroundServer = playgroundResult.server;
        console.log('');
        console.log('✨ Midscene Android Playground is ready!');
        console.log(`🎮 Playground: http://localhost:${playgroundServer.port}`);
        console.log(`📱 Device: ${selectedDeviceId}`);
        console.log(`🔑 Generated Server ID: ${playgroundServer.id}`);
        console.log('');
        open(`http://localhost:${playgroundServer.port}`);
    } catch (error) {
        console.error('Failed to start servers:', error);
        process.exit(1);
    }
};
main();
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
