import { assert } from "../../../../core/assert.js";

export class PeriodicConsolePrinter {
    /**
     *
     * @param {number} timeout in seconds
     * @param {function} builderFunction
     * @param {*} [thisArg]
     */
    constructor(timeout = 15, builderFunction, thisArg) {
        assert.isNumber(timeout, "timeout");
        assert.isFunction(builderFunction, "builderFunction");

        this.__timeout = timeout;
        this.__handle = -1;

        this.__method = () => {
            const text = builderFunction.call(thisArg);

            console.warn(text);
        };
    }

    start() {
        if (this.__handle !== -1) {
            // some handle already exists, clear that first
            this.stop();
        }

        this.__handle = setInterval(this.__method, this.__timeout * 1000);
    }

    stop() {
        if (this.__handle === -1) {
            return;
        }

        clearInterval(this.__handle);

        this.__handle = -1;
    }
}
