import type { SysCallMapping } from "../../plugos/system.ts";
import {
  performLocalFetch,
  ProxyFetchRequest,
  ProxyFetchResponse,
} from "../../common/proxy_fetch.ts";
import type { Client } from "../client.ts";
import {
  base64Decode,
  base64Encode,
} from "../../plugos/asset_bundle/base64.ts";

export function sandboxFetchSyscalls(
  client: Client,
): SysCallMapping {
  return {
    "sandboxFetch.fetch": async (
      _ctx,
      url: string,
      options?: ProxyFetchRequest,
    ): Promise<ProxyFetchResponse> => {
      // console.log("Got sandbox fetch ", url, op);
      url = url.replace(/^https?:\/\//, "");
      const fetchOptions = options
        ? {
          method: options.method,
          headers: options.headers,
          body: options.base64Body && base64Decode(options.base64Body),
        }
        : {};
      if (!client.remoteSpacePrimitives) {
        // No SB server to proxy the fetch available so let's execute the request directly
        return performLocalFetch(url, fetchOptions);
      }
      const resp = await client.remoteSpacePrimitives.authenticatedFetch(
        `${client.remoteSpacePrimitives.url}/!${url}`,
        fetchOptions,
      );
      const body = await resp.arrayBuffer();
      return {
        ok: resp.ok,
        status: resp.status,
        headers: Object.fromEntries(resp.headers.entries()),
        base64Body: base64Encode(new Uint8Array(body)),
      };
    },
  };
}
