"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setupPermissions = exports.uniq = exports.RESULTS = exports.PERMISSIONS = void 0;
const participantSlice_1 = require("../store/participantSlice");
const store_1 = require("../store/store");
const react_native_1 = require("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',
    BLUETOOTH_CONNECT: 'android.permission.BLUETOOTH_CONNECT',
});
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',
});
exports.PERMISSIONS = Object.freeze({
    ANDROID: ANDROID,
    IOS: IOS,
});
exports.RESULTS = Object.freeze({
    UNAVAILABLE: 'unavailable',
    BLOCKED: 'blocked',
    DENIED: 'denied',
    GRANTED: 'granted',
    LIMITED: 'limited',
    PROMPTED: 'prompted',
});
const DyteRNPermissions = react_native_1.NativeModules.RNPermissions || react_native_1.NativeModules.DyteRNPermissions;
function request(permission) {
    return __awaiter(this, void 0, void 0, function* () {
        return DyteRNPermissions.available.includes(permission)
            ? DyteRNPermissions.request(permission)
            : exports.RESULTS.UNAVAILABLE;
    });
}
function uniq(array) {
    return array.filter((item, index) => item != null && array.indexOf(item) === index);
}
exports.uniq = uniq;
function requestMultiplePermissions(permissions) {
    return __awaiter(this, void 0, void 0, function* () {
        if (react_native_1.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] = yield request(permission);
            }
            return output;
        }
        const output = yield DyteRNPermissions.requestMultiplePermissions(permissions);
        return output;
    });
}
const getPlatformEquiPermission = (permission, reverse = false) => {
    const android = {
        camera: exports.PERMISSIONS.ANDROID.CAMERA,
        microphone: exports.PERMISSIONS.ANDROID.RECORD_AUDIO,
        bluetooth: exports.PERMISSIONS.ANDROID.BLUETOOTH_CONNECT,
        externalStorage: exports.PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE,
        readExternalStorage: exports.PERMISSIONS.ANDROID.READ_EXTERNAL_STORAGE,
    };
    const ios = {
        camera: exports.PERMISSIONS.IOS.CAMERA,
        microphone: exports.PERMISSIONS.IOS.MICROPHONE,
        bluetooth: exports.PERMISSIONS.IOS.BLUETOOTH_PERIPHERAL,
        photos: exports.PERMISSIONS.IOS.PHOTO_LIBRARY,
    };
    if (!reverse) {
        if (react_native_1.Platform.OS === 'android') {
            return android[permission];
        }
        return ios[permission];
    }
    if (react_native_1.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: participantSlice_1.permissionEnum.INIT,
        microphone: participantSlice_1.permissionEnum.INIT,
    };
    systemPermissionRequired.forEach((permission) => {
        const normalPermission = getPlatformEquiPermission(permission, true);
        if (status[permission] === exports.RESULTS.GRANTED) {
            permissionState[normalPermission] = participantSlice_1.permissionEnum.ACCEPTED;
        }
        else if (status[permission] !== exports.RESULTS.PROMPTED) {
            permissionState[normalPermission] = participantSlice_1.permissionEnum.DENIED;
        }
    });
    if (permissionState.camera !== participantSlice_1.permissionEnum.INIT &&
        permissionState.microphone !== participantSlice_1.permissionEnum.INIT &&
        timeout)
        clearInterval(timeout);
    store_1.store.dispatch(participantSlice_1.setParticipantPermissions(permissionState));
};
const setupPermissions = (dynamicPermissions) => {
    const systemPermissionRequired = [];
    const permissionsRequired = [
        'bluetooth',
        ...dynamicPermissions,
    ];
    permissionsRequired.forEach((permission) => {
        const systemPermissionEquivalent = getPlatformEquiPermission(permission);
        if (systemPermissionEquivalent) {
            systemPermissionRequired.push(systemPermissionEquivalent);
        }
    });
    if (react_native_1.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);
    });
};
exports.setupPermissions = setupPermissions;
//# sourceMappingURL=permissions.js.map