(function(n,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(n=typeof globalThis<"u"?globalThis:n||self,a(n.MediaMock={}))})(this,function(n){"use strict";var B=Object.defineProperty;var k=(n,a,c)=>a in n?B(n,a,{enumerable:!0,configurable:!0,writable:!0,value:c}):n[a]=c;var r=(n,a,c)=>k(n,typeof a!="symbol"?a+"":a,c);function a(d,e,i){const t=d[e];return Object.defineProperty(d,e,{writable:!0,configurable:!0,value:i}),()=>{Object.defineProperty(d,e,{writable:!0,configurable:!0,value:t})}}function c({deviceId:d,groupId:e,kind:i,label:t,mockCapabilities:o={width:{min:1,max:1280},height:{min:1,max:720}}}){return{deviceId:d,groupId:e,kind:i,label:t,getCapabilities:()=>o,toJSON(){return{deviceId:`${this.deviceId}`,kind:this.kind,label:`${this.label}`,groupId:`${this.groupId}`}}}}const f={aspectRatio:!0,deviceId:!0,displaySurface:!0,echoCancellation:!0,facingMode:!0,frameRate:!0,groupId:!1,height:!0,sampleRate:!1,sampleSize:!1,torch:!1,volume:!0,whiteBalanceMode:!0,width:!0,zoom:!0},g={"iPhone 12":{videoResolutions:[{width:1920,height:1080},{width:1280,height:720},{width:640,height:480}],mediaDeviceInfo:[c({deviceId:"A7FB77364106629BF38E043E6B000EE5FD680B9B",kind:"videoinput",label:"Front Camera",groupId:"C1B048C04520A18C3611DC837450814245482489",mockCapabilities:{aspectRatio:{max:4032,min:.00033068783068783067},deviceId:"1A100C35A33042B643BE0438DBBF9FDC95AF1913",facingMode:["user"],frameRate:{max:60,min:1},groupId:"C1B048C04520A18C3611DC837450814245482489",height:{max:3024,min:1},whiteBalanceMode:["manual","continuous"],width:{max:4032,min:1},zoom:{max:4,min:1}}}),c({deviceId:"9729B396E0C2B460BC7B69C0E368EB0B605058A9",kind:"videoinput",label:"Back Dual Wide Camera",groupId:"A1F2417053FF79495E7D01AF37A6C4461CE0C060",mockCapabilities:{aspectRatio:{max:4032,min:.00033068783068783067},deviceId:"D87C414E22C375BB0697DCB83A24D97BD520624D",facingMode:["environment"],focusDistance:{min:.12},frameRate:{max:60,min:1},groupId:"A1F2417053FF79495E7D01AF37A6C4461CE0C060",height:{max:3024,min:1},torch:!0,whiteBalanceMode:["manual","continuous"],width:{max:4032,min:1},zoom:{max:2,min:.5}}}),c({deviceId:"0B74C1149038CA5235F6C2325E53AE22AA920379",kind:"videoinput",label:"Back Ultra Wide Camera",groupId:"B402A3862F28FB8D54BDF33BD7D41874FE175517",mockCapabilities:{aspectRatio:{max:4032,min:.00033068783068783067},deviceId:"BE00A990BEDE2D324EB0AD51F567EE4ADC24D9B0",facingMode:["environment"],focusDistance:{min:.12},frameRate:{max:60,min:1},groupId:"B402A3862F28FB8D54BDF33BD7D41874FE175517",height:{max:3024,min:1},torch:!0,whiteBalanceMode:["manual","continuous"],width:{max:4032,min:1},zoom:{max:4,min:1}}}),c({deviceId:"C92FE814FCB4F2F856CDCBFD1C555429774DD0E2",kind:"videoinput",label:"Back Camera",groupId:"14122C2CE97B69A84360822AB87E8206C32B5BD8",mockCapabilities:{aspectRatio:{max:4032,min:.00033068783068783067},deviceId:"D13A012C1D5C9F9899B40BDA0790184EE57FD282",facingMode:["environment"],focusDistance:{min:.12},frameRate:{max:60,min:1},groupId:"14122C2CE97B69A84360822AB87E8206C32B5BD8",height:{max:3024,min:1},torch:!0,whiteBalanceMode:["manual","continuous"],width:{max:4032,min:1},zoom:{max:4,min:1}}})],supportedConstraints:f},"Samsung Galaxy M53":{videoResolutions:[{width:1920,height:1080},{width:1280,height:720},{width:640,height:480}],mediaDeviceInfo:[c({deviceId:"87fcafb209f5ff2a6d7c8a5d14afe1c9aba9f209330e93933e545e40b102b35f",groupId:"f70f63d2f4eea57dafe6c6b60833aa69a02f06bb0a6878cb277fb4d70daa9020",kind:"videoinput",label:"camera2 1, facing front",mockCapabilities:{aspectRatio:{max:2400,min:.0009191176470588235},deviceId:"87fcafb209f5ff2a6d7c8a5d14afe1c9aba9f209330e93933e545e40b102b35f",facingMode:["user"],frameRate:{max:30,min:1},groupId:"f70f63d2f4eea57dafe6c6b60833aa69a02f06bb0a6878cb277fb4d70daa9020",height:{max:1088,min:1},resizeMode:["none","crop-and-scale"],width:{max:2400,min:1}}}),c({deviceId:"81cb5898aebd672ef65d04ed1bc7b00c704f2b6aa94200bc5556ff02c89ea14d",groupId:"7300f91d6cb037dcaa6fe16abb59f4e9f92fb471e2280ff0e313e07c49cb536c",kind:"videoinput",label:"camera2 2, facing front",mockCapabilities:{aspectRatio:{max:2400,min:.0009191176470588235},deviceId:"81cb5898aebd672ef65d04ed1bc7b00c704f2b6aa94200bc5556ff02c89ea14d",facingMode:["user"],frameRate:{max:30,min:1},groupId:"7300f91d6cb037dcaa6fe16abb59f4e9f92fb471e2280ff0e313e07c49cb536c",height:{max:1088,min:1},resizeMode:["none","crop-and-scale"],width:{max:2400,min:1}}}),c({deviceId:"99be6eecad8c050052df5dbb08b0460d2715b0a3b18fc5c7f08d6073d312ca34",groupId:"40f44b864c99ab042a21cf87df882d0ef5c7f88f7cbfcee74cefc1e393b8616b",kind:"videoinput",label:"camera2 0, facing back",mockCapabilities:{aspectRatio:{max:3840,min:.000462962962962963},deviceId:"99be6eecad8c050052df5dbb08b0460d2715b0a3b18fc5c7f08d6073d312ca34",facingMode:["environment"],frameRate:{max:30,min:1},groupId:"40f44b864c99ab042a21cf87df882d0ef5c7f88f7cbfcee74cefc1e393b8616b",height:{max:2160,min:1},resizeMode:["none","crop-and-scale"],width:{max:3840,min:1}}})],supportedConstraints:f},"Mac Desktop":{videoResolutions:[{width:1920,height:1080},{width:1280,height:720},{width:640,height:480}],mediaDeviceInfo:[c({deviceId:"e91a0ba82ba051029709163c442d340a3919dfabd",groupId:"7ce19c839ef9ab1a4cba8d4dd4d3c1bbbf3ad",kind:"videoinput",label:"FaceTime HD Camera (2C0E:82E3)",mockCapabilities:{aspectRatio:{max:1920,min:.0005208333333333333},backgroundBlur:[!1],deviceId:"370CF6B3449B7B73599E8DAEEE75FB41788A0712",frameRate:{max:30,min:1},groupId:"F2EFF7249C97B5531FF959C8F977138341165F6B",height:{max:1920,min:1},width:{max:1920,min:1}}})],supportedConstraints:f}};async function b(d){const e=new Image;e.src=d;try{return await e.decode(),e}catch{throw new Error(`Failed to load image: ${d}`)}}function p(){return{mediaDevices:{getUserMedia:!0,getSupportedConstraints:!0,enumerateDevices:!0}}}function I(d){var t;const e=["mp4","webm","ogg","mov","avi","mkv","flv","wmv","m4v","3gp","mpg","mpeg","asf","rm","vob"],i=(t=d.split(".").pop())==null?void 0:t.toLowerCase();return e.includes(i??"")}function C(d){return new Promise(e=>{d.addEventListener("loadeddata",async()=>{try{await d.play(),e()}catch(i){console.error(i),e()}}),d.load()})}class l{constructor(){r(this,"settings",{mediaURL:"./assets/640x480-sample.png",device:g["iPhone 12"],constraints:g["iPhone 12"].supportedConstraints});r(this,"mediaMockImageId","media-mock-image");r(this,"mediaMockCanvasId","media-mock-canvas");r(this,"currentImage",new Image);r(this,"mapUnmockFunction",new Map);r(this,"currentStream");r(this,"intervalId",null);r(this,"debug",!1);r(this,"canvas");r(this,"ctx")}setMediaURL(e){return this.settings.mediaURL=e,this}addMockDevice(e){return this.settings.device.mediaDeviceInfo.push(e),this.triggerDeviceChange(),this}removeMockDevice(e){return this.settings.device.mediaDeviceInfo=this.settings.device.mediaDeviceInfo.filter(i=>i.deviceId!==e),this.triggerDeviceChange(),this}triggerDeviceChange(){navigator.mediaDevices.dispatchEvent(new Event("devicechange"))}enableDebugMode(){return this.debug=!0,this.canvas!=null&&document.querySelector(this.mediaMockCanvasId)==null&&(this.canvas.style.border="10px solid red",document.body.append(this.canvas)),this.currentImage!=null&&document.querySelector(this.mediaMockImageId)==null&&(this.currentImage.style.border="10px solid red",document.body.append(this.currentImage)),this}disableDebugMode(){this.debug=!1;const e=document.getElementById(this.mediaMockCanvasId),i=document.getElementById(this.mediaMockImageId);return e==null||e.remove(),i==null||i.remove(),this}mock(e,i=p()){if(this.settings.device=e,typeof navigator.mediaDevices>"u"){class t extends EventTarget{}a(navigator,"mediaDevices",new t)}if(i!=null&&i.mediaDevices.getUserMedia){const t=a(navigator.mediaDevices,"getUserMedia",o=>this.getMockStream(o));this.mapUnmockFunction.set("getUserMedia",t)}if(i!=null&&i.mediaDevices.getSupportedConstraints){const t=a(navigator.mediaDevices,"getSupportedConstraints",()=>this.settings.constraints);this.mapUnmockFunction.set("getSupportedConstraints",t)}if(i!=null&&i.mediaDevices.enumerateDevices){const t=a(navigator.mediaDevices,"enumerateDevices",async()=>this.settings.device.mediaDeviceInfo);this.mapUnmockFunction.set("enumerateDevices",t)}return this}unmock(){return this.stopMockStream(),this.disableDebugMode(),this.mapUnmockFunction.forEach(e=>e()),this.mapUnmockFunction.clear(),this}stopMockStream(){var e,i,t,o;this.intervalId&&clearInterval(this.intervalId),(i=(e=this.currentStream)==null?void 0:e.getVideoTracks())==null||i.forEach(m=>m.stop()),(o=(t=this.currentStream)==null?void 0:t.stop)==null||o.call(t)}async getMockStream(e){const{width:i,height:t}=this.getResolution(e,this.settings.device),o=I(this.settings.mediaURL),m=this.getFPSFromConstraints(e);if(this.canvas=document.createElement("canvas"),this.canvas.id=this.mediaMockCanvasId,this.canvas.width=i,this.canvas.height=t,this.ctx=this.canvas.getContext("2d"),o){const s=document.createElement("video");s.addEventListener("error",()=>{console.error("Failed to load video source. Ensure the format is supported and the URL is valid.")}),s.src=this.settings.mediaURL,s.muted=!0,s.playsInline=!0,s.loop=!0,s.autoplay=!0,s.hidden=!0,s.crossOrigin="anonymous",await C(s),this.intervalId=setInterval(()=>{var h,u;(h=this.ctx)==null||h.clearRect(0,0,i,t),(u=this.ctx)==null||u.drawImage(s,0,0,i,t)},1e3/m)}else this.currentImage=await b(this.settings.mediaURL),this.currentImage.id=this.mediaMockImageId,this.intervalId=setInterval(()=>{var u,v;const s=(i-this.currentImage.width)/2,h=(t-this.currentImage.height)/2;(u=this.ctx)==null||u.clearRect(0,0,i,t),(v=this.ctx)==null||v.drawImage(this.currentImage,s,h)},1e3/m);return this.debug&&this.enableDebugMode(),this.currentStream=this.canvas.captureStream(m),this.currentStream}getFPSFromConstraints(e){return typeof e.video=="object"&&e.video.frameRate?typeof e.video.frameRate=="number"?e.video.frameRate:e.video.frameRate.ideal||30:30}getResolution(e,i){const t=e.video.width||{ideal:640},o=e.video.height||{ideal:480};return i.videoResolutions.find(m=>(typeof t=="number"?m.width===t:m.width===t.ideal)&&(typeof o=="number"?m.height===o:m.height===o.ideal))||i.videoResolutions[0]}}const D=new l;n.MediaMock=D,n.MediaMockClass=l,n.createMediaDeviceInfo=c,n.devices=g,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})});