import { ConvertToBase64WhenRelease } from "@hitplay/ads_common";
import { ConvertResourceType, Template3d } from "@hitplay/playable_template";
import { QuarksLoader } from "three.quarks";

export const vfx_json: ConvertResourceType = {
  type: "vfx_json",
  resources: [
    {
      name: "HitEffect",
      value: ConvertToBase64WhenRelease(
        "resources/vfx/files/VFX_Lootable_Hit.json",
      ),
    },
    {
      name: "DestroyEffect",
      value: ConvertToBase64WhenRelease(
        "resources/vfx/files/VFX_Lootable_Destroy.json",
      ),
    },
  ],
  loader: quarksLoader,
};

export function quarksLoader(base64String: string) {
  return new Promise((resolve, reject) => {
    try {
      new QuarksLoader(Template3d.manager).parse(
        JSON.parse(atob(base64String.split(",")[1])),
        (obj) => {
          resolve({ obj });
        },
      );
    } catch (error) {
      reject("Error loading vfx: " + error);
    }
  });
}
