import { App, inject, InjectionKey } from 'vue';

export const RuntimeEnv = Symbol() as InjectionKey<any>;

export const fetchRuntimeEnv = async <T>(): Promise<T> => {
  return await fetch('/env.json').then((res) => res.json());
};

export const useRuntimeEnv = <T>(): T => {
  const env = inject(RuntimeEnv);
  return env;
};

export const createRuntimeEnv = <T>(env: T) => ({
  install: (app: App) => {
    app.config.globalProperties.$env = env;
    app.provide(RuntimeEnv, env);
  },
});
