/**
 * Minified by jsDelivr using Terser v5.19.2.
 * Original file: /npm/@georapbox/capture-photo-element@5.0.0/dist/capture-photo.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
/*!
 * @georapbox/capture-photo-element
 * A custom element that implements the MediaDevices.getUserMedia() method of the MediaDevices interface to capture a photo in the browser.
 *
 * @version 5.0.0
 * @homepage https://github.com/georapbox/capture-photo-element#readme
 * @author George Raptis <georapbox@gmail.com>
 * @license MIT
 */
var c=(t,e,i)=>(Number.isNaN(e)&&(e=0),Number.isNaN(i)&&(i=0),Math.min(Math.max(t,Math.min(e,i)),Math.max(e,i))),a="capture-photo",d="\n  :host { display: block; box-sizing: border-box; }\n  :host *, :host *::before, :host *::after { box-sizing: inherit;}\n  :host([hidden]), [hidden], ::slotted([hidden]) { display: none; }\n  video { display: block; }\n  #output:empty { display: none; }\n",u=document.createElement("template");u.innerHTML=`\n  <style>${d}</style>\n  <video part="video" playsinline></video>\n  <canvas hidden></canvas>\n  <div part="actions-container">\n    <slot name="capture-button">\n      <button part="capture-button" type="button">\n        <slot name="capture-button-content">Capture photo</slot>\n      </button>\n    </slot>\n    <slot name="actions"></slot>\n  </div>\n  <slot></slot>\n  <div part="output-container" id="output"></div>\n`;var l=class t extends HTMLElement{#t={};#e=null;#i=null;#s=null;#a=null;#o=null;#n=null;constructor(){super(),this.#t=this.getSupportedConstraints(),this.shadowRoot||this.attachShadow({mode:"open"}).appendChild(u.content.cloneNode(!0))}static get observedAttributes(){return["no-image","pan","tilt","zoom","torch"]}attributeChangedCallback(t,e,i){if(!this.isConnected)return;let s=this.getTrackCapabilities();if("no-image"===t&&e!==i&&this.#r(),"pan"===t&&e!==i&&"pan"in this.#t){let t=!!("pan"in s&&s.pan?.min&&s.pan?.max)&&(this.pan>=s.pan.min&&this.pan<=s.pan.max);"number"==typeof this.pan&&t&&this.#h("pan",this.pan)}if("tilt"===t&&e!==i&&"tilt"in this.#t){let t=!!("tilt"in s&&s.tilt?.min&&s.tilt?.max)&&(this.tilt>=s.tilt.min&&this.tilt<=s.tilt.max);"number"==typeof this.tilt&&t&&this.#h("tilt",this.tilt)}if("zoom"===t&&e!==i&&"zoom"in this.#t){let t=!!("zoom"in s&&s.zoom?.min&&s.zoom?.max)&&(this.zoom>=s.zoom.min&&this.zoom<=s.zoom.max);"number"==typeof this.zoom&&t&&this.#h("zoom",this.zoom)}"torch"===t&&e!==i&&"torch"in this.#t&&this.#h("torch",this.torch)}async connectedCallback(){if(this.#l("autoPlay"),this.#l("noImage"),this.#l("facingMode"),this.#l("cameraResolution"),this.#l("pan"),this.#l("tilt"),this.#l("zoom"),this.#l("torch"),this.#l("calculateFileSize"),this.#i=this.shadowRoot?.querySelector("canvas")||null,this.#s=this.shadowRoot?.getElementById("output")||null,this.#a=this.shadowRoot?.querySelector("video")||null,this.#o=this.shadowRoot?.querySelector('slot[name="capture-button"]')||null,this.#n=this.#u(),this.#a?.addEventListener("loadedmetadata",this.#d),this.#o?.addEventListener("slotchange",this.#c),this.#n?.addEventListener("click",this.#m),!t.isSupported())return this.dispatchEvent(new CustomEvent(`${a}:error`,{bubbles:!0,composed:!0,detail:{error:{name:"NotSupportedError",message:"Not supported"}}}));this.autoPlay&&this.startVideoStream()}disconnectedCallback(){this.stopVideoStream(),this.#n?.removeEventListener("click",this.#m),this.#a?.removeEventListener("loadedmetadata",this.#d),this.#o?.removeEventListener("slotchange",this.#c)}get autoPlay(){return this.hasAttribute("auto-play")}set autoPlay(t){this.toggleAttribute("auto-play",!!t)}get noImage(){return this.hasAttribute("no-image")}set noImage(t){this.toggleAttribute("no-image",!!t)}get facingMode(){let t=this.getAttribute("facing-mode");return"user"!==t?"environment":t}set facingMode(t){this.setAttribute("facing-mode",t)}get cameraResolution(){return this.getAttribute("camera-resolution")||""}set cameraResolution(t){this.setAttribute("camera-resolution",t)}get pan(){return Number(this.getAttribute("pan"))||0}set pan(t){this.setAttribute("pan",null!=t?t.toString():t)}get tilt(){return Number(this.getAttribute("tilt"))||0}set tilt(t){this.setAttribute("tilt",null!=t?t.toString():t)}get zoom(){return Number(this.getAttribute("zoom"))||1}set zoom(t){this.setAttribute("zoom",null!=t?t.toString():t)}get torch(){return this.hasAttribute("torch")}set torch(t){this.toggleAttribute("torch",!!t)}get calculateFileSize(){return this.hasAttribute("calculate-file-size")}set calculateFileSize(t){this.toggleAttribute("calculate-file-size",!!t)}get loading(){return this.hasAttribute("loading")}#m=t=>{t.preventDefault(),this.capture()};#d=t=>{let e=t.target;e.play().then((()=>{this.dispatchEvent(new CustomEvent(`${a}:video-play`,{bubbles:!0,composed:!0,detail:{video:e}}))})).catch((t=>{this.dispatchEvent(new CustomEvent(`${a}:error`,{bubbles:!0,composed:!0,detail:{error:t}}))})).finally((()=>{this.removeAttribute("loading")}))};#r(){this.#s&&this.#s.replaceChildren()}#h(t,e){if(!this.#e)return;let[i]=this.#e.getVideoTracks(),s=this.getTrackCapabilities(),a=this.getTrackSettings(),o="pan"===t||"tilt"===t||"zoom"===t?c(Number(e),s[t]?.min||1,s[t]?.max||1):e;t in a&&i.applyConstraints({advanced:[{[t]:o}]}).catch((()=>{}))}#c=t=>{"capture-button"===t.target?.name&&(this.#n?.removeEventListener("click",this.#m),this.#n=this.#u(),this.#n&&(this.#n.addEventListener("click",this.#m),"BUTTON"!==this.#n.nodeName&&!this.#n.hasAttribute("role")&&this.#n.setAttribute("role","button")))};#u(){return this.#o&&this.#o.assignedElements({flatten:!0}).find((t=>"BUTTON"===t.nodeName||"capture-button"===t.getAttribute("slot")))||null}#l(t){let e=this;if(Object.prototype.hasOwnProperty.call(e,t)){let i=e[t];delete e[t],e[t]=i}}async startVideoStream(e){if(!t.isSupported()||this.#e)return;this.setAttribute("loading","");let i={video:{facingMode:{ideal:this.facingMode},pan:!0,tilt:!0,zoom:!0,torch:this.torch},audio:!1};if("string"==typeof e&&e.trim().length>0&&(i.video.deviceId={exact:e}),"string"==typeof this.cameraResolution&&this.cameraResolution.trim().length>0){let[t=0,e=0]=this.cameraResolution.split("x").map((t=>Number(t)));t>0&&e>0&&(i.video.width=t,i.video.height=e)}try{this.#e=await navigator.mediaDevices.getUserMedia(i),this.#a&&(this.#a.srcObject=this.#e),this.#h("pan",this.pan),this.#h("tilt",this.tilt),this.#h("zoom",this.zoom)}catch(t){this.dispatchEvent(new CustomEvent(`${a}:error`,{bubbles:!0,composed:!0,detail:{error:t}}))}finally{this.removeAttribute("loading")}}restartVideoStream(t){this.#e&&this.#a&&this.stopVideoStream(),this.startVideoStream(t)}stopVideoStream(){if(!this.#a||!this.#e)return;let[t]=this.#e.getVideoTracks();t?.stop(),this.#a.srcObject=null,this.#e=null}async capture(){if(!this.loading&&this.#i&&this.#a)try{let t=this.#i.getContext("2d"),e=this.#a.videoWidth,i=this.#a.videoHeight;this.#i.width=e,this.#i.height=i,t?.drawImage(this.#a,0,0,e,i);let s=this.#i.toDataURL("image/png");if("string"==typeof s&&s.includes("data:image")){if(!this.noImage){let t=new Image;t.src=s,t.width=e,t.height=i,t.alt="Captured photo",t.setAttribute("part","output-image"),this.#r(),this.#s?.appendChild(t)}let t={dataURI:s,width:e,height:i};if(this.calculateFileSize)try{let e=(await(await fetch(s)).blob()).size;e&&(t.size=e)}catch{}this.dispatchEvent(new CustomEvent(`${a}:success`,{bubbles:!0,composed:!0,detail:t}))}}catch(t){this.dispatchEvent(new CustomEvent(`${a}:error`,{bubbles:!0,composed:!0,detail:{error:t}}))}}getSupportedConstraints(){return t.isSupported()&&navigator.mediaDevices.getSupportedConstraints()||{}}getTrackCapabilities(){if(!this.#e)return{};let[t]=this.#e.getVideoTracks();return t&&"function"==typeof t.getCapabilities&&t.getCapabilities()||{}}getTrackSettings(){if(!this.#e)return{};let[t]=this.#e.getVideoTracks();return t&&"function"==typeof t.getSettings&&t.getSettings()||{}}static async getVideoInputDevices(){return navigator.mediaDevices&&navigator.mediaDevices.enumerateDevices?(await navigator.mediaDevices.enumerateDevices()||[]).filter((t=>"videoinput"===t.kind&&!!t.deviceId)):[]}static isSupported(){return!!navigator.mediaDevices?.getUserMedia}static defineCustomElement(e=a){typeof window<"u"&&!window.customElements.get(e)&&window.customElements.define(e,t)}};export{l as CapturePhoto};
//# sourceMappingURL=/sm/965bffbf34b8790b99bba3db5277d6146feab0a8be944fc42b9917659cfe2fe9.map