"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExecutionOutput = exports.ApiReservedQueryParams = exports.WorkflowExecutionParamsKey = exports.ExecutionContext = exports.PUBLISHED_VIEW_MODE = void 0;
const lodash_1 = __importDefault(require("lodash"));
// TODO(pbardea): Rename "view mode" everywhere to something more descriptive.
exports.PUBLISHED_VIEW_MODE = true;
class ExecutionContext {
    globals;
    variables;
    kvStore;
    outputs;
    preparedStatementContext;
    error;
    errorContext;
    constructor(context) {
        this.globals = context && context.globals ? lodash_1.default.cloneDeep(context.globals) : {};
        this.outputs = context && context.outputs ? lodash_1.default.cloneDeep(context.outputs) : {};
        this.preparedStatementContext = context && context.preparedStatementContext ? lodash_1.default.cloneDeep(context.preparedStatementContext) : [];
    }
    // Do not merge the objects in arrays. Just replace the entire array.
    static customMerger(objValue, srcValue) {
        if (lodash_1.default.isArray(objValue)) {
            return srcValue;
        }
    }
    addGlobalVariable(name, value) {
        const obj = { [name]: value };
        this.globals = lodash_1.default.mergeWith(obj, this.globals, ExecutionContext.customMerger);
    }
    addGlobalsOverride(global) {
        for (const key of global.keys()) {
            this.addGlobalVariableOverride(key, global.get(key));
        }
    }
    addGlobalVariableOverride(name, value) {
        const obj = { [name]: value };
        this.globals = lodash_1.default.mergeWith(this.globals, obj, ExecutionContext.customMerger);
    }
    addOutput(name, output) {
        this.outputs[name] = output;
    }
    merge(other) {
        this.globals = {
            ...this.globals,
            ...other.globals
        };
        this.outputs = {
            ...this.outputs,
            ...other.outputs
        };
    }
    globalBindingKeys() {
        if (lodash_1.default.isEmpty(this.globals)) {
            return [];
        }
        return Object.keys(this.globals);
    }
    outputBindingKeys() {
        if (lodash_1.default.isEmpty(this.outputs)) {
            return [];
        }
        return Object.keys(this.outputs);
    }
}
exports.ExecutionContext = ExecutionContext;
var WorkflowExecutionParamsKey;
(function (WorkflowExecutionParamsKey) {
    WorkflowExecutionParamsKey["QUERY_PARAMS"] = "params";
    WorkflowExecutionParamsKey["BODY"] = "body";
})(WorkflowExecutionParamsKey || (exports.WorkflowExecutionParamsKey = WorkflowExecutionParamsKey = {}));
var ApiReservedQueryParams;
(function (ApiReservedQueryParams) {
    ApiReservedQueryParams["AUTH"] = "sb-auth";
    ApiReservedQueryParams["ENVIRONMENT"] = "environment";
    ApiReservedQueryParams["TEST"] = "test";
    ApiReservedQueryParams["RAW"] = "raw";
    ApiReservedQueryParams["PROFILE"] = "profile";
})(ApiReservedQueryParams || (exports.ApiReservedQueryParams = ApiReservedQueryParams = {}));
class ExecutionOutput {
    error;
    authError;
    children;
    startTimeUtc;
    executionTime;
    log;
    structuredLog;
    output;
    request;
    placeholdersInfo;
    integrationErrorCode;
    constructor() {
        this.output = {};
        this.log = [];
        this.structuredLog = [];
    }
    static fromJSONString(json) {
        const obj = JSON.parse(json);
        const instance = new ExecutionOutput();
        instance.error = obj.error;
        instance.children = obj.children;
        instance.executionTime = obj.executionTime;
        instance.log = obj.log;
        instance.structuredLog = obj.structuredLog;
        instance.output = obj.output;
        instance.request = obj.request;
        instance.startTimeUtc = obj.startTimeUtc;
        return instance;
    }
    logInfo(msg) {
        if (msg) {
            this.log.push(`${msg}`);
            this.structuredLog.push({ msg, level: 'info' });
        }
    }
    logWarn(msg) {
        if (msg) {
            this.log.push(`[WARN] ${msg}`);
            this.structuredLog.push({ msg, level: 'warn' });
        }
    }
    logError(msg, authError) {
        if (msg) {
            this.error = msg;
            this.authError = authError;
            this.log.push(`[ERROR] ${msg}`);
            this.structuredLog.push({ msg, level: 'error' });
        }
    }
}
exports.ExecutionOutput = ExecutionOutput;
//# sourceMappingURL=execution.js.map