"use strict";

const { newPublishPage } = require("./publish_browser.js");
const { publishToDouyin } = require("./publish_to_douyin.js");

const PLATFORM_LABELS = {
  douyin: "抖音",
  kuaishou: "快手",
  shipin: "视频号",
  xiaohongshu: "小红书",
};

async function publishToPlatform(platform, accountId, cookies, params) {
  const label = PLATFORM_LABELS[platform] || platform;
  if (platform === "douyin") {
    const page = await newPublishPage(platform, accountId, cookies);
    try {
      return await publishToDouyin(page, params);
    } finally {
      if (!params.keepPageOpen) {
        try {
          await page.close();
        } catch {
          /* ignore */
        }
      }
    }
  }
  return {
    success: false,
    status: "failed",
    message: `${label}自动发布即将支持，请先使用抖音`,
  };
}

module.exports = { publishToPlatform, PLATFORM_LABELS };
