"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.requestBluetoothPermission = exports.promptEnableLocation = exports.managePermission = exports.enableBluetooth = exports.checkBluetoothPermission = void 0;
var _reactNative = require("react-native");
var _reactNativePermissions = require("react-native-permissions");
var _reactNativeBleManager = _interopRequireDefault(require("react-native-ble-manager"));
var _reactNativeDeviceInfo = require("react-native-device-info");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const managePermission = async (permission, enableRequestPermission = false, callBack) => {
  if (permission.success && permission.status === _reactNativePermissions.RESULTS.GRANTED) {
    if (_reactNative.Platform.OS === 'android') {
      const isLocationEnabledFlag = await (0, _reactNativeDeviceInfo.isLocationEnabled)();
      if (!isLocationEnabledFlag) {
        promptEnableLocation();
        return;
      }
      enableBluetooth().then(() => {
        callBack();
      });
    } else {
      await _reactNativeBleManager.default.start({
        showAlert: true
      }).then(() => {
        callBack();
      });
    }
  } else if (enableRequestPermission && !permission.success && permission.status === _reactNativePermissions.RESULTS.DENIED) {
    const requestPermission = await requestBluetoothPermission();
    await managePermission(requestPermission, false, callBack);
  } else if (permission.status === _reactNativePermissions.RESULTS.BLOCKED) {
    bluetoothPermissionMessageAlert();
  }
};
exports.managePermission = managePermission;
const enableBluetooth = async () => {
  try {
    const res = await _reactNativeBleManager.default.enableBluetooth();
    return {
      success: true,
      data: res
    };
  } catch {
    return {
      success: false
    };
  }
};
exports.enableBluetooth = enableBluetooth;
const bluetoothPermissionMessageAlert = () => {
  _reactNative.Alert.alert('Message', 'Please allow Bluetooth permission.', [{
    text: 'Open Settings',
    onPress: () => _reactNative.Linking.openSettings()
  }, {
    text: 'Cancel'
  }]);
};
const evaluatePermissions = (permissionResults, requiredPermissions) => {
  for (const permission of requiredPermissions) {
    if (permissionResults[permission] !== _reactNativePermissions.RESULTS.GRANTED) {
      return {
        success: false,
        status: permissionResults[permission]
      };
    }
  }
  return {
    success: true,
    status: _reactNativePermissions.RESULTS.GRANTED
  };
};
const checkMultiplePermissions = async permissions => {
  return (0, _reactNativePermissions.checkMultiple)(permissions);
};
const requestMultiplePermissions = async permissions => {
  return (0, _reactNativePermissions.requestMultiple)(permissions);
};
const checkBluetoothPermission = async () => {
  return _reactNative.Platform.OS === 'android' ? await checkBluetoothPermissionForAndroid() : await checkBluetoothPermissionForIos();
};
exports.checkBluetoothPermission = checkBluetoothPermission;
const requestBluetoothPermission = async () => {
  return _reactNative.Platform.OS === 'android' ? await requestBluetoothPermissionForAndroid() : await requestBluetoothPermissionForIos();
};
exports.requestBluetoothPermission = requestBluetoothPermission;
const checkBluetoothPermissionForIos = async () => {
  const permissions = [_reactNativePermissions.PERMISSIONS.IOS.BLUETOOTH];
  return evaluatePermissions(await checkMultiplePermissions(permissions), permissions);
};
const checkBluetoothPermissionForAndroid = async () => {
  const androidVersion = Number(_reactNative.Platform.Version);
  const permissions = androidVersion >= 31 ? [_reactNativePermissions.PERMISSIONS.ANDROID.BLUETOOTH_SCAN, _reactNativePermissions.PERMISSIONS.ANDROID.BLUETOOTH_CONNECT, _reactNativePermissions.PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION] : [_reactNativePermissions.PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION, _reactNativePermissions.PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION];
  return evaluatePermissions(await checkMultiplePermissions(permissions), permissions);
};
const requestBluetoothPermissionForIos = async () => {
  const permissions = [_reactNativePermissions.PERMISSIONS.IOS.BLUETOOTH];
  return evaluatePermissions(await requestMultiplePermissions(permissions), permissions);
};
const requestBluetoothPermissionForAndroid = async () => {
  const androidVersion = Number(_reactNative.Platform.Version);
  const permissions = androidVersion >= 31 ? [_reactNativePermissions.PERMISSIONS.ANDROID.BLUETOOTH_SCAN, _reactNativePermissions.PERMISSIONS.ANDROID.BLUETOOTH_CONNECT, _reactNativePermissions.PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION] : [_reactNativePermissions.PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION, _reactNativePermissions.PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION];
  return evaluatePermissions(await requestMultiplePermissions(permissions), permissions);
};
const promptEnableLocation = () => {
  _reactNative.Alert.alert('Location Services Disabled', 'Please enable location services to continue.', [{
    text: 'Open Settings'
  }, {
    text: 'Cancel'
  }]);
};
exports.promptEnableLocation = promptEnableLocation;
//# sourceMappingURL=bluetoothPermissionHelper.js.map