import { AsrRelatime } from '../base.ts';
import fs from 'fs/promises';
import path from 'path';
import net from 'net';
import dotenv from 'dotenv';
dotenv.config();
// const videoTestPath = path.join(process.cwd(), 'videos/asr_example.wav');
// const videoTestPath = path.join(process.cwd(), 'videos/asr_example2.wav');
// const videoTestPath = path.join(process.cwd(), 'videos/tts_mix.mp3');
const videoTestPath = path.join(process.cwd(), 'videos/my_speech_text.wav');
const videoTestPath2 = path.join(process.cwd(), 'videos/asr_example2.wav');
const videoBlankPath = path.join(process.cwd(), 'videos/blank.wav');
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
// 使用示例
async function main() {
  const ws = new AsrRelatime({
    token: process.env.BAILIAN_API_KEY,
    onConnect: async () => {
      ws.emitter.on('message', (event) => {
        // console.log('message', event.data);
      });
      ws.emitter.on('result', ({ text }) => {
        console.log('result:', text);
      });
      ws.emitter.on('close', (event) => {
        const { code, reason, type } = event || {};
        console.log('Connection closed:', code, reason, type);
        if (typeof code === 'number' && code !== 0) {
          ws.reconnect({ timeout: 1000 });
        }
      });
      await ws.start();
      const audioContent = await fs.readFile(videoTestPath);

      ws.sendBuffer(audioContent);
      ws.sendBlank();
      ws.sendBuffer(await fs.readFile(videoTestPath2));
      ws.sendBlank();
    },
  });

  //
}

// 执行主函数
main().catch(console.error);

const server = net.createServer((socket) => {
  socket.on('data', (data) => {
    console.log('data', data);
  });
});
server.listen(10096);