# Patched Nextcloud Talk Desktop with Wayland screen sharing support
# Applies the core change from upstream draft PR #1022:
# https://github.com/nextcloud/talk-desktop/pull/1022
#
# Patches the webpack bundle in app.asar to add setDisplayMediaRequestHandler
# with useSystemPicker: true, enabling native PipeWire/portal-based
# screen sharing on Wayland (Sway, Hyprland, etc.)
{ lib
, nextcloud-talk-desktop
, nodejs
, asar
}:

nextcloud-talk-desktop.overrideAttrs (old: {
  pname = "nextcloud-talk-desktop-patched";

  nativeBuildInputs = (old.nativeBuildInputs or []) ++ [ asar nodejs ];

  # Patch the asar after the main installPhase creates the output
  postFixup = (old.postFixup or "") + ''
    echo "Patching app.asar for Wayland screen sharing..."
    ASAR_PATH="$out/opt/Nextcloud Talk-linux-x64/resources/app.asar"

    WORK=$(mktemp -d)
    asar extract "$ASAR_PATH" "$WORK/app"

    # In the webpack bundle:
    #   session = l, desktopCapturer = a, app = n
    # We inject setDisplayMediaRequestHandler right after n.whenReady().then((async()=>{
    # useSystemPicker: true makes Electron use the native system picker
    # (PipeWire/xdg-desktop-portal on Wayland)
    node -e "
      const fs = require('fs');
      const p = '$WORK/app/.webpack/main/index.js';
      let c = fs.readFileSync(p, 'utf8');

      if (c.includes('setDisplayMediaRequestHandler')) {
        console.log('Already patched');
        process.exit(0);
      }

      const marker = 'n.whenReady().then((async()=>{';
      const idx = c.indexOf(marker);
      if (idx === -1) {
        console.error('ERROR: Could not find whenReady marker in webpack bundle');
        process.exit(1);
      }

      // Inject after the marker
      const injection = 'l.defaultSession.setDisplayMediaRequestHandler(async(e,t)=>{const s=await a.getSources({types:[\"screen\",\"window\"]});s.length>0?t({video:s[0]}):t({})},{useSystemPicker:!0});';

      c = c.slice(0, idx + marker.length) + injection + c.slice(idx + marker.length);
      fs.writeFileSync(p, c, 'utf8');
      console.log('Successfully patched main bundle for Wayland screen sharing');
    "

    asar pack "$WORK/app" "$ASAR_PATH"
    rm -rf "$WORK"
  '';
})
