"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.webBluetooth = void 0;
const APINotImplementedError_1 = require("../../../../exception/APINotImplementedError");
const sleep_1 = require("../../../../sleep");
function webBluetooth(window, opt) {
    const { navigator } = window;
    const bluetooth = {
        requestDevice: (opt) => __awaiter(this, void 0, void 0, function* () {
            // @ts-ignore
            if (navigator.bluetooth) {
                // show system UI on next tick to prevent possible
                // interference with triggering event propagation
                yield (0, sleep_1.sleep)();
                try {
                    // @ts-ignore
                    const device = yield navigator.bluetooth.requestDevice(opt);
                    return {
                        getServer: () => __awaiter(this, void 0, void 0, function* () {
                            const { gatt } = device;
                            if (gatt) {
                                try {
                                    return (yield device.gatt.connect());
                                }
                                catch (err) {
                                    throw new Error(err.message);
                                }
                            }
                            else {
                                throw new Error('cannot find bluetooth device remote GATT server');
                            }
                        }),
                    };
                }
                catch (err) {
                    const { message } = err;
                    if (message === 'User cancelled the requestDevice() chooser.') {
                        throw new Error('user cancelled chooser');
                    }
                    else if (message ===
                        "Failed to execute 'requestDevice' on 'Bluetooth': Either 'filters' should be present or 'acceptAllDevices' should be true, but not both.") {
                        throw new Error(`either 'filters' should be present or 'acceptAllDevices' should be true, but not both.`);
                    }
                    else {
                        throw err;
                    }
                }
            }
            else {
                throw new APINotImplementedError_1.APINotSupportedError('Bluetooth');
            }
        }),
    };
    return bluetooth;
}
exports.webBluetooth = webBluetooth;
//# sourceMappingURL=bluetooth.js.map