"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 });
exports.HotTesterAPI = void 0;
const HotAPI_1 = require("./HotAPI");
const HotRoute_1 = require("./HotRoute");
const HotRouteMethod_1 = require("./HotRouteMethod");
const HotStaq_1 = require("./HotStaq");
class HotTesterAPI extends HotAPI_1.HotAPI {
    constructor(baseUrl, connection = null, db = null) {
        super(baseUrl, connection, db);
        this.executeEventsUsing = HotAPI_1.EventExecutionType.HotAPI;
        let route = new HotRoute_1.HotRoute(connection, "tester");
        route.addMethod({
            "name": "pageLoaded",
            "onServerExecute": this.pageLoaded,
            "parameters": {
                "testerName": {
                    "required": true,
                    "type": "string",
                    "description": "The name of the tester executing the test."
                },
                "testerMap": {
                    "required": true,
                    "type": "string",
                    "description": "The tester map executing the test."
                },
                "pageName": {
                    "required": true,
                    "type": "string",
                    "description": "The name of the page executing the test."
                },
                "testElements": {
                    "required": true,
                    "type": "array",
                    "description": "The test elements on the page."
                },
                "testPaths": {
                    "required": true,
                    "type": "array",
                    "description": "The test paths on the page."
                }
            },
            "returns": "Returns true as an acknowledgement."
        });
        route.addMethod({
            "name": "pageUnloaded",
            "onServerExecute": this.pageUnloaded,
            "parameters": {
                "testerName": {
                    "required": true,
                    "type": "string",
                    "description": "The name of the tester executing the test."
                }
            },
            "returns": "Returns true as an acknowledgement."
        });
        route.addMethod({
            "name": "executeTests",
            "onServerExecute": this.executeTests,
            "parameters": {
                "testerName": {
                    "required": true,
                    "type": "string",
                    "description": "The name of the tester executing the test."
                },
                "testerMap": {
                    "required": true,
                    "type": "object",
                    "description": "The tester map to execute."
                }
            },
            "returns": "Returns true when tests are complete."
        });
        route.addMethod({
            "name": "heartbeat",
            "type": HotRouteMethod_1.HotEventMethod.GET,
            "onServerExecute": this.heartbeat,
            "returns": "Returns true as an acknowledgement."
        });
        this.addRoute(route);
    }
    /**
     * This is called when the page has finished loading in development mode.
     */
    pageLoaded(request) {
        return __awaiter(this, void 0, void 0, function* () {
            let testerObj = {
                testerName: request.jsonObj["testerName"],
                testElements: request.jsonObj["testElements"],
                testPathsStrs: request.jsonObj["testPaths"]
            };
            for (let key in testerObj) {
                // @ts-ignore
                let testObj = testerObj[key];
                let throwError = false;
                if (testObj == null)
                    throwError = true;
                if ((testerObj.testerName == "") ||
                    (testerObj.testElements === "") ||
                    (testerObj.testPathsStrs === "")) {
                    throwError = true;
                }
                if (throwError === true)
                    throw new Error(`TesterAPI: Object ${key} was not passed.`);
            }
            testerObj.testElements = JSON.parse(testerObj.testElements);
            testerObj.testPathsStrs = JSON.parse(testerObj.testPathsStrs);
            let testPaths = {};
            for (let key in testerObj.testPathsStrs) {
                let testPath = eval(testerObj.testPathsStrs[key]);
                testPaths[key] = testPath;
            }
            let tester = this.connection.processor.testers[testerObj.testerName];
            if (tester == null)
                throw new Error(`TesterAPI: Tester ${testerObj.testerName} does not exist!`);
            let newPage = {
                "testElements": testerObj.testElements,
                "testPaths": testPaths
            };
            tester.finishedLoading = true;
            let setAsCurrent = true;
            if (tester.onFinishedLoading != null)
                setAsCurrent = yield tester.onFinishedLoading(newPage);
            if (setAsCurrent === true)
                tester.currentPage = newPage;
            return (true);
        });
    }
    /**
     * This is called when the page has finished unloading in development mode.
     */
    pageUnloaded(request) {
        return __awaiter(this, void 0, void 0, function* () {
            const testerName = HotStaq_1.HotStaq.getParamUnsafe("testerName", request.jsonObj);
            let tester = this.connection.processor.testers[testerName];
            if (tester == null)
                throw new Error(`TesterAPI: Tester ${testerName} does not exist!`);
            tester.finishedLoading = false;
            tester.currentPage = null;
            if (tester.onFinishedUnloading != null)
                yield tester.onFinishedUnloading();
            return (true);
        });
    }
    /**
     * Execute the tests for a page.
     */
    executeTests(req) {
        return __awaiter(this, void 0, void 0, function* () {
            let testerName = req.jsonObj["testerName"];
            let testerMap = req.jsonObj["testerMap"];
            if ((testerName == null) || (testerMap == null))
                throw new Error("TesterAPI: Not all required json objects were passed.");
            if ((testerName === "") || (testerMap === ""))
                throw new Error("TesterAPI: Not all required json objects were passed.");
            let server = this.connection;
            // @ts-ignore
            if (server.executeTests != null) {
                // @ts-ignore
                yield server.executeTests(testerName, testerMap);
            }
            return (true);
        });
    }
    /**
     * Responds with true to heartbeat requests.
     */
    heartbeat(req) {
        return __awaiter(this, void 0, void 0, function* () {
            return (true);
        });
    }
}
exports.HotTesterAPI = HotTesterAPI;
//# sourceMappingURL=HotTesterAPI.js.map