import { setParticipantPermissions, permissionEnum } from '../store/participantSlice';
import { store } from '../store/store';
import { Platform, NativeModules } from 'react-native';
const ANDROID = Object.freeze({
    ACCEPT_HANDOVER: 'android.permission.ACCEPT_HANDOVER',
    ACCESS_BACKGROUND_LOCATION: 'android.permission.ACCESS_BACKGROUND_LOCATION',
    ACCESS_COARSE_LOCATION: 'android.permission.ACCESS_COARSE_LOCATION',
    ACCESS_FINE_LOCATION: 'android.permission.ACCESS_FINE_LOCATION',
    ACTIVITY_RECOGNITION: 'android.permission.ACTIVITY_RECOGNITION',
    ADD_VOICEMAIL: 'com.android.voicemail.permission.ADD_VOICEMAIL',
    ANSWER_PHONE_CALLS: 'android.permission.ANSWER_PHONE_CALLS',
    BODY_SENSORS: 'android.permission.BODY_SENSORS',
    CALL_PHONE: 'android.permission.CALL_PHONE',
    CAMERA: 'android.permission.CAMERA',
    GET_ACCOUNTS: 'android.permission.GET_ACCOUNTS',
    PROCESS_OUTGOING_CALLS: 'android.permission.PROCESS_OUTGOING_CALLS',
    READ_CALENDAR: 'android.permission.READ_CALENDAR',
    READ_CALL_LOG: 'android.permission.READ_CALL_LOG',
    READ_CONTACTS: 'android.permission.READ_CONTACTS',
    READ_EXTERNAL_STORAGE: 'android.permission.READ_EXTERNAL_STORAGE',
    READ_PHONE_NUMBERS: 'android.permission.READ_PHONE_NUMBERS',
    READ_PHONE_STATE: 'android.permission.READ_PHONE_STATE',
    READ_SMS: 'android.permission.READ_SMS',
    RECEIVE_MMS: 'android.permission.RECEIVE_MMS',
    RECEIVE_SMS: 'android.permission.RECEIVE_SMS',
    RECEIVE_WAP_PUSH: 'android.permission.RECEIVE_WAP_PUSH',
    RECORD_AUDIO: 'android.permission.RECORD_AUDIO',
    SEND_SMS: 'android.permission.SEND_SMS',
    USE_SIP: 'android.permission.USE_SIP',
    WRITE_CALENDAR: 'android.permission.WRITE_CALENDAR',
    WRITE_CALL_LOG: 'android.permission.WRITE_CALL_LOG',
    WRITE_CONTACTS: 'android.permission.WRITE_CONTACTS',
    WRITE_EXTERNAL_STORAGE: 'android.permission.WRITE_EXTERNAL_STORAGE',
});
const IOS = Object.freeze({
    APP_TRACKING_TRANSPARENCY: 'ios.permission.APP_TRACKING_TRANSPARENCY',
    BLUETOOTH_PERIPHERAL: 'ios.permission.BLUETOOTH_PERIPHERAL',
    CALENDARS: 'ios.permission.CALENDARS',
    CAMERA: 'ios.permission.CAMERA',
    CONTACTS: 'ios.permission.CONTACTS',
    FACE_ID: 'ios.permission.FACE_ID',
    LOCATION_ALWAYS: 'ios.permission.LOCATION_ALWAYS',
    LOCATION_WHEN_IN_USE: 'ios.permission.LOCATION_WHEN_IN_USE',
    MEDIA_LIBRARY: 'ios.permission.MEDIA_LIBRARY',
    MICROPHONE: 'ios.permission.MICROPHONE',
    MOTION: 'ios.permission.MOTION',
    PHOTO_LIBRARY: 'ios.permission.PHOTO_LIBRARY',
    PHOTO_LIBRARY_ADD_ONLY: 'ios.permission.PHOTO_LIBRARY_ADD_ONLY',
    REMINDERS: 'ios.permission.REMINDERS',
    SIRI: 'ios.permission.SIRI',
    SPEECH_RECOGNITION: 'ios.permission.SPEECH_RECOGNITION',
    STOREKIT: 'ios.permission.STOREKIT',
});
export const PERMISSIONS = Object.freeze({
    ANDROID: ANDROID,
    IOS: IOS,
});
export const RESULTS = Object.freeze({
    UNAVAILABLE: 'unavailable',
    BLOCKED: 'blocked',
    DENIED: 'denied',
    GRANTED: 'granted',
    LIMITED: 'limited',
    PROMPTED: 'prompted'
});
const { DyteRNPermissions } = NativeModules;
async function request(permission) {
    return DyteRNPermissions.available.includes(permission)
        ? DyteRNPermissions.request(permission)
        : RESULTS.UNAVAILABLE;
}
export function uniq(array) {
    return array.filter((item, index) => item != null && array.indexOf(item) === index);
}
async function requestMultiplePermissions(permissions) {
    if (Platform.OS === 'ios') {
        const output = {};
        const dedup = uniq(permissions);
        for (let index = 0; index < dedup.length; index += 1) {
            const permission = dedup[index];
            // eslint-disable-next-line no-await-in-loop
            output[permission] = await request(permission);
        }
        return output;
    }
    const output = await DyteRNPermissions.requestMultiplePermissions(permissions);
    return output;
}
const permissionsRequired = [
    'camera',
    'externalStorage',
    'microphone',
    'bluetooth',
    'photos',
    'readExternalStorage',
];
const getPlatformEquiPermission = (permission, reverse = false) => {
    const android = {
        camera: PERMISSIONS.ANDROID.CAMERA,
        microphone: PERMISSIONS.ANDROID.RECORD_AUDIO,
        externalStorage: PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE,
        readExternalStorage: PERMISSIONS.ANDROID.READ_EXTERNAL_STORAGE,
    };
    const ios = {
        camera: PERMISSIONS.IOS.CAMERA,
        microphone: PERMISSIONS.IOS.MICROPHONE,
        bluetooth: PERMISSIONS.IOS.BLUETOOTH_PERIPHERAL,
        photos: PERMISSIONS.IOS.PHOTO_LIBRARY,
    };
    if (!reverse) {
        if (Platform.OS === 'android') {
            return android[permission];
        }
        return ios[permission];
    }
    if (Platform.OS === 'android') {
        const androidReverse = {};
        Object.keys(android).forEach((key) => {
            androidReverse[android[key]] = key;
        });
        return androidReverse[permission];
    }
    const iosReverse = {};
    Object.keys(ios).forEach((key) => {
        iosReverse[ios[key]] = key;
    });
    return iosReverse[permission];
};
let timeout;
const processPermissionResult = (systemPermissionRequired) => (status) => {
    const permissionState = {
        camera: permissionEnum.INIT,
        microphone: permissionEnum.INIT,
    };
    systemPermissionRequired.forEach((permission) => {
        const normalPermission = getPlatformEquiPermission(permission, true);
        if (status[permission] === RESULTS.GRANTED) {
            permissionState[normalPermission] = permissionEnum.ACCEPTED;
        }
        else if (status[permission] !== RESULTS.PROMPTED) {
            permissionState[normalPermission] = permissionEnum.DENIED;
        }
    });
    if (permissionState.camera !== permissionEnum.INIT
        && permissionState.microphone !== permissionEnum.INIT
        && timeout)
        clearInterval(timeout);
    store.dispatch(setParticipantPermissions(permissionState));
};
export const setupPermissions = () => {
    const systemPermissionRequired = [];
    permissionsRequired.forEach((permission) => {
        const systemPermissionEquivalent = getPlatformEquiPermission(permission);
        if (systemPermissionEquivalent) {
            systemPermissionRequired.push(systemPermissionEquivalent);
        }
    });
    if (Platform.OS === "android") {
        const checkPermLoop = () => {
            DyteRNPermissions.checkMultiplePermissions(systemPermissionRequired)
                .then(processPermissionResult(systemPermissionRequired));
        };
        if (timeout) {
            clearInterval(timeout);
        }
        timeout = setInterval(checkPermLoop, 1000);
    }
    // requesting permission status
    requestMultiplePermissions(systemPermissionRequired)
        .then(processPermissionResult(systemPermissionRequired))
        .catch((error) => {
        console.log('Error Checking Permission:', error);
    });
};
//# sourceMappingURL=permissions.js.map