import { download } from './io.js';

// Starting a browser download does not prove that the user saved the file.
export function attachDownloadFeedback(list, getResults) {
  list.addEventListener('click', (event) => {
    const button = event.target.closest('[data-download]');
    if (!button || !list.contains(button)) return;
    const item = getResults()[Number(button.dataset.download)];
    if (!item) return;
    const message = button.parentElement.querySelector('.download-status');
    try {
      download(item.blob, item.name);
      message.textContent = '已请求下载,请在浏览器下载记录中确认。';
      message.className = 'download-status';
    } catch {
      message.textContent = '未能开始下载,请重试。当前结果仍保留。';
      message.className = 'download-status error';
    }
  });
}

export function saveHint(count) {
  return `结果保存在当前页面中,请下载后再关闭或刷新。${count > 1 ? '多个文件请逐个下载。' : ''}`;
}