"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.webMedia = webMedia;
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: async (opt) => {
            if (!navigator || !navigator.mediaDevices) {
                throw new MediaDeviceAPINotSupported_1.MediaDevicesAPINotSupported();
            }
            if (!navigator.mediaDevices.getUserMedia) {
                throw new MediaDeviceAPINotSupported_1.MediaDevicesAPINotSupported();
            }
            try {
                return await 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: async (opt) => {
            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: async () => {
            if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {
                throw new APINotImplementedError_1.APINotSupportedError('Enumerate Media Devices');
            }
            return navigator.mediaDevices.enumerateDevices();
        },
        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;
}
//# sourceMappingURL=media.js.map