import 'dart:async';
import 'dart:js_interop';
import 'dart:typed_data';

import 'package:web/web.dart' as web;

Future<bool> openPdfBytes(Uint8List bytes) async {
  final blob = web.Blob(
    [bytes.toJS].toJS,
    web.BlobPropertyBag(type: 'application/pdf'),
  );
  final url = web.URL.createObjectURL(blob);
  final openedWindow = web.window.open(url, '_blank', 'noopener,noreferrer');
  if (openedWindow == null) {
    web.URL.revokeObjectURL(url);
    return false;
  }
  // Revoke later to avoid revoking before the new tab has loaded the blob.
  Future<void>.delayed(const Duration(seconds: 30), () {
    web.URL.revokeObjectURL(url);
  });
  return true;
}