import React__default, { useState, useEffect } from 'react';
import { Service, Dict, LMConfig } from '@zenview/micro-basic-library';

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

function InitialConfig({
  children,
  mode = 'server'
}) {
  const [state, setState] = useState({
    isInit: false,
    isUpdate: false
  });
  Dict.useTypeCodes();
  useEffect(() => {
    Promise.all([querySystemConfig(mode), queryMapConfig(mode), queryThemeConfig(mode), queryMicroApplicationList(mode), queryLogoConfig(), queryZenMapConfig(), queryZenMap3DConfig()]).then(res => {
      const [BSConfig, LMapConfig, themeConfig, AppConfig, LogConfig, ZenMapConfig, ZenMap3DConfig] = res;
      LMConfig.registerBSConfig(BSConfig);
      LMConfig.registerLMapConfig(LMapConfig);
      LMConfig.registerAppConfig(formatAppConfig(AppConfig));
      LMConfig.registerThemeConfig(themeConfig);
      LMConfig.registerLogConfig(LogConfig);
      LMConfig.registerCustomConfig({
        ZenMap: ZenMapConfig,
        ZenMap3D: ZenMap3DConfig
      });
      setState(() => ({
        isUpdate: BSConfig.isUpdate,
        isInit: true
      }));
    });
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
  const {
    isInit,
    isUpdate
  } = state;
  if (!isInit) {
    return null;
  }
  return /*#__PURE__*/React__default.createElement(React__default.Fragment, null, children(isUpdate));
}

export { InitialConfig as default };
//# sourceMappingURL=index.js.map
