export type ExportFlowStatus = "composition_failed" | "download_failed" | "downloaded_not_saved" | "downloaded_and_saved";

export const exportResultCopy: Record<ExportFlowStatus, string> = {
  composition_failed: "合成失败，请返回编辑器检查",
  download_failed: "浏览器下载失败，可重新下载同一次合成结果",
  downloaded_not_saved: "文件已下载，但未保存为最新成品",
  downloaded_and_saved: "已下载并保存为最新成品",
};

export async function runExportFlow(input: {
  compose: () => Promise<Blob>;
  download: (blob: Blob) => Promise<void>;
  persist: (blob: Blob) => Promise<void>;
}): Promise<{ blob: Blob | null; status: ExportFlowStatus }> {
  let blob: Blob;
  try {
    blob = await input.compose();
  } catch {
    return { blob: null, status: "composition_failed" };
  }
  try {
    await input.download(blob);
  } catch {
    return { blob, status: "download_failed" };
  }
  try {
    await input.persist(blob);
    return { blob, status: "downloaded_and_saved" };
  } catch {
    return { blob, status: "downloaded_not_saved" };
  }
}
