"use strict";
/**
 * @file: serve a page for diff, select and patch
 * @author: yangqianjun
 * @Date: 2019-12-17 20:16:34
 * @LastEditors: yangqianjun
 * @LastEditTime: 2020-01-03 10:23:25
 */
/* eslint-disable @typescript-eslint/no-var-requires */
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var detect_port_1 = tslib_1.__importDefault(require("detect-port"));
var express_1 = tslib_1.__importDefault(require("express"));
var body_parser_1 = tslib_1.__importDefault(require("body-parser"));
var open_1 = tslib_1.__importDefault(require("open"));
var chalk_1 = tslib_1.__importDefault(require("chalk"));
var consts_1 = require("../consts");
var diff_1 = require("./diff");
function serveDiff(curVersion, newVersion) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, detect_port_1.default(3007).then(function (port) {
                        return new Promise(function (resolve) {
                            var _a = diff_1.diffAndPatch(curVersion, newVersion), selectDelta = _a.selectDelta, patch = _a.patch, delta = _a.delta;
                            var diffVersion = "" + Math.random();
                            if (delta) {
                                var app = express_1.default();
                                var server_1 = undefined;
                                var close_1 = function () {
                                    var _a;
                                    (_a = server_1) === null || _a === void 0 ? void 0 : _a.close();
                                };
                                app.use('/static', express_1.default.static(consts_1.StaticDir, { cacheControl: true }));
                                app.get('/', function (req, res) {
                                    res.send("\n<html>\n  <head>\n    <link rel=\"stylesheet\" href=\"/static/html.css\">\n    <link rel=\"stylesheet\" href=\"/static/annotated.css\">\n    <link rel=\"stylesheet\" href=\"/static/service.css\">\n    <script src=\"/static/jsondiffpatch.umd.js\"></script>\n  </head>\n  <body>\n    <div id=\"menu\" class=\"menu\"></div>\n    <div id=\"canvas\" class=\"canvas\"></div>\n    <script>\n      const diffVersion=\"" + diffVersion + "\";\n      const curVersion = " + JSON.stringify(curVersion, null, 2) + ";\n      const delta = " + JSON.stringify(delta, null, 2) + ";\n    </script>\n    <script src=\"/static/service.js\"></script>\n  </body>\n</html>\n          ");
                                });
                                app.get('/diff', function (req, res) {
                                    res.json({
                                        code: 0,
                                        result: delta
                                    });
                                });
                                app.post('/patch', body_parser_1.default.json(), function (req, res) {
                                    var _a = req.body, keys = _a.keys, version = _a.version, unkeys = _a.unkeys;
                                    if (version === diffVersion) {
                                        res.json({
                                            code: 0
                                        });
                                        // 全量
                                        if (!unkeys.length) {
                                            resolve(newVersion);
                                            // 增量
                                        }
                                        else if (keys.length) {
                                            var select = selectDelta(keys);
                                            patch(select);
                                            resolve(curVersion);
                                        }
                                        close_1();
                                    }
                                    else {
                                        res.json({
                                            code: 1,
                                            message: 'Diff 版本不一致，无法同步'
                                        });
                                    }
                                });
                                server_1 = app.listen(port, function () {
                                    var url = "http://127.0.0.1:" + port + "/";
                                    console.log(chalk_1.default.green("[INFO]: \u6253\u5F00\u94FE\u63A5 " + url + " \u7F16\u8F91"));
                                    open_1.default(url);
                                });
                                process.once('exit', close_1);
                            }
                            else {
                                resolve();
                            }
                        });
                    })];
                case 1: return [2 /*return*/, _a.sent()];
            }
        });
    });
}
exports.serveDiff = serveDiff;
//# sourceMappingURL=serve.js.map