"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MablAction = void 0;
exports.argumentIsMablVariable = argumentIsMablVariable;
exports.parseArgument = parseArgument;
exports.distinctStrings = distinctStrings;
exports.convertObjectToMablscriptArgs = convertObjectToMablscriptArgs;
const mablscriptFind_1 = require("./mablscriptFind");
const MablSymbol_1 = require("./MablSymbol");
class MablAction {
    name;
    actionCode;
    args;
    actionSourceIndexInStep;
    cumulativeActionSourceIndexInFlow;
    constructor(name, actionCode, args) {
        this.name = name;
        this.args = args;
        this.actionCode = actionCode;
    }
    getActionArgs() {
        return this.args ? this.args : [];
    }
    setActionSourceIndexInStep(index) {
        this.actionSourceIndexInStep = index;
    }
    getActionSourceIndexInStep() {
        return this.actionSourceIndexInStep;
    }
    getActionCode() {
        return this.actionCode;
    }
    subVariable(argument) {
        if (argumentIsMablVariable(argument)) {
            const matches = argument.match(MablSymbol_1.SINGLE_VARIABLE_PATTERN_REGEX);
            if (matches) {
                return matches[1];
            }
        }
        return `${argument}`;
    }
    getActionName() {
        return this.name;
    }
    toMablscript() {
        return '';
    }
    static findUniqueVariableReferencesInValue(value) {
        const variableReferences = [];
        if ((0, MablSymbol_1.isMablSymbolObject)(value)) {
            return [value.name];
        }
        else if (typeof value === 'string') {
            variableReferences.push(...(0, mablscriptFind_1.findVariables)(value));
        }
        else if (Array.isArray(value)) {
            variableReferences.push(...value.flatMap((v) => MablAction.findUniqueVariableReferencesInValue(v)));
        }
        else if (typeof value === 'object') {
            variableReferences.push(...Object.values(value).flatMap((v) => MablAction.findUniqueVariableReferencesInValue(v)));
        }
        else {
            return [];
        }
        return distinctStrings(variableReferences);
    }
    getInputVariables() {
        return [];
    }
    getOutputVariables() {
        return [];
    }
}
exports.MablAction = MablAction;
function argumentIsMablVariable(argument) {
    return MablSymbol_1.SINGLE_VARIABLE_PATTERN_REGEX.test(argument);
}
function parseArgument(arg, convertStringToSymbol = true) {
    if (convertStringToSymbol && typeof arg === 'string') {
        const name = MablSymbol_1.SINGLE_VARIABLE_PATTERN_REGEX.exec(arg)?.[1];
        if (name) {
            return new MablSymbol_1.MablSymbol(name);
        }
    }
    return arg;
}
function distinctStrings(values) {
    return Array.from(new Set(values));
}
function convertObjectToMablscriptArgs(objectArgs) {
    const mablArgs = Object.keys(objectArgs)
        .sort()
        .filter((key) => objectArgs?.[key] !== undefined)
        .map((key) => {
        switch (typeof objectArgs[key]) {
            case 'object':
                return `${key}: ${convertObjectToMablscriptArgs(objectArgs[key])}`;
            case 'boolean':
            case 'number':
                return `${key}: ${objectArgs[key]}`;
            default:
                return `${key}: "${(0, mablscriptFind_1.escapeMablscriptString)(objectArgs[key])}"`;
        }
    });
    return `{${mablArgs.join(', ')}}`;
}
