"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const loggingProvider_1 = require("../providers/logging/loggingProvider");
const Observer_1 = require("./Observer");
const syntheticActionCodes_1 = require("./syntheticActionCodes");
const OBSERVATION_ARTIFACT_NAME = 'variables.json';
class VariablesObserver extends Observer_1.AfterObserver {
    constructor(dispatcher) {
        super(Observer_1.ObservationType.VARIABLES, dispatcher);
        this.observeAfterFunction = async (context) => {
            const executionIndex = context.executionIndex;
            const properties = context.properties;
            if (!properties?.variables) {
                return {};
            }
            try {
                const variables = JSON.parse(properties.variables);
                if (variables?.resolvedInAction?.length === 0 &&
                    variables?.registeredInAction?.length === 0 &&
                    executionIndex.actionName !== syntheticActionCodes_1.CAPTURE_APPLICATION_STATE_ACTION_CODE) {
                    return {};
                }
            }
            catch (e) {
                loggingProvider_1.logger.warn('Unable to parse variables as JSON to check resolved/registered, ' +
                    'will write out file without checking.', e);
            }
            const tmpFile = await this.writeTempFile(OBSERVATION_ARTIFACT_NAME, properties.variables);
            const filesByObservationType = {};
            if (tmpFile) {
                filesByObservationType[this.observationType] = { uri: tmpFile };
            }
            return { filesByObservationType };
        };
        this.observeAfter = async (options) => {
            if (!options.properties?.isIntermediateAction) {
                await super.observeAfter(options);
            }
        };
    }
}
exports.default = VariablesObserver;
