"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.webMedia = void 0;
const APINotImplementedError_1 = require("../../../../exception/APINotImplementedError");
const DisplayMediaAPINotSupported_1 = require("../../../../exception/DisplayMediaAPINotSupported");
const MediaDeviceAPINotSupported_1 = require("../../../../exception/MediaDeviceAPINotSupported");
function webMedia(window, opt) {
    const media = {
        getUserMedia: (opt) => __awaiter(this, void 0, void 0, function* () {
            if (!navigator || !navigator.mediaDevices) {
                throw new MediaDeviceAPINotSupported_1.MediaDevicesAPINotSupported();
            }
            if (!navigator.mediaDevices.getUserMedia) {
                throw new MediaDeviceAPINotSupported_1.MediaDevicesAPINotSupported();
            }
            try {
                return yield navigator.mediaDevices.getUserMedia(opt);
            }
            catch (err) {
                const { message } = err;
                if (message ===
                    "Failed to execute 'getUserMedia' on 'MediaDevices': At least one of audio and video must be requested") {
                    throw new Error('at least one of audio or video must be requested');
                }
                throw err;
            }
        }),
        getDisplayMedia: (opt) => __awaiter(this, void 0, void 0, function* () {
            if (!navigator.mediaDevices || !navigator.mediaDevices.getDisplayMedia) {
                throw new DisplayMediaAPINotSupported_1.DisplayMediaAPINotSupported();
            }
            try {
                return navigator.mediaDevices.getDisplayMedia(opt);
            }
            catch (err) {
                throw new Error(err.message);
            }
        }),
        enumerateDevices: () => __awaiter(this, void 0, void 0, function* () {
            if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {
                throw new APINotImplementedError_1.APINotSupportedError('Enumerate Media Devices');
            }
            return navigator.mediaDevices
                .enumerateDevices()
                .then((ds) => {
                return ds.map((d) => ({
                    deviceId: d.deviceId,
                    kind: d.kind,
                    groupId: d.groupId,
                    label: d.label,
                }));
            });
        }),
        image: {
            createImageBitmap: function (image, rect, opt) {
                if (rect.x !== undefined &&
                    rect.y !== undefined &&
                    rect.width !== undefined &&
                    rect.height !== undefined) {
                    return window.createImageBitmap(image, rect.x, rect.y, rect.width, rect.height, opt);
                }
                else {
                    return window.createImageBitmap(image, opt);
                }
            },
        },
    };
    return media;
}
exports.webMedia = webMedia;
//# sourceMappingURL=media.js.map