import { Environment } from "@marcbachmann/cel-js";
export function createCelEnvironment(args) {
    const env = new Environment({
        unlistedVariablesAreDyn: true,
    });
    const programCache = new Map();
    const callSync = (name, callArgs) => {
        const result = args.engine.call(name, callArgs);
        if (result instanceof Promise) {
            throw new Error(`CEL helper "${name}" returned a Promise; asynchronous helpers are not supported`);
        }
        return result;
    };
    for (const { name } of args.engine.listFunctions()) {
        env.registerFunction(`${name}(): dyn`, () => {
            const result = callSync(name);
            return normalizeCelValue(result);
        });
    }
    const evaluate = (expression, context) => {
        let program = programCache.get(expression);
        if (!program) {
            program = env.parse(expression);
            programCache.set(expression, program);
        }
        const result = program(context);
        return normalizeCelValue(result);
    };
    return { evaluate };
}
export function normalizeCelValue(value) {
    if (typeof value === "bigint") {
        const numeric = Number(value);
        if (Number.isSafeInteger(numeric)) {
            return numeric;
        }
        return value.toString();
    }
    if (Array.isArray(value)) {
        return value.map((entry) => normalizeCelValue(entry));
    }
    if (value &&
        typeof value === "object" &&
        !(value instanceof Uint8Array) &&
        !(value instanceof Date)) {
        const result = {};
        for (const [key, nested] of Object.entries(value)) {
            result[key] = normalizeCelValue(nested);
        }
        return result;
    }
    return value;
}
//# sourceMappingURL=cel-environment.js.map