import { resolve } from "path";
import AbortablePromise from "abortable-gen-promises";
import { deferedSpawn } from "async-spawner";
import tempy from "tempy";

export default function perspectiveShift(
  source_image: string,
  reverse_transform_parameters: number[],
  output_image: string = tempy.file({ extension: "jpg" })
): AbortablePromise<any> {
  return new AbortablePromise(async function* (await_or_abort) {
    const def = await deferedSpawn("python", [
      resolve(__dirname, "../main.py"),
      source_image,
      reverse_transform_parameters.join(","),
      output_image,
    ]);
    yield;
    return await_or_abort(def.waitForAnswer(), () => {
      def.kill();
    });
  });
}
