// @ts-nocheck
import { Platform, Vibration, } from 'react-native';
'use strict';
const _InCallManager = require('react-native').NativeModules.InCallManager;
class InCallManager {
    constructor() {
        this.vibrate = false;
        this.recordPermission = 'unknow';
        this.cameraPermission = 'unknow';
        this.audioUriMap = {
            ringtone: { _BUNDLE_: null, _DEFAULT_: null },
            ringback: { _BUNDLE_: null, _DEFAULT_: null },
            busytone: { _BUNDLE_: null, _DEFAULT_: null },
        };
        this.checkRecordPermission = this.checkRecordPermission.bind(this);
        this.requestRecordPermission = this.requestRecordPermission.bind(this);
        this.checkCameraPermission = this.checkCameraPermission.bind(this);
        this.requestCameraPermission = this.requestCameraPermission.bind(this);
        this.checkRecordPermission();
        this.checkCameraPermission();
    }
    start(setup) {
        setup = (setup === undefined) ? {} : setup;
        const auto = setup.auto !== false;
        const media = (setup.media === 'video') ? 'video' : 'audio';
        const ringback = (setup.ringback) ? (typeof setup.ringback === 'string') ? setup.ringback : '' : '';
        _InCallManager.start(media, auto, ringback);
    }
    stop(setup) {
        setup = (setup === undefined) ? {} : setup;
        const busytone = (setup.busytone) ? (typeof setup.busytone === 'string') ? setup.busytone : '' : '';
        _InCallManager.stop(busytone);
    }
    turnScreenOff() {
        _InCallManager.turnScreenOff();
    }
    turnScreenOn() {
        _InCallManager.turnScreenOn();
    }
    async getIsWiredHeadsetPluggedIn() {
        try {
            return await _InCallManager.getIsWiredHeadsetPluggedIn();
        }
        catch (e) {
            throw new Error('Incall Manager Error getting wired headset');
        }
    }
    setFlashOn(enable, brightness) {
        if (Platform.OS === 'ios') {
            enable = (enable === true);
            brightness = (typeof brightness === 'number') ? brightness : 0;
            _InCallManager.setFlashOn(enable, brightness);
        }
        else {
            console.log("Android doesn't support setFlashOn(enable, brightness)");
        }
    }
    setKeepScreenOn(enable) {
        enable = (enable === true);
        _InCallManager.setKeepScreenOn(enable);
    }
    setSpeakerphoneOn(enable) {
        enable = (enable === true);
        _InCallManager.setSpeakerphoneOn(enable);
    }
    setForceSpeakerphoneOn(_flag) {
        const flag = (typeof _flag === 'boolean') ? (_flag) ? 1 : -1 : 0;
        _InCallManager.setForceSpeakerphoneOn(flag);
    }
    setMicrophoneMute(enable) {
        enable = (enable === true);
        _InCallManager.setMicrophoneMute(enable);
    }
    startRingtone(ringtone, vibrate_pattern, ios_category, seconds) {
        ringtone = (typeof ringtone === 'string') ? ringtone : '_DEFAULT_';
        this.vibrate = !!(Array.isArray(vibrate_pattern));
        ios_category = (ios_category === 'playback') ? 'playback' : 'default';
        seconds = (typeof seconds === 'number' && seconds > 0) ? parseInt(seconds) : -1; // --- android only, default looping
        if (Platform.OS === 'android') {
            _InCallManager.startRingtone(ringtone, seconds);
        }
        else {
            _InCallManager.startRingtone(ringtone, ios_category);
        }
        // --- should not use repeat, it may cause infinite loop in some cases.
        if (this.vibrate) {
            Vibration.vibrate(vibrate_pattern, false); // --- ios needs RN 0.34 to support vibration pattern
        }
    }
    stopRingtone() {
        if (this.vibrate) {
            Vibration.cancel();
        }
        _InCallManager.stopRingtone();
    }
    startRingback(ringback) {
        ringback = (typeof ringback === 'string') ? ringback : '_DTMF_';
        _InCallManager.startRingback(ringback);
    }
    stopRingback() {
        _InCallManager.stopRingback();
    }
    async checkRecordPermission() {
        // --- on android which api < 23, it will always be "granted"
        const result = await _InCallManager.checkRecordPermission();
        this.recordPermission = result;
        return result;
    }
    async requestRecordPermission() {
        // --- on android which api < 23, it will always be "granted"
        const result = await _InCallManager.requestRecordPermission();
        this.recordPermission = result;
        return result;
    }
    async checkCameraPermission() {
        // --- on android which api < 23, it will always be "granted"
        const result = await _InCallManager.checkCameraPermission();
        this.cameraPermission = result;
        return result;
    }
    async requestCameraPermission() {
        // --- on android which api < 23, it will always be "granted"
        const result = await _InCallManager.requestCameraPermission();
        this.cameraPermission = result;
        return result;
    }
    pokeScreen(_timeout) {
        if (Platform.OS === 'android') {
            const timeout = (typeof _timeout === 'number' && _timeout > 0) ? _timeout : 3000; // --- default 3000 ms
            _InCallManager.pokeScreen(timeout);
        }
        else {
            console.log("ios doesn't support pokeScreen()");
        }
    }
    async getAudioUri(audioType, fileType) {
        if (typeof this.audioUriMap[audioType] === 'undefined') {
            return null;
        }
        if (this.audioUriMap[audioType][fileType]) {
            return this.audioUriMap[audioType][fileType];
        }
        try {
            const result = await _InCallManager.getAudioUriJS(audioType, fileType);
            if (typeof result === 'string' && result.length > 0) {
                this.audioUriMap[audioType][fileType] = result;
                return result;
            }
            return null;
        }
        catch (err) {
            return null;
        }
    }
    async chooseAudioRoute(route) {
        const result = await _InCallManager.chooseAudioRoute(route);
        return result;
    }
}
export default new InCallManager();
//# sourceMappingURL=RNInCallManager.js.map