/**
 * Tauri音频播放工具
 * 用于调用后端的音频播放命令
 */
import { invoke, isTauri } from '@tauri-apps/api/core';

const IS_TAURI = isTauri();

// 定义音频命令类型
const AUDIO_COMMANDS = {
  PLAY: 'play_prompt_tone',
} as const;

/**
 * 播放音频
 */
export async function playAudio(): Promise<void> {
  try {
    if (IS_TAURI) {
      await invoke(AUDIO_COMMANDS.PLAY);
    } else {
      playPromptTone();
    }
  } catch (error: any) {
    console.error('播放音频失败:', error);
  }
}

let audioPlayer: HTMLAudioElement | undefined = undefined;
if (!IS_TAURI) {
  audioPlayer = new Audio('/audio/click_prompt_tone.wav');
  audioPlayer.load();
  console.log('audioPlayer====');
}

/** 播放提示音声音 */
export function playPromptTone() {
  // 创建新的音频元素 （资源在 public/audio下）
  // 设置初始音量和播放速率
  if (audioPlayer) {
    audioPlayer.volume = 0.25;
    audioPlayer.playbackRate = 1;
    audioPlayer.muted = false;
    audioPlayer.play();
  }
}
