"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const analytics_broker_1 = require("./analytics-broker");
const file_log_service_1 = require("../../detached-processes/file-log-service");
const yok_1 = require("../../common/yok");
const pathToBootstrap = process.argv[2];
if (!pathToBootstrap || !fs.existsSync(pathToBootstrap)) {
    throw new Error("Invalid path to bootstrap.");
}
const logFile = process.argv[3];
require(pathToBootstrap);
const analyticsLoggingService = yok_1.injector.resolve(file_log_service_1.FileLogService, { logFile });
analyticsLoggingService.logData({ message: "Initializing AnalyticsBroker." });
const analyticsBroker = yok_1.injector.resolve(analytics_broker_1.AnalyticsBroker, {
    pathToBootstrap,
    analyticsLoggingService,
});
let trackingQueue = Promise.resolve();
const sendDataForTracking = (data) => __awaiter(void 0, void 0, void 0, function* () {
    trackingQueue = trackingQueue.then(() => analyticsBroker.sendDataForTracking(data));
    yield trackingQueue;
});
const finishTracking = (data) => __awaiter(void 0, void 0, void 0, function* () {
    analyticsLoggingService.logData({
        message: `analytics-broker-process finish tracking started`,
    });
    yield trackingQueue;
    analyticsLoggingService.logData({
        message: `analytics-broker-process tracking finished`,
    });
});
const killCurrentProcessGracefully = () => {
    yok_1.injector.dispose();
    process.exit();
};
process.on("message", (data) => __awaiter(void 0, void 0, void 0, function* () {
    analyticsLoggingService.logData({
        message: `analytics-broker-process received message of type: ${JSON.stringify(data)}`,
    });
    if (data.type === "PreviewAppData") {
        return;
    }
    if (data.type === "FinishTracking") {
        yield finishTracking();
        if (process.connected) {
            analyticsLoggingService.logData({
                message: `analytics-broker-process will send ${"ProcessFinishedTasks"} message`,
            });
            process.send("ProcessFinishedTasks", () => {
                analyticsLoggingService.logData({
                    message: `analytics-broker-process sent ${"ProcessFinishedTasks"} message and will exit gracefully now`,
                });
                killCurrentProcessGracefully();
            });
        }
        return;
    }
    yield sendDataForTracking(data);
}));
process.on("disconnect", () => __awaiter(void 0, void 0, void 0, function* () {
    analyticsLoggingService.logData({
        message: "analytics-broker-process received process.disconnect event",
    });
    yield finishTracking();
    killCurrentProcessGracefully();
}));
analyticsLoggingService.logData({
    message: `analytics-broker-process will send ${"ProcessReadyToReceive"} message`,
});
process.send("ProcessReadyToReceive");
