import { onlyId } from "@/utils/shared";
import { useInstanceProps } from "./use-global-vars";

export const useResourceHandler = () => {
  const handler = useInstanceProps().get().handlerResource;
  const mateMap: Record<string, string> = {
    svg: "image/svg+xml",
  };

  return async (data: string | Blob | File, type?: string) => {
    type = type && mateMap[type!] ? mateMap[type!] : type
    if (typeof data === "string") {
      data = new Blob([data], { type });
    }
    if (!(data instanceof File)) {
      const entrie = Object.entries(mateMap).find(([k, v]) => v === type)
      data = new File([data], onlyId() + (entrie ? `.${entrie![0]}` : ''))
    }
    return await handler(data as File)
  };
};
