"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MablCoreProcessAction = exports.MablCoreAction = exports.ObservationErrorSeverity = exports.ObservationErrorCode = exports.ExecutionPhase = exports.EventChannelMessageType = exports.mablEventEmitter = exports.MablCoreEventEmitter = void 0;
exports.getEmitter = getEmitter;
exports.getDefaultLogMetadataForInfo = getDefaultLogMetadataForInfo;
exports.createAssertFailureMessage = createAssertFailureMessage;
exports.createAssertResultMessage = createAssertResultMessage;
const events_1 = require("events");
const loggingProvider_1 = require("../../providers/logging/loggingProvider");
const pureUtil_1 = require("../../util/pureUtil");
const NO_ID_PLACEHOLDER = 'NO_ID_PROVIDED';
class MablCoreEventEmitter {
    constructor(debug) {
        this.messageIndex = -1;
        this.outputEventChannel = new events_1.EventEmitter();
        this.inputEventChannel = new events_1.EventEmitter();
        this.debugOutputOn = debug ?? false;
    }
    getOutputEventChannel() {
        return this.outputEventChannel;
    }
    resetOutputEventChannel() {
        this.outputEventChannel = new events_1.EventEmitter();
    }
    getInputEventChannel() {
        return this.inputEventChannel;
    }
    getNextMessageIndex() {
        this.messageIndex += 1;
        return this.messageIndex;
    }
    dispatch(action, metadata) {
        this.outputEventChannel.emit(EventChannelMessageType.coreAction, action, metadata);
    }
    sendToProcess(action) {
        this.inputEventChannel.emit(EventChannelMessageType.coreProcessAction, action);
    }
    log(line, timestamp, color, metadata) {
        this.outputEventChannel.emit(EventChannelMessageType.outputLogLine, line, timestamp, color, metadata);
    }
    debug(line, timestamp, color, metadata) {
        const effectiveMetadata = metadata ?? {
            logLevel: loggingProvider_1.LogLevel.Debug,
            timestamp: Date.now(),
        };
        this.log(line, timestamp, color, {
            ...effectiveMetadata,
            shouldOutputInConsole: this.debugOutputOn,
        });
    }
}
exports.MablCoreEventEmitter = MablCoreEventEmitter;
global.mablEventEmitter = global.mablEventEmitter ?? new MablCoreEventEmitter();
exports.mablEventEmitter = global.mablEventEmitter;
function getEmitter(eventEmitter) {
    return eventEmitter ?? exports.mablEventEmitter;
}
var EventChannelMessageType;
(function (EventChannelMessageType) {
    EventChannelMessageType["coreAction"] = "coreAction";
    EventChannelMessageType["coreProcessAction"] = "coreProcessAction";
    EventChannelMessageType["outputLogLine"] = "outputLogLine";
})(EventChannelMessageType || (exports.EventChannelMessageType = EventChannelMessageType = {}));
var ExecutionPhase;
(function (ExecutionPhase) {
    ExecutionPhase["AFTER_ACTION"] = "AFTER_ACTION";
    ExecutionPhase["AFTER_DEFERRED_ACTION"] = "AFTER_DEFERRED_ACTION";
    ExecutionPhase["AFTER_FLOW"] = "AFTER_FLOW";
    ExecutionPhase["AFTER_TEST"] = "AFTER_TEST";
    ExecutionPhase["ASYNC"] = "ASYNC";
    ExecutionPhase["BEFORE_ACTION"] = "BEFORE_ACTION";
    ExecutionPhase["BEFORE_FLOW"] = "BEFORE_FLOW";
    ExecutionPhase["BEFORE_TEST"] = "BEFORE_TEST";
    ExecutionPhase["DURING_ACTION"] = "DURING_ACTION";
    ExecutionPhase["SKIP_ACTION"] = "SKIP_ACTION";
})(ExecutionPhase || (exports.ExecutionPhase = ExecutionPhase = {}));
var ObservationErrorCode;
(function (ObservationErrorCode) {
    ObservationErrorCode["ILLEGAL_URL_ERROR"] = "ILLEGAL_URL_ERROR";
    ObservationErrorCode["PAGE_LOAD_FAILURE"] = "PAGE_LOAD_FAILURE";
})(ObservationErrorCode || (exports.ObservationErrorCode = ObservationErrorCode = {}));
var ObservationErrorSeverity;
(function (ObservationErrorSeverity) {
    ObservationErrorSeverity["ERROR"] = "ERROR";
    ObservationErrorSeverity["WARN"] = "WARN";
})(ObservationErrorSeverity || (exports.ObservationErrorSeverity = ObservationErrorSeverity = {}));
class MablCoreAction {
    constructor(type, payload, timestamp = Date.now()) {
        this.type = type;
        this.payload = payload;
        this.timestamp = timestamp;
    }
}
exports.MablCoreAction = MablCoreAction;
class MablCoreProcessAction extends MablCoreAction {
}
exports.MablCoreProcessAction = MablCoreProcessAction;
function getDefaultLogMetadataForInfo(testId, testInvariantId) {
    return {
        executionIndex: {
            testId: testId ?? NO_ID_PLACEHOLDER,
            testInvariantId: testInvariantId ?? NO_ID_PLACEHOLDER,
        },
        executionPhase: ExecutionPhase.BEFORE_TEST,
        logLevel: loggingProvider_1.LogLevel.Info,
        timestamp: Date.now(),
    };
}
function createAssertFailureMessage(assertFailure) {
    if (assertFailure) {
        const { reason, expected, found, onFailure, shortReason } = assertFailure;
        return {
            reason,
            shortReason,
            expected: (0, pureUtil_1.stringifyIfPresent)(expected),
            found: (0, pureUtil_1.stringifyIfPresent)(found),
            onFailure,
        };
    }
    return;
}
function createAssertResultMessage(assertResult) {
    if (assertResult) {
        const { success, assertionCriteria, downloadURI } = assertResult;
        return {
            success,
            assertionCriteria,
            downloadURI,
            ...createAssertFailureMessage(assertResult),
        };
    }
    return;
}
