import 'dart:typed_data';

import 'package:universal_html/html.dart' as html;

import 'package:pshared/models/file/downloaded_file.dart';


Future<void> downloadFile(DownloadedFile file) async {
  final blob = html.Blob(
    [Uint8List.fromList(file.bytes)],
    file.mimeType,
  );

  final url = html.Url.createObjectUrlFromBlob(blob);

  final anchor = html.AnchorElement(href: url)
    ..download = file.filename
    ..style.display = 'none';

  html.document.body!.append(anchor);
  anchor.click();
  anchor.remove();

  html.Url.revokeObjectUrl(url);
}
