"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MAX_DOM_EVENT_IDENTIFIERS = void 0;
exports.resolveVariableValue = resolveVariableValue;
const MablSymbol_1 = require("@mablhq/test-model/MablSymbol");
const variable_utils_1 = require("../../variable-utils");
const uuid_1 = require("uuid");
const importer_1 = require("@mablhq/test-model/importer");
const mablscript_utils_1 = require("../mablscript-utils");
const lodash_get_1 = __importDefault(require("lodash.get"));
exports.MAX_DOM_EVENT_IDENTIFIERS = 20;
class BaseStep {
    constructor(stepType) {
        this.stepType = stepType;
        this.disabled = false;
        this.sourceEventIdentifiers = [];
    }
    setDisabled(disabled) {
        this.disabled = disabled;
    }
    static create(_descriptor) {
        throw new Error('create() must be implemented in subclass');
    }
    generateStepId() {
        this.id = (0, uuid_1.v4)();
    }
    setStepId(id) {
        this.id = id;
    }
    mergeOptions(updatedOptions) {
        this.options = {
            ...(this.options ? this.options : {}),
            ...updatedOptions,
        };
    }
    willGenerateJsonSteps() {
        return false;
    }
    willGenerateMablscript() {
        return true;
    }
    generateMablscript() {
        throw new Error(`Unimplemented function generateMablscript for ${this.stepType}`);
    }
    generateMablStep() {
        const mablStep = (0, importer_1.parseMablScriptIntoV2Step)((0, mablscript_utils_1.translateToMablScript)([this]));
        if (this.annotations) {
            mablStep.setAnnotation(this.annotations);
        }
        return mablStep;
    }
    inputVariables() {
        return [];
    }
    static maybeExtractVariable(valueWithVariableMaybe, variables) {
        try {
            const symbol = MablSymbol_1.MablSymbol.fromString(valueWithVariableMaybe);
            const variable = resolveVariableValue(symbol.name, variables);
            if ((0, variable_utils_1.isVariableOnImport)(variable)) {
                return variable;
            }
            console.warn(`Did not receive a proper variable back from resolveVariableValue for "${symbol.name}", using default var`);
            return {
                name: `flow.${symbol.name}`,
                humanizedName: `flow.${symbol.name}`,
                value: undefined,
            };
        }
        catch (e) {
            if (e.message.includes('Invalid MablSymbol')) {
                return;
            }
            throw e;
        }
    }
}
exports.default = BaseStep;
function extractVariableObjectByName(variables, variableExpression) {
    try {
        const variableExpressionRegex = /(?<variableName>[^\[\]]+)(?:\[(?<index>[^\[\]]+)\])?/;
        const { variableName, index } = variableExpressionRegex.exec(variableExpression)?.groups ?? {};
        if (variableName === undefined) {
            return undefined;
        }
        const variable = variables[variableName] ??
            (0, lodash_get_1.default)(variables, variableName) ??
            variables[`user.${variableName}`] ??
            (0, lodash_get_1.default)(variables, `user.${variableName}`);
        return index !== undefined ? undefined : variable;
    }
    catch {
        return undefined;
    }
}
function resolveVariableValue(variableName, variables) {
    return extractVariableObjectByName(variables, variableName);
}
