import { LogUtils } from "@imengyu/imengyu-utils";
import { inject, provide, ref, type Ref } from "vue";
import { BaseAppServerRequestUrlManager } from "../BaseAppServerRequestModule";
import ConfigurationApi, { type IConfigurationItem } from "./ConfigurationApi";

export const APP_CONFIGURATION_KEY = Symbol('APP_CONFIGURATION_KEY');

const TAG = 'useAppConfiguration';

export function useAppConfiguration() {
  const appConfiguration = ref<IConfigurationItem | null>(null);

  async function loadAppConfiguration() {
    //获取配置
    const config = await ConfigurationApi.getConfig();
    LogUtils.printLog(TAG, 'info', '获取配置', config);
    appConfiguration.value = config;
    BaseAppServerRequestUrlManager.notifyUrlConfigUpdated('base-app-server', config.baseServerUrl);
  }

  provide(APP_CONFIGURATION_KEY, appConfiguration);

  return {
    loadAppConfiguration,
    appConfiguration,
  };
}

export function injectAppConfiguration() {
  return inject(APP_CONFIGURATION_KEY) as Ref<IConfigurationItem | null>;
}