"use strict";

/**
 * 音色一键复刻（对齐 Electron ipc aliyun:cosyvoice:enroll）
 */
const fs = require("fs");
const pipelineNative = require("./pipeline_native.js");
const desktopConfig = require("./desktop_config.js");

globalThis.__nodejsMain = async function main(params) {
  const p = params || {};
  const audioPath = String(p.audioPath || "").trim();
  const name = String(p.name || "").trim();
  if (!audioPath || !fs.existsSync(audioPath)) {
    return { success: false, error: "请先录制或上传参考声音" };
  }
  if (!name) {
    return { success: false, error: "请先输入音色名称" };
  }

  const apiKey =
    desktopConfig.get("BAILIAN_API_KEY") ||
    desktopConfig.get("DASHSCOPE_API_KEY");
  if (!apiKey) {
    return {
      success: false,
      error: "缺少 BAILIAN_API_KEY（或 DASHSCOPE_API_KEY），请在配置中设置",
    };
  }

  return pipelineNative.qwenVoiceEnroll({ apiKey, audioPath, name });
};
