import{a0 as e,r as l,q as a,g as t,M as u,K as o,N as n,y as s,W as i,a1 as v}from"./index-Co9nPWLO.js";const r=e("audio",()=>{const e=l([]),r=l(null),c=l([]),d=l(-1),f=l(!1),g=l(0),h=l(0),y=l(1),p=l("off"),m=l(!1),P=l(!0);let w=null;const x=a(()=>c.value.length>0),M=a(()=>"list"===p.value||d.value<c.value.length-1),I=a(()=>"list"===p.value||d.value>0);function T(){w||(w=v(),w.onPlay(()=>{f.value=!0}),w.onPause(()=>{f.value=!1}),w.onEnded(()=>{P.value||"list"===p.value||"single"===p.value?"single"===p.value?w&&(w.seek(0),w.play()):m.value&&c.value.length>1?U():M.value?(d.value++,N(c.value[d.value])):"list"===p.value&&c.value.length>0?(d.value=0,N(c.value[d.value])):f.value=!1:f.value=!1}),w.onTimeUpdate(()=>{g.value=(null==w?void 0:w.currentTime)||0,h.value=(null==w?void 0:w.duration)||0}),w.onError(e=>{console.error("音频播放错误:",e),f.value=!1}))}function N(e){T(),r.value=e;const l=i(e.audioUrl);w&&w.src===l?w.play():w&&(w.src=l,w.play())}function k(){w&&w.pause()}function E(){w&&w.play()}function U(){if(m.value&&c.value.length>1){let e;do{e=Math.floor(Math.random()*c.value.length)}while(e===d.value);d.value=e}else if(M.value)d.value++;else{if(!("list"===p.value&&c.value.length>0))return;d.value=0}N(c.value[d.value])}return{voices:e,currentAudio:r,playlist:c,currentIndex:d,isPlaying:f,currentTime:g,duration:h,playRate:y,loopMode:p,shuffleMode:m,autoPlayNext:P,hasPlaylist:x,hasNext:M,hasPrev:I,fetchVoices:async function(){const l=await t("/tts/voices");return e.value=l.voices,l.voices},generateAudio:async function(e,l,a){console.log("📤 TTS 请求参数:",{text:e.substring(0,50)+"...",textLength:e.length,voiceId:l,voiceParams:a}),u({title:"正在创建任务..."});try{const{audioId:u}=await o("/tts/generate",{text:e,voiceId:l,voiceParams:a});let i=0;const v=60;let r=1;for(console.log("🔄 开始轮询状态, audioId:",u);i<v;){await new Promise(e=>setTimeout(e,1e3*r));const e=await t(`/tts/status/${u}`);if(console.log(`📊 轮询 ${i+1}: status = ${e.status}`),"completed"===e.status&&e.audio)return n(),console.log("✅ 生成成功"),s({title:"生成成功",icon:"success"}),{audioId:u,audioUrl:e.audio.audioUrl,duration:e.audio.audioDuration,size:e.audio.audioSize};if("failed"===e.status)throw console.log("❌ 状态为 failed"),console.log("❌ 完整状态:",e),n(),new Error("音频生成失败，请重试");r=Math.min(r+.5,5),i++}throw n(),new Error("生成超时，请稍后重试")}catch(i){throw n(),i}},play:N,pause:k,resume:E,togglePlay:function(){f.value?k():E()},playPrev:function(){if(m.value&&c.value.length>1){let e;do{e=Math.floor(Math.random()*c.value.length)}while(e===d.value);d.value=e}else if(I.value)d.value--;else{if(!("list"===p.value&&c.value.length>0))return;d.value=c.value.length-1}N(c.value[d.value])},playNext:U,seek:function(e){w&&(w.seek(e),g.value=e)},setPlayRate:function(e){y.value=e,w&&(w.playbackRate=e)},setPlaylist:function(e,l=0,a=!0){c.value=e,d.value=l,e.length>0&&a&&N(e[l])},setAutoPlayNext:function(e){P.value=e},toggleLoopMode:function(){"off"===p.value?p.value="list":"list"===p.value?p.value="single":p.value="off"},toggleShuffleMode:function(){m.value=!m.value},destroy:function(){w&&(w.destroy(),w=null)}}});export{r as u};
