"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const react_native_1 = require("react-native");
const { DyteRNBackgroundTimer } = react_native_1.NativeModules;
const Emitter = new react_native_1.NativeEventEmitter(DyteRNBackgroundTimer);
class BackgroundTimer {
    constructor() {
        this.uniqueId = 0;
        this.callbacks = {};
        Emitter.addListener('backgroundTimer.timeout', (id) => {
            if (this.callbacks[id]) {
                const callbackById = this.callbacks[id];
                const { callback } = callbackById;
                if (!this.callbacks[id].interval) {
                    delete this.callbacks[id];
                }
                else {
                    DyteRNBackgroundTimer.backgroundTimerSetTimeout(id, this.callbacks[id].timeout);
                }
                callback();
            }
        });
    }
    // Original API
    start(delay = 0) {
        return DyteRNBackgroundTimer.start(delay);
    }
    stop() {
        return DyteRNBackgroundTimer.stop();
    }
    runBackgroundTimer(callback, delay) {
        const EventEmitter = react_native_1.Platform.select({
            ios: () => react_native_1.NativeAppEventEmitter,
            android: () => react_native_1.DeviceEventEmitter,
        })();
        this.start(0);
        this.backgroundListener = EventEmitter.addListener('backgroundTimer', () => {
            this.backgroundListener.remove();
            this.backgroundClockMethod(callback, delay);
        });
    }
    backgroundClockMethod(callback, delay) {
        this.backgroundTimer = this.setTimeout(() => {
            callback();
            this.backgroundClockMethod(callback, delay);
        }, delay);
    }
    stopBackgroundTimer() {
        this.stop();
        this.clearTimeout(this.backgroundTimer);
    }
    // New API, allowing for multiple timers
    setTimeout(callback, timeout) {
        this.uniqueId += 1;
        const timeoutId = this.uniqueId;
        this.callbacks[timeoutId] = {
            callback,
            interval: false,
            timeout,
        };
        DyteRNBackgroundTimer.setTimeout(timeoutId, timeout);
        return timeoutId;
    }
    clearTimeout(timeoutId) {
        if (this.callbacks[timeoutId]) {
            delete this.callbacks[timeoutId];
            // DyteRNBackgroundTimer.clearTimeout(timeoutId);
        }
    }
    setInterval(callback, timeout) {
        this.uniqueId += 1;
        const intervalId = this.uniqueId;
        this.callbacks[intervalId] = {
            callback,
            interval: true,
            timeout,
        };
        DyteRNBackgroundTimer.backgroundTimerSetTimeout(intervalId, timeout);
        return intervalId;
    }
    clearInterval(intervalId) {
        if (this.callbacks[intervalId]) {
            delete this.callbacks[intervalId];
            // DyteRNBackgroundTimer.clearTimeout(intervalId);
        }
    }
}
exports.default = new BackgroundTimer();
//# sourceMappingURL=BackgroundTimer.js.map