import { Service } from '@zenview/micro-basic-library';

export function formatAppConfig(appConfig) {
  const dataMap = {};
  appConfig.forEach((item) => {
    const urls = item.url.split(',');
    dataMap[item.alias] = {
      id: item.id,
      name: item.alias,
      script: `${urls[0]}?version=${item.version}`,
      style: `${urls[1]}?version=${item.version}`,
      appTitle: item.name,
      appType: item.type,
      version: item.version,
      status: item.status,
      describe: item.describe,
      tag: item.tag,
      group: item.group,
    };
  });
  return dataMap;
}

export async function querySystemConfig(mode) {
  if (mode === 'local') {
    return Service.$http({ url: `/micro-apps/micro-dep-librarys/config/systemConfig.json?${Date.now()}`, requestId: 'systemConfig' });
  }
  const res1 = await Service.$http({ url: '/api/micro/configure/v1/queryConfigureIsDefaultByType/1', requestId: 'systemConfig' }).catch(() => false);
  if (res1 && res1.data && res1.data.info) {
    return res1.data.info;
  }
  return Service.$http({ url: `/micro-apps/micro-dep-librarys/config/systemConfig.json?${Date.now()}`, requestId: 'systemConfig' });
}

export async function queryThemeConfig(mode) {
  if (mode === 'local') {
    return Service.$http({ url: `/micro-apps/micro-dep-librarys/config/themeConfig.json?${Date.now()}`, requestId: 'themeConfig' });
  }
  const res1 = await Service.$http({ url: '/api/micro/configure/v1/queryConfigureIsDefaultByType/2', requestId: 'themeConfig' }).catch(() => false);
  if (res1 && res1.data && res1.data.info) {
    return res1.data.info;
  }
  return Service.$http({ url: `/micro-apps/micro-dep-librarys/config/themeConfig.json?${Date.now()}`, requestId: 'themeConfig' });
}

export async function queryMapConfig(mode) {
  if (mode === 'local') {
    return Service.$http({ url: `/micro-apps/micro-dep-librarys/config/map.json?${Date.now()}`, requestId: 'mapConfig' });
  }
  const res1 = await Service.$http({ url: '/api/micro/configure/v1/queryConfigureIsDefaultByType/3', requestId: 'mapConfig' }).catch(() => false);
  if (res1 && res1.data && res1.data.info) {
    return res1.data.info;
  }
  return Service.$http({ url: `/micro-apps/micro-dep-librarys/config/map.json?${Date.now()}`, requestId: 'mapConfig' });
}

export async function queryMicroApplicationList(mode) {
  if (mode === 'local') {
    return Service.$http({ url: `/micro-apps/micro-dep-librarys/config/appConfig.json?${Date.now()}`, requestId: 'appConfig' });
  }
  const res = await Service.$http({ url: `/api/micro/microApplication/v1/queryMicroApplicationList`, requestId: 'appConfig' });
  return res.data;
}

export async function queryLogoConfig() {
  return Service.$http({ url: `/micro-apps/micro-dep-librarys/config/logConfig.json`, requestId: 'queryLogoConfig' }).catch(() => ({}));
}

export async function queryZenMapConfig() {
  const res1 = await Service.$http({ url: '/api/micro/configure/v1/queryConfigureIsDefaultByType/4', requestId: 'mapConfig' }).catch(() => false);
  if (res1 && res1.data && res1.data.info) {
    return res1.data.info;
  }
}

export async function queryZenMap3DConfig() {
  const res1 = await Service.$http({ url: '/api/micro/configure/v1/queryConfigureIsDefaultByType/5', requestId: 'mapConfig' }).catch(() => false);
  if (res1 && res1.data && res1.data.info) {
    return res1.data.info;
  }
}
