import fs from "fs";

export const title = "图片批量处理";

const IMAGE_EXTS = [".jpg", ".jpeg", ".png", ".webp", ".gif", ".tiff", ".avif"];

export function listImageFiles() {
  try {
    const cwd = process.cwd();
    const files = fs.readdirSync(cwd);
    return files.filter(file => {
      const ext = file.slice(file.lastIndexOf(".")).toLowerCase();
      return IMAGE_EXTS.includes(ext);
    });
  } catch (err) {
    console.error("读取文件列表失败:", err);
    return [];
  }
}

export function getSteps() {
  const files = listImageFiles();

  return [
    {
      name: "源文件",
      type: "multiselect",
      message: files.length ? "选择要处理的图片文件" : "当前目录未找到图片文件",
      options: files.map(file => ({ value: file, label: file })),
      default: [],
      emptyMessage: "请按 Esc 返回并放入图片文件后重试"
    },
    {
      name: "压缩",
      type: "select",
      message: "选择压缩质量",
      options: [
        { value: "skip", label: "跳过此操作" },
        { value: 90, label: "高质量 (90%)" },
        { value: 80, label: "标准 (80%)" },
        { value: 60, label: "中等 (60%)" },
        { value: 40, label: "低质量 (40%)" }
      ],
      default: "skip"
    },
    {
      name: "生成多尺寸",
      type: "select",
      message: "选择尺寸预设",
      options: [
        { value: "skip", label: "跳过此操作" },
        { value: "responsive", label: "响应式 (800/400/200)" },
        { value: "thumbnail", label: "缩略图 (300/150/75)" },
        { value: "large", label: "大图 (1920/1280/640)" }
      ],
      default: "skip"
    },
    {
      name: "统一背景",
      type: "select",
      message: "选择背景颜色",
      options: [
        { value: "skip", label: "跳过此操作" },
        { value: 0xFFFFFFFF, label: "白色" },
        { value: 0x000000FF, label: "黑色" },
        { value: 0xF5F5F5FF, label: "浅灰" },
        { value: 0x333333FF, label: "深灰" }
      ],
      default: "skip"
    },
    {
      name: "裁剪补边",
      type: "select",
      message: "选择目标画布尺寸",
      options: [
        { value: "skip", label: "跳过此操作" },
        { value: "1200x800", label: "1200×800 (3:2)" },
        { value: "1920x1080", label: "1920×1080 (16:9)" },
        { value: "1080x1080", label: "1080×1080 (1:1)" },
        { value: "800x600", label: "800×600 (4:3)" }
      ],
      default: "skip"
    }
  ];
}
