
import { ElementsNames } from 'client/Containers/Calculation/types/elements';
import { TableValuesNames } from 'client/Containers/Calculation/types/tables';
import UserStore from 'client/stores/UserStore';
import CrmService from 'core/services/CrmService';
import {
  insuranceOptionsQuery,
  mainOptionsQuery,
  ownerOptionsQuery
} from 'core/services/CrmService/graphql/query/options';
import {
  staticDataQuery,
  systemUserQuery
} from 'core/services/CrmService/graphql/query/staticData';
import { CRMEntity, CRMEntityNames, IAccount, ILead, IOpportunity, ISystemUser } from 'core/services/CrmService/types/entities';
import {
  insuranceVariables,
  mainOptionsVariables,
  staticDataVariables
} from './variables';


export function composeRequests() {
  const mainOptionsRequest = CrmService.getCRMOptions<ElementsNames, CRMEntity>(
    {
      query: mainOptionsQuery,
      variables: mainOptionsVariables,
    },
  );

  const staticDataRequest = CrmService.getCRMEntities<
    CRMEntityNames,
    CRMEntity[]
  >({
    query: staticDataQuery,
    variables: staticDataVariables,
  });

  const domainname = UserStore.getDomainName();

  const systemUserRequest = CrmService.getCRMEntities<
    'systemuser',
    ISystemUser
  >({
    query: systemUserQuery,
    variables: { domainname },
  });

  const ownerOptionsRequest = CrmService.getCRMOptions<
    ElementsNames,
    ILead & IOpportunity
  >({
    query: ownerOptionsQuery,
    variables: {
      statecode: 0,
      domainname,
    },
  });

  const insuranceRequest = CrmService.getCRMOptions<TableValuesNames, IAccount>(
    {
      query: insuranceOptionsQuery,
      variables: insuranceVariables,
    },
  );

  return {
    mainOptionsRequest,
    staticDataRequest,
    systemUserRequest,
    insuranceRequest,
    ownerOptionsRequest,
  };
}
