"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 (!window.navigator || !window.navigator.mediaDevices) {
                throw new MediaDeviceAPINotSupported_1.MediaDevicesAPINotSupported();
            }
            if (!window.navigator.mediaDevices.getUserMedia) {
                throw new MediaDeviceAPINotSupported_1.MediaDevicesAPINotSupported();
            }
            try {
                return await window.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 (!window.navigator.mediaDevices ||
                !window.navigator.mediaDevices.getDisplayMedia) {
                throw new DisplayMediaAPINotSupported_1.DisplayMediaAPINotSupported();
            }
            try {
                return window.navigator.mediaDevices.getDisplayMedia(opt);
            }
            catch (err) {
                throw new Error(err.message);
            }
        },
        enumerateDevices: async () => {
            if (!window.navigator.mediaDevices ||
                !window.navigator.mediaDevices.enumerateDevices) {
                throw new APINotImplementedError_1.APINotSupportedError('Enumerate Media Devices');
            }
            return window.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