import lodash from 'lodash';
import { fetchExtensionInfo } from '@/api/autoshopee/cms';
import { loggerBackground } from '@/plugins/logger';

export const getConfigService = async () => {
  const configService = {
    connectApp: false,
    connectWorker: false,
    fetchUrl: false,
    fetchListenerActive: false,
    fetchListenerLimit: 0,
  };

  try {
    const extensionInfo = await fetchExtensionInfo(false);

    configService.connectApp = lodash.get(extensionInfo, 'workerService.socketApp.active', false);
    configService.connectWorker = lodash.get(extensionInfo, 'workerService.socketWorker.active', false);
    configService.fetchUrl = lodash.get(extensionInfo, 'workerService.fetchUrl.active', false);
    configService.fetchListenerActive = lodash.get(extensionInfo, 'workerService.fetchListener.active', false);
    configService.fetchListenerLimit = lodash.get(extensionInfo, 'workerService.fetchListener.limit', 0);
  } catch (error) {
    loggerBackground.error({
      msg: 'GET_CONFIG_SERVICE_ERROR',
      error,
    });
  }

  return configService;
};
