import{a as h,b as o,n as a}from"./property-ChPqBGW_.js";import{r as n}from"./state-BnFRu-9h.js";import{B as v}from"./base-element-CNS0Iccv.js";import{r as m,b as f,d as b}from"./styles-BhbdDvdk.js";import{A as g}from"./audio-capture-D84fe_7S.js";import"./index-fe5hJ7Kl.js";import"./audio-analyzer-DIELKr9b.js";var y=Object.defineProperty,i=(u,e,s,r)=>{for(var c=void 0,l=u.length-1,p;l>=0;l--)(p=u[l])&&(c=p(e,s,c)||c);return c&&y(e,s,c),c};const d=class d extends v{constructor(){super("MicrophoneElement"),this.enabled=!1,this.showSelector=!0,this.showVolume=!0,this.deviceId="",this.microphoneId="",this.audioTrack=null,this.captureVolume=100,this.earMonitorVolume=0,this.profile="standard",this._devices=[],this._currentDeviceId="",this._showDeviceList=!1,this._loading=!1,this._error="",this._volume=0,this.captureService=null}onInitialize(){this.captureService=new g({analyzeVolume:this.showVolume,microphoneId:this.microphoneId||this.deviceId||void 0,audioTrack:this.audioTrack||void 0,captureVolume:this.captureVolume,earMonitorVolume:this.earMonitorVolume,profile:this.profile}),this.setupListeners(),this.refreshDevices()}onDisconnect(){this.captureService?.dispose()}setupListeners(){this.captureService&&(this.captureService.on("enabled",({stream:e,deviceId:s})=>{this.enabled=!0,this._currentDeviceId=s,this._error="",this.dispatchCustomEvent("enabled",{stream:e,deviceId:s})}),this.captureService.on("disabled",()=>{this.enabled=!1,this._currentDeviceId="",this._volume=0,this.dispatchCustomEvent("disabled")}),this.captureService.on("devices-updated",({devices:e})=>{this._devices=e,this.dispatchCustomEvent("devices-updated",{devices:e})}),this.captureService.on("device-added",({devices:e})=>{this.dispatchCustomEvent("device-added",{devices:e})}),this.captureService.on("device-removed",({devices:e})=>{this.dispatchCustomEvent("device-removed",{devices:e})}),this.captureService.on("device-disconnected",({deviceId:e})=>{this._error="设备已断开",this.dispatchCustomEvent("device-disconnected",{deviceId:e})}),this.captureService.on("error",e=>{this._error=this.captureService?.error??"获取失败",this.dispatchCustomEvent("error",{error:e})}),this.showVolume&&this.captureService.setVolumeCallback(e=>{this._volume=e.volume}))}async refreshDevices(){this.captureService&&(this._devices=await this.captureService.refreshDevices())}async toggleMicrophone(){if(!(this._loading||!this.captureService)){this._loading=!0,this._error="";try{this.enabled?await this.captureService.stop():await this.captureService.start(this.deviceId||void 0)}catch{}finally{this._loading=!1}}}async switchDevice(e){this.captureService&&(await this.captureService.switchDevice(e),this._showDeviceList=!1)}async onCaptureVolumeChange(e){const s=e.target,r=parseInt(s.value,10);this.captureVolume=r,await this.captureService?.setCaptureVolume(r)}async onEarMonitorVolumeChange(e){const s=e.target,r=parseInt(s.value,10);this.earMonitorVolume=r,await this.captureService?.setEarMonitorVolume(r)}async onProfileChange(e){const r=e.target.value;this.profile=r,await this.captureService?.setProfile(r)}async start(e){await this.captureService?.start(e)}async stop(){await this.captureService?.stop()}getStream(){return this.captureService?.getStream()??null}getDevices(){return[...this._devices]}async setCaptureVolume(e){this.captureVolume=Math.max(0,Math.min(200,e)),await this.captureService?.setCaptureVolume(this.captureVolume)}getCaptureVolume(){return this.captureVolume}async setEarMonitorVolume(e){this.earMonitorVolume=Math.max(0,Math.min(100,e)),await this.captureService?.setEarMonitorVolume(this.earMonitorVolume)}getEarMonitorVolume(){return this.earMonitorVolume}async setProfile(e){this.profile=e,await this.captureService?.setProfile(e)}getProfile(){return this.profile}async setAudioTrack(e){this.audioTrack=e,this.captureService&&await this.captureService.updateOptions({audioTrack:e||void 0})}getAudioTrack(){return this.audioTrack}getProcessedStream(){return this.captureService?.getProcessedStream()??null}render(){return o`
      <div class="mic-container">
        <button 
          class="mic-button ${this.enabled?"":"disabled"} ${this._loading?"loading":""}"
          @click=${this.toggleMicrophone}
          title="${this.enabled?"关闭麦克风":"开启麦克风"}"
          ?disabled=${this._loading}
        >
          ${this._loading?o`<iconify-icon icon="mdi:loading" class="spin"></iconify-icon>`:this.enabled?o`<iconify-icon icon="mdi:microphone"></iconify-icon>`:o`<iconify-icon icon="mdi:microphone-off"></iconify-icon>`}
        </button>

        ${this.showVolume&&this.enabled?o`
          <div class="volume-indicator">
            <div class="volume-bar" style="height: ${this._volume}%"></div>
          </div>
        `:""}

        ${this.showSelector?o`
          <div class="device-selector">
            <button
              class="settings-button"
              @click=${()=>this._showDeviceList=!this._showDeviceList}
              title="麦克风设置"
            >
              <iconify-icon icon="mdi:cog"></iconify-icon>
            </button>
            ${this._showDeviceList?o`
              <div class="device-list">
                <div class="device-section">
                  <div class="device-section-title">麦克风</div>
                  ${this._devices.map(e=>o`
                    <div
                      class="device-item ${e.deviceId===this._currentDeviceId?"active":""}"
                      @click=${()=>this.switchDevice(e.deviceId)}
                    >
                      ${e.deviceId===this._currentDeviceId?o`<span class="check"><iconify-icon icon="mdi:check"></iconify-icon></span>`:""}
                      <span>${e.label}</span>
                    </div>
                  `)}
                  ${this._devices.length===0?o`
                    <div class="device-item">无可用设备</div>
                  `:""}
                </div>

                <div class="device-section">
                  <div class="device-section-title">音量控制</div>
                  <div class="volume-controls">
                    <div class="volume-control">
                      <span class="volume-label">采集音量</span>
                      <input
                        type="range"
                        class="volume-slider"
                        min="0"
                        max="200"
                        .value="${this.captureVolume}"
                        @input=${this.onCaptureVolumeChange}
                      />
                      <span class="volume-value">${this.captureVolume}%</span>
                    </div>
                    <div class="volume-control">
                      <span class="volume-label">耳返音量</span>
                      <input
                        type="range"
                        class="volume-slider"
                        min="0"
                        max="100"
                        .value="${this.earMonitorVolume}"
                        @input=${this.onEarMonitorVolumeChange}
                      />
                      <span class="volume-value">${this.earMonitorVolume}%</span>
                    </div>
                  </div>
                </div>

                <div class="device-section">
                  <div class="device-section-title">音质配置</div>
                  <select
                    class="profile-select"
                    .value="${this.profile}"
                    @change=${this.onProfileChange}
                  >
                    <option value="standard" ?selected=${this.profile==="standard"}>标准音质</option>
                    <option value="high" ?selected=${this.profile==="high"}>高音质</option>
                    <option value="music" ?selected=${this.profile==="music"}>音乐音质</option>
                    <option value="music-stereo" ?selected=${this.profile==="music-stereo"}>音乐立体声</option>
                  </select>
                </div>
              </div>
            `:""}
          </div>
        `:""}

        ${this._error?o`
          <span class="error-text">${this._error}</span>
        `:""}
      </div>
    `}};d.styles=[m,f,b,h`
      :host {
        display: inline-block;
        position: relative;
      }

      .mic-container {
        display: flex;
        align-items: center;
        gap: 8px;
      }

      .mic-button {
        width: 48px;
        height: 48px;
        border-radius: 50%;
        border: none;
        background: #52c41a;
        color: white;
        font-size: 20px;
        cursor: pointer;
        transition: all 0.3s;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      .mic-button:hover {
        background: #73d13d;
      }

      .mic-button.disabled {
        background: #ff4d4f;
      }

      .mic-button.disabled:hover {
        background: #ff7875;
      }

      .mic-button.loading {
        opacity: 0.7;
        cursor: wait;
      }

      .volume-indicator {
        width: 4px;
        height: 24px;
        background: #e0e0e0;
        border-radius: 2px;
        overflow: hidden;
        position: relative;
      }

      .volume-bar {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        background: #52c41a;
        transition: height 0.05s;
      }

      .settings-button {
        width: 32px;
        height: 32px;
        border-radius: 50%;
        border: 1px solid #d9d9d9;
        background: white;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        transition: background 0.2s;
      }

      .settings-button:hover {
        background: #f5f5f5;
      }

      .spin {
        animation: icon-spin 1s linear infinite;
      }
      @keyframes icon-spin {
        from { transform: rotate(0deg); }
        to { transform: rotate(360deg); }
      }

      .device-section {
        padding: 8px 0;
        border-bottom: 1px solid #eee;
      }

      .device-section:last-child {
        border-bottom: none;
      }

      .device-section-title {
        padding: 4px 16px;
        font-size: 12px;
        color: #666;
        font-weight: 500;
      }

      .error-text {
        color: #ff4d4f;
        font-size: 12px;
        position: absolute;
        bottom: -20px;
        left: 50%;
        transform: translateX(-50%);
        white-space: nowrap;
      }

      .volume-controls {
        display: flex;
        flex-direction: column;
        gap: 8px;
        padding: 8px 16px;
      }

      .volume-control {
        display: flex;
        align-items: center;
        gap: 8px;
      }

      .volume-label {
        font-size: 12px;
        color: #666;
        min-width: 60px;
      }

      .volume-slider {
        flex: 1;
        height: 4px;
        -webkit-appearance: none;
        appearance: none;
        background: #e0e0e0;
        border-radius: 2px;
        outline: none;
      }

      .volume-slider::-webkit-slider-thumb {
        -webkit-appearance: none;
        appearance: none;
        width: 12px;
        height: 12px;
        background: #52c41a;
        border-radius: 50%;
        cursor: pointer;
      }

      .volume-slider::-moz-range-thumb {
        width: 12px;
        height: 12px;
        background: #52c41a;
        border-radius: 50%;
        cursor: pointer;
        border: none;
      }

      .volume-value {
        font-size: 12px;
        color: #666;
        min-width: 40px;
        text-align: right;
      }

      .profile-select {
        padding: 8px 16px;
        border: none;
        border-radius: 4px;
        font-size: 12px;
        width: 100%;
        outline: none;
        cursor: pointer;
      }
    `];let t=d;i([a({type:Boolean})],t.prototype,"enabled");i([a({type:Boolean,attribute:"show-selector"})],t.prototype,"showSelector");i([a({type:Boolean,attribute:"show-volume"})],t.prototype,"showVolume");i([a({type:String,attribute:"device-id"})],t.prototype,"deviceId");i([a({type:String,attribute:"microphone-id"})],t.prototype,"microphoneId");i([a({type:Object})],t.prototype,"audioTrack");i([a({type:Number,attribute:"capture-volume"})],t.prototype,"captureVolume");i([a({type:Number,attribute:"ear-monitor-volume"})],t.prototype,"earMonitorVolume");i([a({type:String})],t.prototype,"profile");i([n()],t.prototype,"_devices");i([n()],t.prototype,"_currentDeviceId");i([n()],t.prototype,"_showDeviceList");i([n()],t.prototype,"_loading");i([n()],t.prototype,"_error");i([n()],t.prototype,"_volume");customElements.get("m7s-audio-capture")||customElements.define("m7s-audio-capture",t);export{t as MicrophoneElement};
