const timers = [];
export const newTimer = (info) => {
    let id = new Date().getTime().toString();
    const timer = {
        id: id,
        state: "idle",
        time: 0,
        ref: undefined,
        config: info
    };
    timers.push(timer);
    return { id, getTimer, startTimer, stopTimer, pauseTimer, resumeTimer, resetTimer };
};
const getTimer = (id) => {
    let timer = timers.find((item) => item.id == id);
    return timer;
};
const startTimer = (id) => {
    let timer = timers.find((item) => item.id == id);
    if (timer) {
        timer.time = 0;
        timer.state = "running";
        timer.ref = setInterval((id) => {
            let timer = timers.find((item) => item.id == id);
            if (timer && timer.state == "running") {
                timer.config.onUpdate ? timer.config.onUpdate(timer) : null;
                timer.time = timer.time + 10;
            }
        }, 10, id);
    }
    return timer;
};
const stopTimer = (id) => {
    let timer = timers.find((item) => item.id == id);
    if (timer) {
        clearInterval(timer.ref);
        timer.ref = undefined;
        timer.state = "idle";
    }
    return timer;
};
const pauseTimer = (id) => {
    let timer = timers.find((item) => item.id == id);
    if (timer) {
        timer.state = "idle";
    }
    return timer;
};
const resumeTimer = (id) => {
    let timer = timers.find((item) => item.id == id);
    if (timer) {
        timer.state = "running";
    }
    return timer;
};
const resetTimer = (id) => {
    let timer = timers.find((item) => item.id == id);
    if (timer) {
        timer.state = "running";
        timer.time = 0;
    }
    return timer;
};
//# sourceMappingURL=timer.js.map