export const supportedFunctions = [ "SUM", "AVERAGE", "MAX", "MIN", "COUNT", "COUNTA", "STDEV", "VAR", "MEDIAN", "IF", "AND", "OR", "NOT", "ROUND", "FLOOR", "CEILING", "ABS", "SQRT", "POWER", "MOD", "INT", "RAND", "TODAY", "NOW", "YEAR", "MONTH", "DAY", "HOUR", "MINUTE", "SECOND" ];

function findMatchingCloseParen(formula, openParenIndex) {
    let depth = 1;
    for (let i = openParenIndex + 1; i < formula.length; i++) if ("(" === formula[i]) depth++; else if (")" === formula[i] && (depth--, 
    0 === depth)) return i;
    return -1;
}

export function detectFunctionParameterPosition(formula, cursorPosition) {
    if (!formula.startsWith("=")) return {
        inParamMode: !1,
        functionParamPosition: null
    };
    const functionRegex = /([A-Za-z]+)\s*\(/g;
    let match, positionBeforeCursor = -1;
    for (;null !== (match = functionRegex.exec(formula)); ) {
        match[1].toUpperCase();
        const functionStart = match.index, openParenIndex = formula.indexOf("(", functionStart);
        if (openParenIndex < cursorPosition && openParenIndex > positionBeforeCursor) {
            const closeParenIndex = findMatchingCloseParen(formula, openParenIndex);
            if (cursorPosition > openParenIndex && (-1 === closeParenIndex || cursorPosition <= closeParenIndex)) return {
                inParamMode: !0,
                functionParamPosition: {
                    start: openParenIndex + 1,
                    end: cursorPosition
                }
            };
            if (cursorPosition === openParenIndex + 1) return {
                inParamMode: !0,
                functionParamPosition: {
                    start: openParenIndex + 1,
                    end: cursorPosition
                }
            };
            positionBeforeCursor = openParenIndex;
        }
    }
    if (cursorPosition > 1 && cursorPosition <= formula.length) {
        const prevChar = formula[cursorPosition - 1];
        if ([ "+", "-", "*", "/", "=", ">", "<", "&", "|", "^", "(", "," ].includes(prevChar)) return {
            inParamMode: !0,
            functionParamPosition: {
                start: cursorPosition,
                end: cursorPosition
            }
        };
    }
    return 1 === cursorPosition && "=" === formula[0] ? {
        inParamMode: !0,
        functionParamPosition: {
            start: cursorPosition,
            end: cursorPosition
        }
    } : {
        inParamMode: !1,
        functionParamPosition: null
    };
}
//# sourceMappingURL=formula-helper.js.map