import { detectTriggerRemoval } from "./detect-trigger-removal";
import { monitorTriggeredText } from "./monitor-triggered-text";
export const getAppendTransactionHandler = (getCurrentView, getActiveTrigger, resetActiveTrigger, contentConverter) => {
    return (transactions, _oldState, newState) => {
        const activeTrigger = getActiveTrigger();
        if (!activeTrigger) {
            return;
        }
        const triggerRemoved = detectTriggerRemoval(transactions[0], // Pass the first transaction
        activeTrigger, resetActiveTrigger, contentConverter, getCurrentView());
        if (triggerRemoved) {
            resetActiveTrigger();
            return;
        }
        for (const transaction of transactions) {
            if (transaction.docChanged) {
                activeTrigger.position = transaction.mapping.map(activeTrigger.position, 
                // eslint-disable-next-line unicorn/no-array-method-this-argument -- ProseMirror Mapping#map(pos, assoc), not Array#map
                -1);
            }
        }
        monitorTriggeredText(newState.doc, activeTrigger, newState.selection.from, contentConverter, getCurrentView());
    };
};
