import {ICellRendererParams} from "ag-grid-community";
import {GridCellValueRenderer} from "@webbpm/base-package";

export class FileDownloadCellRenderer implements GridCellValueRenderer {
  render(params: ICellRendererParams): HTMLElement {
    const container = document.createElement('div');
    container.className = 'download-cell-renderer';

    const button = document.createElement('button');
    button.className = 'download-btn';
    button.innerHTML = '<i class="fa fa-download"></i>';
    button.title = params.data.fileName || 'Скачать файл';

    if (!params.value) {
      button.style.display = 'none';
    }

    button.addEventListener('click', () => {
      event.stopPropagation();

      const fileId = params.data.fileId;
      if (!fileId) {
        return;
      }

      const parentComponent = params.context.parentComponent;
      if (parentComponent) {
        parentComponent.downloadFile(fileId);
      }

      button.blur();
    });

    container.appendChild(button);
    return container;
  }

}