import { delay } from "../../../../globalUtils/delay.js";
import { createLog } from "../../../logger/logger.js";
import { tiImport } from "./tiFullFlow/tiImport.js";

// add a running check so we cant flag it twice
export let tiExportRunning = false;

export const runTiImport = async () => {
    let finished = false;
    let test: any;
    tiExportRunning = true;
    do {
        createLog("info", "ti", "notify", "processing new data");
        // code block to be executed
        test = await tiImport();
        createLog(
            "info",
            "ti",
            "notify",
            `Still more to process? ${test.success ? "No" : "Yes"}`
        );
        if (test.success) {
            finished = true;
        }

        if (!test.success) {
            //errors are handled in the tiImport function
            tiExportRunning = false;
        }
        await delay(1000 * 5);
    } while (!finished);
    tiExportRunning = false;
    return { success: true, message: "Finished processing all data." };
};

export default runTiImport;
