"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react"),v=require("../utils/BlobFix.cjs.js");function D(){const l=["audio/webm","audio/mp4","audio/ogg","audio/wav","audio/aac"];for(let o=0;o<l.length;o++)if(MediaRecorder.isTypeSupported(l[o]))return l[o]}function S(){const[l,o]=e.useState(!1),[g,f]=e.useState(0),[b,w]=e.useState(!1),t=e.useRef(null),c=e.useRef(null),d=e.useRef([]),s=e.useRef(null),p=e.useRef(0),h=e.useCallback(async()=>{try{t.current||(t.current=await navigator.mediaDevices.getUserMedia({audio:!0}));const r=D(),n=new MediaRecorder(t.current,{mimeType:r});c.current=n,d.current=[],p.current=Date.now(),n.addEventListener("dataavailable",u=>{u.data.size>0&&d.current.push(u.data)}),n.start(),o(!0),s.current=window.setInterval(()=>{f(Math.floor((Date.now()-p.current)/1e3))},1e3)}catch(r){console.error("Error accessing microphone:",r),alert("Unable to access microphone. Please check your permissions.")}},[]),y=e.useCallback(()=>new Promise(r=>{if(c.current&&c.current.state==="recording"){const n=c.current.mimeType,u=Date.now()-p.current;c.current.addEventListener("stop",async()=>{let a=new Blob(d.current,{type:n});n==="audio/webm"&&(a=await v.webmFixDuration(a,u,a.type)),r(a)}),c.current.stop(),o(!1),f(0),s.current&&(window.clearInterval(s.current),s.current=null),t.current&&(t.current.getTracks().forEach(a=>a.stop()),t.current=null)}else r(null)}),[]),T=e.useCallback(async(r,n,u,a)=>{w(!0);try{const i=new FormData;i.append("file",r,"audio.webm"),i.append("model",a),i.append("response_format","json");const R={};n&&(R.Authorization=`Bearer ${n}`);const m=await fetch(u,{method:"POST",headers:R,body:i});if(!m.ok)throw new Error(`Transcription failed: ${m.statusText}`);return(await m.json()).text||""}catch(i){throw console.error("Transcription error:",i),i}finally{w(!1)}},[]),k=e.useCallback(()=>{c.current&&c.current.stop(),t.current&&(t.current.getTracks().forEach(r=>r.stop()),t.current=null),s.current&&(window.clearInterval(s.current),s.current=null),d.current=[],o(!1),f(0)},[]);return{isRecording:l,recordingTime:g,isTranscribing:b,startRecording:h,stopRecording:y,transcribeAudio:T,cancelRecording:k}}exports.useAudioRecorder=S;
//# sourceMappingURL=useAudioRecorder.cjs.js.map
