import { UpdateServerRequestModule } from '@/api/RequestModules';
import { LogUtils } from '@imengyu/imengyu-utils';
import { DataModel } from '@imengyu/js-request-transform';
import DefaultConfiguration from './DefaultConfiguration.json';

export const CommonConfigurationConfig = {
  /**
   * 应用id
   */
  appId: 4,
  appConfigId: 14,
}

export interface IConfigurationItem {
  banners: {
    homeTop: string;
    homeTitle: string;
    home: string[];
    dig: string[];
    discover: string[];
  }
}

export class ConfigurationApi extends UpdateServerRequestModule<DataModel> {

  constructor() {
    super();
  }

  /**
   * 获取当前配置，有缓存，会根据激活的历史版本获取对应配置
   * @returns 
   */
  async getConfig() {
    try {
      return (await this.get<{
        data: IConfigurationItem
      }>('/app-configuration-get', '获取配置', {
        name: CommonConfigurationConfig.appConfigId,
        appId: CommonConfigurationConfig.appId,
        })).data!.data;
    } catch (error) {
      LogUtils.printLog("ConfigurationApi", 'error', '获取配置失败，使用默认配置', error);
      return DefaultConfiguration as IConfigurationItem;
    }
  }
}

export default new ConfigurationApi();
