export const syncToPC = async (gameId: string, title: string) => {
  try {
    if (!('showDirectoryPicker' in window)) {
      alert("Your browser does not support the File System Access API. Please use a modern Chromium-based browser.");
      return;
    }

    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const dirHandle = await (window as any).showDirectoryPicker();
    console.log(`Syncing ${title} (${gameId}) to`, dirHandle.name);
    
    // Placeholder logic for downloading unzipped files sequentially
    const fileHandle = await dirHandle.getFileHandle(`${title.replace(/[^a-z0-9]/gi, '_')}.rom`, { create: true });
    const writable = await fileHandle.createWritable();
    
    // In a real app, this would fetch the actual parts of the game
    // and stream it to the writable. For now, writing a dummy buffer.
    const dummyData = new TextEncoder().encode("DUMMY ROM DATA CONTENT - IMPLEMENT NATIVE FETCH HERE");
    await writable.write(dummyData);
    await writable.close();
    
    console.log(`Successfully synced ${title} to PC.`);
    alert(`Successfully synced ${title} to PC.`);
  } catch (err) {
    if (err instanceof Error && err.name === 'AbortError') {
      console.log('User cancelled the directory picker');
    } else {
      console.error('Failed to sync directory:', err);
      alert('Failed to sync to PC. See console for details.');
    }
  }
};
