import axios from "axios";
/* global wordgrd_editor */


// fetch batches recursively until all matches are collected
export default function fetchBatchContent(content, offset = 0, limit = 100, allMatches = []) {
    return axios.post(
        wordgrd_editor.ajax_url,
        new URLSearchParams({
            action: "wordgrd_scan_content",
            content,
            offset,
            limit,
            _ajax_nonce: wordgrd_editor.wordgrd_nonce,
        })
    )
        .then(resp => {
            if (resp.data?.success) {
                const { matches, has_more, next_offset } = resp.data.data;

                allMatches = allMatches.concat(matches);

                if (has_more) {
                    return fetchBatchContent(content, next_offset, limit, allMatches);
                }
            } else {
                console.error("Batch scan failed:", resp.data);
            }
            return allMatches; // always return accumulated matches
        })
        .catch(err => {
            console.error("Scan request failed", err);
            return allMatches;
        });
}

// main scan function

