import { DeviceEventEmitter, NativeEventEmitter, NativeModules, Platform } from 'react-native';
let eventSubscription = null;
const LINKING_ERROR = `The package 'react-native-mytatva-rn-sdk' doesn't seem to be linked. Make sure: \n\n` + Platform.select({
  ios: "- You have run 'pod install'\n",
  default: ''
}) + '- You rebuilt the app after installing the package\n' + '- You are not using Expo Go\n';
const cgmLib = NativeModules.CgmTrackyLib ? NativeModules.CgmTrackyLib : new Proxy({}, {
  get() {
    throw new Error(LINKING_ERROR);
  }
});
function initializeCGMEventListener(callback) {
  const iosEventEmitter = new NativeEventEmitter(cgmLib);
  if (eventSubscription) {
    eventSubscription.remove();
  }
  if (Platform.OS === 'android') {
    eventSubscription = DeviceEventEmitter.addListener('cgmDeviceEvent', eventData => {
      const {
        status
      } = eventData;
      if (status === 'WARM_PERIOD_STARTED') {
        callback(eventData);
      } else {
        callback(eventData);
      }
    });
  } else if (Platform.OS === 'ios' && iosEventEmitter) {
    eventSubscription = iosEventEmitter.addListener('cgmDeviceEvent', eventData => {
      const {
        status
      } = eventData;
      if (status === 'WARM_PERIOD_STARTED') {
        callback(eventData);
      } else {
        callback(eventData);
      }
    });
  }
}
function removeCGMEventListener() {
  if (eventSubscription) {
    eventSubscription.remove();
    eventSubscription = null;
  }
}
const startCGM = async (token, envType, userData) => {
  console.log('startCGM===');
  try {
    const result = await cgmLib.startCgmTracky(token, envType, userData);
    console.log(result);
  } catch (error) {
    console.error(error);
  }
};
const reconnectCGM = async (token, envType, userData) => {
  console.log('reconnectCGM====');
  try {
    const result = await cgmLib.reconnectCgmTracky(token, envType, userData);
    console.log(result);
  } catch (error) {
    console.error(error);
  }
};
const observeAllGlucoseDataHandler = async (token, isCleanData, envType, userData) => {
  console.log('observeAllGlucoseDataHandler====');
  try {
    if (Platform.OS === 'android') {
      const result = await cgmLib.observeAllGlucoseData(token, isCleanData, envType, userData);
      const deviceStatus = await cgmLib.observeDeviceStatus(token, envType);
      console.log(result);
      console.log(deviceStatus);
    } else if (Platform.OS === 'ios') {
      const result = await cgmLib.observeAllGlucoseData(token, isCleanData, envType);
      console.log(result);
    }
  } catch (error) {
    console.log('error====', error);
    console.error(error);
  }
};
const helpCGM = async (token, envType) => {
  console.log('helpCGM====');
  try {
    const result = await cgmLib.openHelpSupport();
    console.log(result);
  } catch (error) {
    console.error(error);
  }
};
const observeTransmitterUnbindStatusHandler = async (token, apiResponse, patientId, envType, userData) => {
  console.log('observeTransmitterUnbindStatusHandler====');
  try {
    const result = Platform.OS === 'android' ? await cgmLib.observeTransmitterUnbindStatus(token, apiResponse, patientId, envType, userData) : await cgmLib.observeTransmitterUnbindStatus(token, apiResponse, patientId, envType);
    console.log(result);
  } catch (error) {
    console.error(error);
  }
};
const observeResetLogoutHandler = async () => {
  try {
    const result = await cgmLib.resetCgmState();
    console.log(result);
  } catch (error) {
    console.error(error);
  }
};
const getSqliteDBPath = async () => {
  const result = await cgmLib.getTrackLibDbPath();
  return result;
};
const stopCGM = async () => {
  // Implementation
};

/**
 * Get all CGM log file paths
 * Returns array of absolute file paths to CGM log files (CSV format)
 * Use react-native-fs or similar in your main app to read these files
 *
 * @returns Promise<string[]> - Array of absolute file paths
 *
 * @example
 * const logPaths = await getCgmLogFilePaths();
 * // Android: ['/storage/emulated/0/Android/data/.../Documents/12345_POCTech.csv']
 * // iOS: ['/var/mobile/Containers/Data/Application/.../Documents/runLog/1_SensorID.csv']
 */
const getCgmLogFilePaths = async () => {
  try {
    const result = await cgmLib.getCgmLogFiles();
    console.log(`CGM Log Files: ${result.length} files found`);
    return result;
  } catch (error) {
    console.error('Error getting CGM log file paths:', error);
    throw error;
  }
};
export { startCGM, stopCGM, initializeCGMEventListener, removeCGMEventListener, observeAllGlucoseDataHandler, reconnectCGM, helpCGM, observeTransmitterUnbindStatusHandler, observeResetLogoutHandler, getSqliteDBPath, getCgmLogFilePaths };
//# sourceMappingURL=CGMConnect.js.map