import { templateRender } from "@vue-email/compiler";
import { useRuntimeConfig, useStorage } from "#imports";
const storageKey = "assets:emails";
export async function useCompiler(filename, data, verbose = false) {
  const vueEmailOptions = useRuntimeConfig().public.vueEmail;
  const source = await useStorage(storageKey).getItem(filename);
  const keys = await useStorage(storageKey).getKeys();
  const components = [];
  for (const key of keys) {
    const value = await useStorage(storageKey).getItem(key);
    if (value && key.endsWith(".vue")) {
      components.push({
        name: key,
        source: value
      });
    }
  }
  if (!source)
    throw new Error(`Template ${filename} not found`);
  const template = await templateRender(
    filename,
    { source, components },
    data,
    {
      verbose,
      options: {
        baseUrl: vueEmailOptions?.baseUrl,
        i18n: vueEmailOptions?.i18n,
        tailwind: vueEmailOptions?.tailwind
      }
    }
  );
  return template;
}
