import { LixLogSchema } from "../../log/schema-definition.js";
import { uuidV7Sync } from "../../engine/functions/uuid-v7.js";
import { getTimestampSync } from "../../engine/functions/timestamp.js";
import { insertTransactionState } from "../transaction/insert-transaction-state.js";
// Track if logging is in progress per Lix instance to prevent recursion
const loggingInProgressMap = new WeakMap();
/**
 * Insert a log entry directly using insertTransactionState to avoid recursion
 * when logging from within the virtual table methods.
 *
 * This is a minimal wrapper that can be mocked in tests to control timestamps.
 */
export function insertVTableLog(args) {
    if (loggingInProgressMap.get(args.engine)) {
        return;
    }
    loggingInProgressMap.set(args.engine, true);
    try {
        const id = args.id ?? uuidV7Sync({ engine: args.engine });
        // Insert into transaction state (untracked) to preserve previous behavior.
        // Note: If called outside a vtable write, this may require a later commit to flush.
        insertTransactionState({
            engine: args.engine,
            timestamp: args.timestamp ?? getTimestampSync({ engine: args.engine }),
            data: [
                {
                    entity_id: id,
                    schema_key: LixLogSchema["x-lix-key"],
                    file_id: "lix",
                    plugin_key: "lix_sdk",
                    snapshot_content: JSON.stringify({
                        id,
                        key: args.key,
                        message: args.message ?? null,
                        payload: args.payload ?? null,
                        level: args.level,
                    }),
                    schema_version: LixLogSchema["x-lix-version"],
                    version_id: "global",
                    untracked: true,
                },
            ],
        });
    }
    finally {
        loggingInProgressMap.set(args.engine, false);
    }
}
//# sourceMappingURL=insert-vtable-log.js.map