import _ from 'lodash';
// TODO(pbardea): Rename "view mode" everywhere to something more descriptive.
export const PUBLISHED_VIEW_MODE = true;
export class ExecutionContext {
    globals;
    variables;
    kvStore;
    outputs;
    preparedStatementContext;
    error;
    errorContext;
    constructor(context) {
        this.globals = context && context.globals ? _.cloneDeep(context.globals) : {};
        this.outputs = context && context.outputs ? _.cloneDeep(context.outputs) : {};
        this.preparedStatementContext = context && context.preparedStatementContext ? _.cloneDeep(context.preparedStatementContext) : [];
    }
    // Do not merge the objects in arrays. Just replace the entire array.
    static customMerger(objValue, srcValue) {
        if (_.isArray(objValue)) {
            return srcValue;
        }
    }
    addGlobalVariable(name, value) {
        const obj = { [name]: value };
        this.globals = _.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 = _.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 (_.isEmpty(this.globals)) {
            return [];
        }
        return Object.keys(this.globals);
    }
    outputBindingKeys() {
        if (_.isEmpty(this.outputs)) {
            return [];
        }
        return Object.keys(this.outputs);
    }
}
export var WorkflowExecutionParamsKey;
(function (WorkflowExecutionParamsKey) {
    WorkflowExecutionParamsKey["QUERY_PARAMS"] = "params";
    WorkflowExecutionParamsKey["BODY"] = "body";
})(WorkflowExecutionParamsKey || (WorkflowExecutionParamsKey = {}));
export var ApiReservedQueryParams;
(function (ApiReservedQueryParams) {
    ApiReservedQueryParams["AUTH"] = "sb-auth";
    ApiReservedQueryParams["ENVIRONMENT"] = "environment";
    ApiReservedQueryParams["TEST"] = "test";
    ApiReservedQueryParams["RAW"] = "raw";
    ApiReservedQueryParams["PROFILE"] = "profile";
})(ApiReservedQueryParams || (ApiReservedQueryParams = {}));
export 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' });
        }
    }
}
//# sourceMappingURL=execution.js.map