import { createEffect } from "solid-js";
import { listen } from "@tauri-apps/api/event";
import { getCurrentWindow } from "@tauri-apps/api/window";
import { sendImage } from "@network/index";

export const onSendImage = () => {
	let sentImage = "";

	createEffect(async () => {
		// Listen for PNG processing events
		const unlisten = listen("png-processed", async (event) => {
			const base64Data = event.payload as string;

			if (base64Data === sentImage) {
				return;
			}

			sentImage = base64Data;

			const appWindow = getCurrentWindow();

			appWindow.show();
			appWindow.setFocus();

			await sendImage("test-image.png", base64Data);
		});

		return () => {
			unlisten.then((fn) => fn()); // Cleanup listener
		};
	});
};
