"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useBluetoothHeadsetDetection = exports.removeListener = exports.addListener = exports.getHeadset = void 0;
const react_native_1 = require("react-native");
const react_1 = require("react");
const BluetoothHeadsetDetectModule = react_native_1.NativeModules.DyteRNBluetoothHeadsetDetect;
const bluetoothHeadsetDetectEmitter = new react_native_1.NativeEventEmitter(BluetoothHeadsetDetectModule);
let device = null;
const listeners = [];
const bluetoothListener = bluetoothHeadsetDetectEmitter.addListener('onChange', ({ devices }) => {
    device = devices.length ? devices[0] : null;
    listeners.forEach((listener) => {
        listener(device);
    });
});
// Events
const getHeadset = () => device;
exports.getHeadset = getHeadset;
const addListener = (listener) => {
    listeners.push(listener);
};
exports.addListener = addListener;
const removeListener = (listener) => {
    const idx = listeners.indexOf(listener);
    if (idx === -1) {
        return;
    }
    listeners.splice(idx, 1);
};
exports.removeListener = removeListener;
// React hook
const useBluetoothHeadsetDetection = () => {
    const [headset, setHeadset] = react_1.useState(null);
    react_1.useEffect(() => {
        setHeadset(device);
        exports.addListener(setHeadset);
        return () => {
            exports.removeListener(setHeadset);
            bluetoothListener.remove();
        };
    }, []);
    return headset;
};
exports.useBluetoothHeadsetDetection = useBluetoothHeadsetDetection;
//# sourceMappingURL=BluetoothHeadsetDetect.js.map