"use strict";
const common_vendor = require("../../common/vendor.js");
const _sfc_main = {
  data() {
    return {
      messages: [],
      isRecording: false,
      recorderManager: null,
      innerAudioContext: null,
      scrollTop: 0,
      apiUrl: "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions"
    };
  },
  onLoad() {
    this.recorderManager = common_vendor.index.getRecorderManager();
    this.innerAudioContext = common_vendor.index.createInnerAudioContext();
    this.recorderManager.onStop((res) => {
      this.processAudio(res.tempFilePath);
    });
  },
  methods: {
    startRecording() {
      this.isRecording = true;
      this.recorderManager.start({
        format: "mp3",
        sampleRate: 16e3,
        numberOfChannels: 1
      });
    },
    stopRecording() {
      this.isRecording = false;
      this.recorderManager.stop();
    },
    processAudio(filePath) {
      common_vendor.index.showLoading({
        title: "处理中..."
      });
      this.speechToText(filePath).then((text) => {
        this.addMessage("user", text);
        this.callDashScopeAPI(text);
      }).catch((err) => {
        common_vendor.index.hideLoading();
        common_vendor.index.showToast({
          title: "语音识别失败",
          icon: "none"
        });
        console.error(err);
      });
    },
    speechToText(filePath) {
      return new Promise((resolve) => {
        console.log("录音文件路径:", filePath);
        setTimeout(() => {
          resolve("你好，我需要帮助");
        }, 500);
      });
    },
    callDashScopeAPI(userInput) {
      const requestBody = {
        model: "qwen-plus",
        messages: [
          {
            role: "system",
            content: "You are a helpful assistant."
          },
          {
            role: "user",
            content: userInput
          }
        ]
      };
      common_vendor.index.request({
        url: this.apiUrl,
        method: "POST",
        header: {
          "Content-Type": "application/json",
          "Authorization": "Bearer YOUR_API_KEY"
          // 替换为您的API密钥
        },
        data: requestBody,
        success: (res) => {
          common_vendor.index.hideLoading();
          if (res.statusCode === 200 && res.data && res.data.choices && res.data.choices.length > 0) {
            const assistantMessage = res.data.choices[0].message.content;
            this.addMessage("assistant", assistantMessage);
            this.textToSpeech(assistantMessage);
          } else {
            common_vendor.index.showToast({
              title: "获取回复失败",
              icon: "none"
            });
            console.error("API响应错误:", res);
          }
        },
        fail: (err) => {
          common_vendor.index.hideLoading();
          common_vendor.index.showToast({
            title: "网络请求失败",
            icon: "none"
          });
          console.error(err);
        }
      });
    },
    addMessage(role, content) {
      this.messages.push({ role, content });
      this.$nextTick(() => {
        this.scrollTop = 9999;
      });
    },
    textToSpeech(text) {
      common_vendor.index.request({
        url: "您的文本转语音服务URL",
        method: "POST",
        data: { text },
        success: (res) => {
          if (res.statusCode === 200 && res.data && res.data.audio_url) {
            this.innerAudioContext.src = res.data.audio_url;
            this.innerAudioContext.play();
          }
        }
      });
    }
  }
};
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  return {
    a: common_vendor.f($data.messages, (message, index, i0) => {
      return {
        a: common_vendor.t(message.content),
        b: index,
        c: common_vendor.n(message.role)
      };
    }),
    b: $data.scrollTop,
    c: common_vendor.t($data.isRecording ? "松开结束" : "按住说话"),
    d: $data.isRecording ? 1 : "",
    e: common_vendor.o((...args) => $options.startRecording && $options.startRecording(...args)),
    f: common_vendor.o((...args) => $options.stopRecording && $options.stopRecording(...args))
  };
}
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
wx.createPage(MiniProgramPage);
