@z-api/call 1.0.0-staging.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -911,5 +911,5 @@ var ZAPICall=(function(x){"use strict";class O{constructor(){this.listeners=new
911
911
  </button>
912
912
  </div>
913
913
  `:""}
914
- `;const n=e.querySelector(`[data-accept="${t.callId}"]`);return n==null||n.addEventListener("click",()=>{this.config.onAccept(t.callId),this.config.onResume()}),e.querySelectorAll(`[data-reject="${t.callId}"]`).forEach(o=>o.addEventListener("click",()=>this.config.onReject(t.callId))),s&&(this.timerStarts.has(t.callId)||this.timerStarts.set(t.callId,Date.now()),this.startTimer(t.callId,e)),t.state==="ended"&&this.stopTimer(t.callId),e}stateLabel(t){switch(t){case"ringing":return"Chamando";case"calling":return"Ligando...";case"preaccepted":return"Conectando";case"accepted":return"Aceita";case"active":return"Ativa";case"ended":return"Encerrada";default:return t}}startTimer(t,e){this.stopTimer(t);const a=this.timerStarts.get(t)||Date.now(),i=e.querySelector(`[data-timer="${t}"]`);if(!i)return;const s=()=>{i.textContent=B(Date.now()-a)};s(),this.timers.set(t,setInterval(s,1e3))}stopTimer(t){const e=this.timers.get(t);e&&(clearInterval(e),this.timers.delete(t))}startInCallTimer(t){this.stopInCallTimer();const e=this.timerStarts.get(t)||Date.now(),a=()=>{const i=B(Date.now()-e);this.incallTimerEl.textContent=i,this.videoTimerEl&&(this.videoTimerEl.textContent=i)};a(),this.incallTimer=setInterval(a,1e3)}stopInCallTimer(){this.incallTimer&&(clearInterval(this.incallTimer),this.incallTimer=null),this.incallTimerEl.textContent="00:00"}showVideo(t){if(!this.videoContainer)return;this.videoContainer.style.display=t?"flex":"none";const e=this.incallView.querySelector(".audio-call-ui");e&&(e.style.display=t?"none":""),t&&(this.videoPhoneEl.textContent=this.incallPhoneEl.textContent,this.videoTimerEl.textContent=this.incallTimerEl.textContent)}getRemoteCanvas(){return this.remoteVideoCanvas}resetCamera(){this.cameraOn=!1,this.updateCameraButton()}updateMuteButtons(){this.incallMuteBtn.classList.toggle("muted",this.muted),this.incallMuteBtn.innerHTML=this.muted?R:S;const t=this.incallView.querySelector(".video-btn-mute");t&&(t.classList.toggle("muted",this.muted),t.innerHTML=this.muted?R:S)}updateCameraButton(){this.incallView.querySelectorAll(".video-btn-camera, .incall-btn-camera").forEach(e=>{e.classList.toggle("muted",!this.cameraOn),e.innerHTML=this.cameraOn?T:A})}showMakeCallError(t){const e=t.includes("PHONE_NOT_ON_WHATSAPP")?"Número não encontrado":"Erro ao realizar chamada",a=this.dialerInput.placeholder;this.dialerInput.value="",this.dialerInput.placeholder=e,this.dialerInput.classList.add("error"),setTimeout(()=>{this.dialerInput.placeholder=a,this.dialerInput.classList.remove("error")},3e3)}updateAudioLevel(t){const e=Math.min(100,Math.max(0,t*100));this.audioLevelBar.style.width=`${e}%`}renderAvatar(t){const e=this.pictureCache.get(t);return e?`<img src="${e}" class="avatar-img" />`:(!this.pictureRequested.has(t)&&t&&(this.pictureRequested.add(t),this.config.onRequestProfilePicture(t)),D)}updateProfilePicture(t,e){if(this.pictureCache.set(t,e),!e)return;const a=`<img src="${e}" class="avatar-img" />`;this.shadow.querySelectorAll(`[data-avatar-phone="${t}"]`).forEach(i=>{i.innerHTML=a})}clearAllTimers(){for(const t of this.timers.values())clearInterval(t);this.timers.clear(),this.timerStarts.clear(),this.stopInCallTimer()}destroy(){this.clearAllTimers(),this.host.remove()}}class P extends O{constructor(t){super(),this.widget=null,this.audioConfig={sampleRate:16e3,channels:1},this.connected=!1,this.micStarted=!1,this.cameraStarted=!1,this.destroyed=!1,this.sdkId="",this.contacts=[],this.opts=t,this.state=new X,this.audio=new z(this.audioConfig.sampleRate),this.video=new Y,this.ws=new j("https://staging-call.z-api.io",t.instanceId,t.getToken),this.ws.onJson=e=>this.handleJsonMessage(e),this.ws.onBinary=e=>this.handleBinaryMessage(e),this.ws.onConnectionChange=e=>this.handleConnectionChange(e),this.audio.onMicData=e=>{const a=new ArrayBuffer(1+e.byteLength);new Uint8Array(a)[0]=1,new Uint8Array(a,1).set(new Uint8Array(e)),this.ws.send(a)},this.audio.onAudioLevel=e=>{var a;this.emit("audio:level",e),(a=this.widget)==null||a.updateAudioLevel(e)},this.on("video:frame",e=>this.video.renderFrame(e)),this.video.onEncodedCameraFrame=(e,a,i,s,n,r)=>{const o=new ArrayBuffer(22+e.byteLength),l=new DataView(o);l.setUint8(0,3),l.setUint16(1,a,!0),l.setUint16(3,i,!0),l.setFloat64(5,s),l.setUint8(13,n?1:0),l.setFloat64(14,r),new Uint8Array(o,22).set(new Uint8Array(e)),this.ws.send(o)},this.video.onCameraFrame=(e,a,i)=>{const s=this.rgbaToNv12(new Uint8Array(e),a,i),n=new ArrayBuffer(5+s.byteLength),r=new DataView(n);r.setUint8(0,2),r.setUint16(1,a,!0),r.setUint16(3,i,!0),new Uint8Array(n,5).set(s),this.ws.send(n)},t.autoWidget!==!1&&(this.widget=new lt({position:t.position||"bottom-right",theme:t.theme||{},showVideoDialButton:t.showVideoDialButton===!0,onAccept:e=>this.accept(e),onReject:e=>this.reject(e),onMute:e=>this.mute(e),onCamera:e=>this.setCamera(e),onResume:()=>this.audio.resume(),onMakeCall:(e,a)=>this.makeCall(e,a),onRequestContacts:()=>this.requestContacts(),onRequestProfilePicture:e=>this.requestProfilePicture(e)}),this.video.setRemoteCanvas(this.widget.getRemoteCanvas())),this.ws.connect()}accept(t){console.log("[ZAPICall] >> call:accept",t),this.ws.sendJson({type:"call:accept",callId:t}),this.startMicIfNeeded()}reject(t){console.log("[ZAPICall] >> call:reject",t),this.ws.sendJson({type:"call:reject",callId:t})}mute(t){this.ws.sendJson({type:"call:mute",muted:t})}async setCamera(t){var e;this.ws.sendJson({type:"call:camera",enabled:t}),t?(await this.startCameraIfNeeded(),(e=this.widget)==null||e.showVideo(!0)):(this.video.stopCamera(),this.cameraStarted=!1)}isCameraActive(){return this.cameraStarted}makeCall(t,e=!1){var a,i;console.log("[ZAPICall] >> call:make",t,e?"(video)":"(audio)"),this.audio.resume(),this.ws.sendJson({type:"call:make",number:t,isVideo:e}),this.emit("call:make",t),(i=(a=this.opts).onMakeCall)==null||i.call(a,t)}getCalls(){return this.state.getAllCalls()}getActiveCall(){return this.state.getActiveCall()}isConnected(){return this.connected}getSdkId(){return this.sdkId}requestContacts(){this.ws.sendJson({type:"contacts:list"})}requestProfilePicture(t){this.ws.sendJson({type:"profile:picture",phone:t})}getContacts(){return this.contacts}destroy(){var t;this.destroyed||(this.destroyed=!0,this.ws.destroy(),this.audio.destroy(),this.video.destroy(),(t=this.widget)==null||t.destroy(),this.state.clear(),this.removeAllListeners())}handleJsonMessage(t){var e,a,i,s,n,r,o,l,p,u,b,d,m,f,g,C,E,y,w,M;switch(t.type){case"connected":this.sdkId=t.sdkId||"",t.audioConfig&&(t.audioConfig.sampleRate!==this.audioConfig.sampleRate&&(this.audio.destroy(),this.audio=new z(t.audioConfig.sampleRate),this.audio.onMicData=h=>{const v=new ArrayBuffer(1+h.byteLength);new Uint8Array(v)[0]=1,new Uint8Array(v,1).set(new Uint8Array(h)),this.ws.send(v)},this.audio.onAudioLevel=h=>{var v;this.emit("audio:level",h),(v=this.widget)==null||v.updateAudioLevel(h)}),this.audioConfig=t.audioConfig);break;case"call:incoming":{const h={callId:t.callId,from:t.from,notify:t.notify,isVideo:t.isVideo,isGroup:t.isGroup,state:t.state||"ringing",timestamp:t.timestamp};this.state.addCall(h),this.emit("call:incoming",h),(a=(e=this.opts).onIncomingCall)==null||a.call(e,h),(i=this.widget)==null||i.updateCalls(this.state.getAllCalls(),this.connected);break}case"call:outgoing":{const h={callId:t.callId,from:t.to,isVideo:t.isVideo,isGroup:!1,state:"calling",timestamp:Date.now()};this.state.addCall(h),this.emit("call:outgoing",h),(s=this.widget)==null||s.updateCalls(this.state.getAllCalls(),this.connected),this.startMicIfNeeded();break}case"call:state":{if(!this.state.getCall(t.callId))break;if(this.state.updateState(t.callId,t.state),this.emit("call:state",t.callId,t.state),t.state==="accepted"||t.state==="active"){this.startMicIfNeeded();const h=this.state.getCall(t.callId);h!=null&&h.isVideo?((n=this.widget)==null||n.showVideo(!0),this.startCameraIfNeeded()):(this.video.stopCamera(),this.cameraStarted=!1,(r=this.widget)==null||r.showVideo(!1))}(o=this.widget)==null||o.updateCalls(this.state.getAllCalls(),this.connected);break}case"call:terminated":{this.state.updateState(t.callId,"ended"),this.emit("call:terminated",t.callId,t.reason),(p=(l=this.opts).onCallTerminated)==null||p.call(l,t.callId),this.video.stopCamera(),this.video.clearCanvas(),this.cameraStarted=!1,(u=this.widget)==null||u.showVideo(!1),(b=this.widget)==null||b.resetCamera(),setTimeout(()=>{var h;this.state.removeCall(t.callId),(h=this.widget)==null||h.updateCalls(this.state.getAllCalls(),this.connected),this.state.hasActiveCalls()||(this.audio.stopMic(),this.micStarted=!1)},3e3),(d=this.widget)==null||d.updateCalls(this.state.getAllCalls(),this.connected);break}case"call:video-state":{this.state.updateIsVideo(t.callId,t.isVideo),this.emit("call:video-state",t.callId,t.isVideo);const h=this.state.getCall(t.callId);h&&(h.state==="accepted"||h.state==="active")&&(t.isVideo?(m=this.widget)==null||m.showVideo(!0):(this.video.stopCamera(),this.cameraStarted=!1,(f=this.widget)==null||f.showVideo(!1))),(g=this.widget)==null||g.updateCalls(this.state.getAllCalls(),this.connected);break}case"call:claimed":{this.state.removeCall(t.callId),this.emit("call:claimed",t.callId),(C=this.widget)==null||C.updateCalls(this.state.getAllCalls(),this.connected);break}case"call:dismissed":{this.state.removeCall(t.callId),this.emit("call:dismissed",t.callId),(E=this.widget)==null||E.updateCalls(this.state.getAllCalls(),this.connected);break}case"contacts:list":{this.contacts=t.contacts||[],this.emit("contacts:list",this.contacts),(y=this.widget)==null||y.updateContacts(this.contacts);break}case"profile:picture":{this.emit("profile:picture",t.phone,t.url),(w=this.widget)==null||w.updateProfilePicture(t.phone,t.url);break}case"pong":break;case"error":t.command==="call:make"&&(this.emit("call:make:error","",t.message||"UNKNOWN_ERROR"),(M=this.widget)==null||M.showMakeCallError(t.message||"UNKNOWN_ERROR")),this.emit("error",new Error(`${t.command}: ${t.message}`));break}}handleBinaryMessage(t){if(t.byteLength<2)return;const e=new DataView(t),a=e.getUint8(0);if(a===1&&t.byteLength>=16){const i=e.getUint16(1,!0),s=e.getUint16(3,!0),n=e.getUint8(5),r=e.getUint8(6),o=e.getUint8(7)!==0,l=e.getFloat64(8,!1),p=t.slice(16),u={width:i,height:s,format:n,orientation:r,isKeyFrame:o,timestamp:l,data:p};this.emit("video:frame",u);return}if(a===0&&t.byteLength%2===1){this.audio.playPcm(t.slice(1));return}this.audio.playPcm(t)}handleConnectionChange(t){var e,a,i;this.connected=t,t?this.emit("connected"):this.emit("disconnected"),(a=(e=this.opts).onConnectionChange)==null||a.call(e,t),(i=this.widget)==null||i.updateCalls(this.state.getAllCalls(),t)}async startCameraIfNeeded(){if(!(this.cameraStarted||this.destroyed)){this.cameraStarted=!0;try{const t=document.createElement("video");t.style.display="none",document.body.appendChild(t),await this.video.startCamera(t,320,240,30)}catch(t){console.warn("[ZAPICall] Camera start failed:",t),this.cameraStarted=!1}}}rgbaToNv12(t,e,a){const i=e*a,s=new Uint8Array(i+(i>>1));for(let r=0;r<a;r++)for(let o=0;o<e;o++){const l=(r*e+o)*4;s[r*e+o]=(66*t[l]+129*t[l+1]+25*t[l+2]+128>>8)+16}let n=i;for(let r=0;r<a;r+=2)for(let o=0;o<e;o+=2){const l=(r*e+o)*4;s[n++]=(-38*t[l]-74*t[l+1]+112*t[l+2]+128>>8)+128,s[n++]=(112*t[l]-94*t[l+1]-18*t[l+2]+128>>8)+128}return s}async startMicIfNeeded(){if(!(this.micStarted||this.destroyed)){this.micStarted=!0;try{await this.audio.startMic()}catch(t){this.emit("error",t instanceof Error?t:new Error(String(t)))}}}}const dt={NV12:0,I420:1,RGB24:2,RGBA:3,H264:100},ht={Unknown:0,Normal:1,Rotate90:2,Rotate180:3,Rotate270:4};function H(c){return new P(c)}const pt={init:H,Client:P};return x.VideoFormat=dt,x.VideoOrientation=ht,x.ZAPICall=pt,x.ZAPICallClient=P,x.init=H,Object.defineProperty(x,Symbol.toStringTag,{value:"Module"}),x})({});
914
+ `;const n=e.querySelector(`[data-accept="${t.callId}"]`);return n==null||n.addEventListener("click",()=>{this.config.onAccept(t.callId),this.config.onResume()}),e.querySelectorAll(`[data-reject="${t.callId}"]`).forEach(o=>o.addEventListener("click",()=>this.config.onReject(t.callId))),s&&(this.timerStarts.has(t.callId)||this.timerStarts.set(t.callId,Date.now()),this.startTimer(t.callId,e)),t.state==="ended"&&this.stopTimer(t.callId),e}stateLabel(t){switch(t){case"ringing":return"Chamando";case"calling":return"Ligando...";case"preaccepted":return"Conectando";case"accepted":return"Aceita";case"active":return"Ativa";case"ended":return"Encerrada";default:return t}}startTimer(t,e){this.stopTimer(t);const a=this.timerStarts.get(t)||Date.now(),i=e.querySelector(`[data-timer="${t}"]`);if(!i)return;const s=()=>{i.textContent=B(Date.now()-a)};s(),this.timers.set(t,setInterval(s,1e3))}stopTimer(t){const e=this.timers.get(t);e&&(clearInterval(e),this.timers.delete(t))}startInCallTimer(t){this.stopInCallTimer();const e=this.timerStarts.get(t)||Date.now(),a=()=>{const i=B(Date.now()-e);this.incallTimerEl.textContent=i,this.videoTimerEl&&(this.videoTimerEl.textContent=i)};a(),this.incallTimer=setInterval(a,1e3)}stopInCallTimer(){this.incallTimer&&(clearInterval(this.incallTimer),this.incallTimer=null),this.incallTimerEl.textContent="00:00"}showVideo(t){if(!this.videoContainer)return;this.videoContainer.style.display=t?"flex":"none";const e=this.incallView.querySelector(".audio-call-ui");e&&(e.style.display=t?"none":""),t&&(this.videoPhoneEl.textContent=this.incallPhoneEl.textContent,this.videoTimerEl.textContent=this.incallTimerEl.textContent)}getRemoteCanvas(){return this.remoteVideoCanvas}resetCamera(){this.cameraOn=!1,this.updateCameraButton()}updateMuteButtons(){this.incallMuteBtn.classList.toggle("muted",this.muted),this.incallMuteBtn.innerHTML=this.muted?R:S;const t=this.incallView.querySelector(".video-btn-mute");t&&(t.classList.toggle("muted",this.muted),t.innerHTML=this.muted?R:S)}updateCameraButton(){this.incallView.querySelectorAll(".video-btn-camera, .incall-btn-camera").forEach(e=>{e.classList.toggle("muted",!this.cameraOn),e.innerHTML=this.cameraOn?T:A})}showMakeCallError(t){const e=t.includes("PHONE_NOT_ON_WHATSAPP")?"Número não encontrado":"Erro ao realizar chamada",a=this.dialerInput.placeholder;this.dialerInput.value="",this.dialerInput.placeholder=e,this.dialerInput.classList.add("error"),setTimeout(()=>{this.dialerInput.placeholder=a,this.dialerInput.classList.remove("error")},3e3)}updateAudioLevel(t){const e=Math.min(100,Math.max(0,t*100));this.audioLevelBar.style.width=`${e}%`}renderAvatar(t){const e=this.pictureCache.get(t);return e?`<img src="${e}" class="avatar-img" />`:(!this.pictureRequested.has(t)&&t&&(this.pictureRequested.add(t),this.config.onRequestProfilePicture(t)),D)}updateProfilePicture(t,e){if(this.pictureCache.set(t,e),!e)return;const a=`<img src="${e}" class="avatar-img" />`;this.shadow.querySelectorAll(`[data-avatar-phone="${t}"]`).forEach(i=>{i.innerHTML=a})}clearAllTimers(){for(const t of this.timers.values())clearInterval(t);this.timers.clear(),this.timerStarts.clear(),this.stopInCallTimer()}destroy(){this.clearAllTimers(),this.host.remove()}}class P extends O{constructor(t){super(),this.widget=null,this.audioConfig={sampleRate:16e3,channels:1},this.connected=!1,this.micStarted=!1,this.cameraStarted=!1,this.destroyed=!1,this.sdkId="",this.contacts=[],this.opts=t,this.state=new X,this.audio=new z(this.audioConfig.sampleRate),this.video=new Y,this.ws=new j("https://call.z-api.io",t.instanceId,t.getToken),this.ws.onJson=e=>this.handleJsonMessage(e),this.ws.onBinary=e=>this.handleBinaryMessage(e),this.ws.onConnectionChange=e=>this.handleConnectionChange(e),this.audio.onMicData=e=>{const a=new ArrayBuffer(1+e.byteLength);new Uint8Array(a)[0]=1,new Uint8Array(a,1).set(new Uint8Array(e)),this.ws.send(a)},this.audio.onAudioLevel=e=>{var a;this.emit("audio:level",e),(a=this.widget)==null||a.updateAudioLevel(e)},this.on("video:frame",e=>this.video.renderFrame(e)),this.video.onEncodedCameraFrame=(e,a,i,s,n,r)=>{const o=new ArrayBuffer(22+e.byteLength),l=new DataView(o);l.setUint8(0,3),l.setUint16(1,a,!0),l.setUint16(3,i,!0),l.setFloat64(5,s),l.setUint8(13,n?1:0),l.setFloat64(14,r),new Uint8Array(o,22).set(new Uint8Array(e)),this.ws.send(o)},this.video.onCameraFrame=(e,a,i)=>{const s=this.rgbaToNv12(new Uint8Array(e),a,i),n=new ArrayBuffer(5+s.byteLength),r=new DataView(n);r.setUint8(0,2),r.setUint16(1,a,!0),r.setUint16(3,i,!0),new Uint8Array(n,5).set(s),this.ws.send(n)},t.autoWidget!==!1&&(this.widget=new lt({position:t.position||"bottom-right",theme:t.theme||{},showVideoDialButton:t.showVideoDialButton===!0,onAccept:e=>this.accept(e),onReject:e=>this.reject(e),onMute:e=>this.mute(e),onCamera:e=>this.setCamera(e),onResume:()=>this.audio.resume(),onMakeCall:(e,a)=>this.makeCall(e,a),onRequestContacts:()=>this.requestContacts(),onRequestProfilePicture:e=>this.requestProfilePicture(e)}),this.video.setRemoteCanvas(this.widget.getRemoteCanvas())),this.ws.connect()}accept(t){console.log("[ZAPICall] >> call:accept",t),this.ws.sendJson({type:"call:accept",callId:t}),this.startMicIfNeeded()}reject(t){console.log("[ZAPICall] >> call:reject",t),this.ws.sendJson({type:"call:reject",callId:t})}mute(t){this.ws.sendJson({type:"call:mute",muted:t})}async setCamera(t){var e;this.ws.sendJson({type:"call:camera",enabled:t}),t?(await this.startCameraIfNeeded(),(e=this.widget)==null||e.showVideo(!0)):(this.video.stopCamera(),this.cameraStarted=!1)}isCameraActive(){return this.cameraStarted}makeCall(t,e=!1){var a,i;console.log("[ZAPICall] >> call:make",t,e?"(video)":"(audio)"),this.audio.resume(),this.ws.sendJson({type:"call:make",number:t,isVideo:e}),this.emit("call:make",t),(i=(a=this.opts).onMakeCall)==null||i.call(a,t)}getCalls(){return this.state.getAllCalls()}getActiveCall(){return this.state.getActiveCall()}isConnected(){return this.connected}getSdkId(){return this.sdkId}requestContacts(){this.ws.sendJson({type:"contacts:list"})}requestProfilePicture(t){this.ws.sendJson({type:"profile:picture",phone:t})}getContacts(){return this.contacts}destroy(){var t;this.destroyed||(this.destroyed=!0,this.ws.destroy(),this.audio.destroy(),this.video.destroy(),(t=this.widget)==null||t.destroy(),this.state.clear(),this.removeAllListeners())}handleJsonMessage(t){var e,a,i,s,n,r,o,l,p,u,b,d,m,f,g,C,E,y,w,M;switch(t.type){case"connected":this.sdkId=t.sdkId||"",t.audioConfig&&(t.audioConfig.sampleRate!==this.audioConfig.sampleRate&&(this.audio.destroy(),this.audio=new z(t.audioConfig.sampleRate),this.audio.onMicData=h=>{const v=new ArrayBuffer(1+h.byteLength);new Uint8Array(v)[0]=1,new Uint8Array(v,1).set(new Uint8Array(h)),this.ws.send(v)},this.audio.onAudioLevel=h=>{var v;this.emit("audio:level",h),(v=this.widget)==null||v.updateAudioLevel(h)}),this.audioConfig=t.audioConfig);break;case"call:incoming":{const h={callId:t.callId,from:t.from,notify:t.notify,isVideo:t.isVideo,isGroup:t.isGroup,state:t.state||"ringing",timestamp:t.timestamp};this.state.addCall(h),this.emit("call:incoming",h),(a=(e=this.opts).onIncomingCall)==null||a.call(e,h),(i=this.widget)==null||i.updateCalls(this.state.getAllCalls(),this.connected);break}case"call:outgoing":{const h={callId:t.callId,from:t.to,isVideo:t.isVideo,isGroup:!1,state:"calling",timestamp:Date.now()};this.state.addCall(h),this.emit("call:outgoing",h),(s=this.widget)==null||s.updateCalls(this.state.getAllCalls(),this.connected),this.startMicIfNeeded();break}case"call:state":{if(!this.state.getCall(t.callId))break;if(this.state.updateState(t.callId,t.state),this.emit("call:state",t.callId,t.state),t.state==="accepted"||t.state==="active"){this.startMicIfNeeded();const h=this.state.getCall(t.callId);h!=null&&h.isVideo?((n=this.widget)==null||n.showVideo(!0),this.startCameraIfNeeded()):(this.video.stopCamera(),this.cameraStarted=!1,(r=this.widget)==null||r.showVideo(!1))}(o=this.widget)==null||o.updateCalls(this.state.getAllCalls(),this.connected);break}case"call:terminated":{this.state.updateState(t.callId,"ended"),this.emit("call:terminated",t.callId,t.reason),(p=(l=this.opts).onCallTerminated)==null||p.call(l,t.callId),this.video.stopCamera(),this.video.clearCanvas(),this.cameraStarted=!1,(u=this.widget)==null||u.showVideo(!1),(b=this.widget)==null||b.resetCamera(),setTimeout(()=>{var h;this.state.removeCall(t.callId),(h=this.widget)==null||h.updateCalls(this.state.getAllCalls(),this.connected),this.state.hasActiveCalls()||(this.audio.stopMic(),this.micStarted=!1)},3e3),(d=this.widget)==null||d.updateCalls(this.state.getAllCalls(),this.connected);break}case"call:video-state":{this.state.updateIsVideo(t.callId,t.isVideo),this.emit("call:video-state",t.callId,t.isVideo);const h=this.state.getCall(t.callId);h&&(h.state==="accepted"||h.state==="active")&&(t.isVideo?(m=this.widget)==null||m.showVideo(!0):(this.video.stopCamera(),this.cameraStarted=!1,(f=this.widget)==null||f.showVideo(!1))),(g=this.widget)==null||g.updateCalls(this.state.getAllCalls(),this.connected);break}case"call:claimed":{this.state.removeCall(t.callId),this.emit("call:claimed",t.callId),(C=this.widget)==null||C.updateCalls(this.state.getAllCalls(),this.connected);break}case"call:dismissed":{this.state.removeCall(t.callId),this.emit("call:dismissed",t.callId),(E=this.widget)==null||E.updateCalls(this.state.getAllCalls(),this.connected);break}case"contacts:list":{this.contacts=t.contacts||[],this.emit("contacts:list",this.contacts),(y=this.widget)==null||y.updateContacts(this.contacts);break}case"profile:picture":{this.emit("profile:picture",t.phone,t.url),(w=this.widget)==null||w.updateProfilePicture(t.phone,t.url);break}case"pong":break;case"error":t.command==="call:make"&&(this.emit("call:make:error","",t.message||"UNKNOWN_ERROR"),(M=this.widget)==null||M.showMakeCallError(t.message||"UNKNOWN_ERROR")),this.emit("error",new Error(`${t.command}: ${t.message}`));break}}handleBinaryMessage(t){if(t.byteLength<2)return;const e=new DataView(t),a=e.getUint8(0);if(a===1&&t.byteLength>=16){const i=e.getUint16(1,!0),s=e.getUint16(3,!0),n=e.getUint8(5),r=e.getUint8(6),o=e.getUint8(7)!==0,l=e.getFloat64(8,!1),p=t.slice(16),u={width:i,height:s,format:n,orientation:r,isKeyFrame:o,timestamp:l,data:p};this.emit("video:frame",u);return}if(a===0&&t.byteLength%2===1){this.audio.playPcm(t.slice(1));return}this.audio.playPcm(t)}handleConnectionChange(t){var e,a,i;this.connected=t,t?this.emit("connected"):this.emit("disconnected"),(a=(e=this.opts).onConnectionChange)==null||a.call(e,t),(i=this.widget)==null||i.updateCalls(this.state.getAllCalls(),t)}async startCameraIfNeeded(){if(!(this.cameraStarted||this.destroyed)){this.cameraStarted=!0;try{const t=document.createElement("video");t.style.display="none",document.body.appendChild(t),await this.video.startCamera(t,320,240,30)}catch(t){console.warn("[ZAPICall] Camera start failed:",t),this.cameraStarted=!1}}}rgbaToNv12(t,e,a){const i=e*a,s=new Uint8Array(i+(i>>1));for(let r=0;r<a;r++)for(let o=0;o<e;o++){const l=(r*e+o)*4;s[r*e+o]=(66*t[l]+129*t[l+1]+25*t[l+2]+128>>8)+16}let n=i;for(let r=0;r<a;r+=2)for(let o=0;o<e;o+=2){const l=(r*e+o)*4;s[n++]=(-38*t[l]-74*t[l+1]+112*t[l+2]+128>>8)+128,s[n++]=(112*t[l]-94*t[l+1]-18*t[l+2]+128>>8)+128}return s}async startMicIfNeeded(){if(!(this.micStarted||this.destroyed)){this.micStarted=!0;try{await this.audio.startMic()}catch(t){this.emit("error",t instanceof Error?t:new Error(String(t)))}}}}const dt={NV12:0,I420:1,RGB24:2,RGBA:3,H264:100},ht={Unknown:0,Normal:1,Rotate90:2,Rotate180:3,Rotate270:4};function H(c){return new P(c)}const pt={init:H,Client:P};return x.VideoFormat=dt,x.VideoOrientation=ht,x.ZAPICall=pt,x.ZAPICallClient=P,x.init=H,Object.defineProperty(x,Symbol.toStringTag,{value:"Module"}),x})({});
915
915
  //# sourceMappingURL=z-api-call.global.js.map