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

export const CommonConfigurationConfig = {
  /**
   * 应用id
   */
  appId: 2,
  appConfigId: {
    dev: 7,
    prod: 6,
  },
  getAppConfigId(isDev: boolean) {
    return isDev ? this.appConfigId.dev : this.appConfigId.prod;
  },
}

export interface IConfigurationItem {
  baseServerUrl: string,
  articleMark: string,
  collectSignYear: number,
  collectFormYear: number,
  routeListImage: string,
  routeListImageStyle: Record<string, any>,
  routeListMarginTop: number,
}

export class ConfigurationApi extends UpdateServerRequestModule<DataModel> {

  constructor() {
    super();
  }

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

export default new ConfigurationApi();
