"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const UrlQuery = __importStar(require("query-string"));
const FS = __importStar(require("fs"));
const Server = __importStar(require("./server"));
const Utils = __importStar(require("../../util/src"));
const Project = __importStar(require("../../project/src"));
const QRCode = require('qrcode-terminal');
const handlers = Object.create(null);
handlers['file'] = onFile;
handlers['preview'] = onPreview;
handlers['qrcode'] = onQRCode;
handlers['devices'] = onDevices;
handlers['cdp2'] = onCube2Devtools;
handlers['cdp'] = onDevtools2Cube;
function launch() {
    Server.Singleton.receiveSocketMessage = (data) => {
        try {
            const message = JSON.parse(data);
            let func = undefined;
            if (message.key) {
                if (message.key === 'cdp2') {
                    func = handlers['cdp2'];
                }
                //  其他场景 只输出日志即可
            }
            else {
                //  没有配置 key 时总是认为由 devtools 投递而来
                func = handlers['cdp'];
            }
            if (func) {
                func(message);
            }
        }
        catch (error) {
        }
    };
    Utils.Console.info(`路由模块启动完毕`);
}
exports.launch = launch;
/*
    HTTP Request
*/
Server.Singleton.server.on('request', (request, response) => {
    const queryinfo = UrlQuery.parseUrl(`${request.url}`);
    if (queryinfo.url === '/api') {
        const func = handlers[`${queryinfo.query.action}`];
        if (func) {
            func(queryinfo.query, request, response);
        }
        else {
            response.statusCode = 404;
            response.end();
        }
    }
});
function onFile(params, request, response) {
    const path = `${params.path}`;
    if (FS.existsSync(path)) {
        response.statusCode = 200;
        response.end(FS.readFileSync(path));
    }
    else {
        response.statusCode = 201;
        response.end();
    }
}
function onPreview(params, request, response) {
    const path = `${params.path}`;
    Project.preview(path).then(value => {
        response.statusCode = 200;
        response.end();
    }).catch(reason => {
        response.statusCode = 201;
        response.statusMessage = `${reason}`;
        response.end();
    });
}
function onQRCode(params, request, response) {
    if (Server.Singleton.server.listening) {
        QRCode.generate(Server.qrvalue(), { small: true }, (value) => {
            response.setHeader('Content-Type', 'text/plain');
            response.write(`${value}`);
            response.statusCode = 200;
            response.end();
        });
    }
    else {
        response.statusCode = 201;
        response.statusMessage = `服务未启动`;
        response.end();
    }
}
function onDevices(params, request, response) {
    response.setHeader('Content-Type', 'application/json');
    response.write(JSON.stringify(Server.clientSessions()));
    response.statusCode = 200;
    response.end();
}
//  Cube => Devtools
function onCube2Devtools(message) {
    for (const client of Server.Singleton.wss.clients) {
        if (client.session === undefined) {
            client.send(JSON.stringify(message.payload));
        }
    }
}
//  Devtools => Cube
function onDevtools2Cube(message) {
    if (Server.clientSessions().length > 0) {
        Server.broadcase(JSON.stringify({
            key: 'cdp',
            payload: message
        }));
    }
}
//# sourceMappingURL=router.js.map