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-8qyqvBFG.js";import{r as m,b as f,d as b}from"./styles-BhbdDvdk.js";import{A as g}from"./audio-capture-DDCtvKgv.js";import"./index-DWRYsVdq.js";import"./audio-analyzer-Bq-HMQdl.js";var y=Object.defineProperty,i=(d,e,r,s)=>{for(var c=void 0,l=d.length-1,p;l>=0;l--)(p=d[l])&&(c=p(e,r,c)||c);return c&&y(e,r,c),c};const u=class u 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:r})=>{this.enabled=!0,this._currentDeviceId=r,this._error="",this.dispatchCustomEvent("enabled",{stream:e,deviceId:r})}),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="Device disconnected",this.dispatchCustomEvent("device-disconnected",{deviceId:e})}),this.captureService.on("error",e=>{this._error=this.captureService?.error??"Fetch failed",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 r=e.target,s=parseInt(r.value,10);this.captureVolume=s,await this.captureService?.setCaptureVolume(s)}async onEarMonitorVolumeChange(e){const r=e.target,s=parseInt(r.value,10);this.earMonitorVolume=s,await this.captureService?.setEarMonitorVolume(s)}async onProfileChange(e){const s=e.target.value;this.profile=s,await this.captureService?.setProfile(s)}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?"CloseMicrophone":"OnMicrophone"}"
          ?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="MicrophoneSettings"
            >
              <iconify-icon icon="mdi:cog"></iconify-icon>
            </button>
            ${this._showDeviceList?o`
              <div class="device-list">
                <div class="device-section">
                  <div class="device-section-title">Microphone</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">无可用Device</div>
                  `:""}
                </div>

                <div class="device-section">
                  <div class="device-section-title">Volume control</div>
                  <div class="volume-controls">
                    <div class="volume-control">
                      <span class="volume-label">Capture volume</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">Monitor volume</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">Audio quality</div>
                  <select
                    class="profile-select"
                    .value="${this.profile}"
                    @change=${this.onProfileChange}
                  >
                    <option value="standard" ?selected=${this.profile==="standard"}>Standard quality</option>
                    <option value="high" ?selected=${this.profile==="high"}>High quality</option>
                    <option value="music" ?selected=${this.profile==="music"}>Music quality</option>
                    <option value="music-stereo" ?selected=${this.profile==="music-stereo"}>Music stereo</option>
                  </select>
                </div>
              </div>
            `:""}
          </div>
        `:""}

        ${this._error?o`
          <span class="error-text">${this._error}</span>
        `:""}
      </div>
    `}};u.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=u;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};
