import { buildExpressionParser, } from "@feedloop/expression-parser";
import { evaluate, javascriptConfig, mergeConfig, } from "@feedloop/formula-editor";
import dayjs from "dayjs";
const functions = [
    {
        name: "FormatDate",
        description: "format date with given formatting tokens",
        parameters: [
            {
                name: "format",
                type: "string",
                description: "string of formatting tokens",
            },
            { name: "date", type: "date", description: "date to format" },
        ],
        return: {
            type: "string",
            description: "string formatted date",
        },
        evaluate: (format, date) => dayjs(new Date(date)).format(format),
    },
    {
        name: "Filtrex",
        parameters: [
            { name: "expression", type: "string", description: "filtrex expression" },
        ],
        return: {
            type: "any",
            description: "evaluated expression",
        },
        evaluate: (expression) => evaluateExpression(expression),
    },
    {
        name: "FormatCurrency",
        description: "format number into currency with given locale and currency code",
        parameters: [
            { name: "number", type: "number" },
            {
                name: "locale",
                type: "string",
                description: "ISO 639-1 code with optional ISO 3166 code",
            },
            {
                name: "currency",
                type: "string",
                description: "ISO 4217 code",
            },
        ],
        examples: [
            'FormatCurrency(100000, "id", "IDR")',
            'FormatCurrency(100000, "en-US", "USD")',
        ],
        return: {
            type: "string",
            description: "string formatted currency",
        },
        evaluate: (number, locale, currency) => new Intl.NumberFormat(locale, {
            style: "currency",
            currency,
            currencyDisplay: "narrowSymbol",
        }).format(number),
    },
];
const operators = [];
const variables = [];
export const qoreAppsFormulaConfigs = { functions, operators, variables };
export const parseExpression = buildExpressionParser(mergeConfig(javascriptConfig, {
    functions,
    operators,
    variables,
}));
export const evaluateExpression = (expression, additionalVariables = []) => {
    const parseExpression = buildExpressionParser(mergeConfig(javascriptConfig, {
        functions,
        operators,
        variables: variables.concat(additionalVariables),
    }));
    const parseResult = parseExpression(expression !== null && expression !== void 0 ? expression : "");
    if (parseResult.errors.length > 0)
        return {
            type: "error",
            errors: parseResult.errors,
        };
    try {
        return {
            type: "ok",
            result: evaluate(parseResult.ast, "javascript", {
                functions,
                operators,
                variables: variables.concat(additionalVariables),
            }),
        };
    }
    catch (e) {
        return { type: "ok", result: "#Error!" };
    }
};
//# sourceMappingURL=expressionParser.js.map