import CrmService from 'core/services/CrmService';
import { IAccount, ILead } from 'core/services/CrmService/types/entities';
import { ICalculationStore } from 'core/types/Calculation/Store';
import { gql } from 'graphql.macro';

export function fillIndAgent(this: ICalculationStore, lead?: ILead) {
  if (lead?.evo_agent_accountid) {
    CrmService.getCRMOptions<'accountOptions', IAccount>({
      query: gql`
        query selectIndAgent($evo_agent_accountid: Uuid!) {
          accountOptions: account(accountid: $evo_agent_accountid) {
            accountid
            name
          }
        }
      `,
      variables: {
        evo_agent_accountid: lead.evo_agent_accountid,
      },
    }).then(({ accountOptions }) => {
      if (accountOptions && accountOptions.length > 0) {
        this.setOptions('selectIndAgent', accountOptions);
        this.setValue('indAgent', accountOptions[0].accountid);
      } else {
        this.setValue('indAgent', null);
        this.setOptions('selectIndAgent', []);
      }
    });
  } else {
    this.setValue('indAgent', null);
    this.setOptions('selectIndAgent', []);
  }
}

export function fillCalcDoubleAgent(this: ICalculationStore, lead?: ILead) {
  if (lead?.evo_double_agent_accountid) {
    CrmService.getCRMOptions<'accountOptions', IAccount>({
      query: gql`
        query selectCalcDoubleAgent($evo_double_agent_accountid: Uuid!) {
          accountOptions: account(accountid: $evo_double_agent_accountid) {
            accountid
            name
          }
        }
      `,
      variables: {
        evo_double_agent_accountid: lead.evo_double_agent_accountid,
      },
    }).then(({ accountOptions }) => {
      if (accountOptions && accountOptions.length > 0) {
        this.setOptions('selectCalcDoubleAgent', accountOptions);
        this.setValue('calcDoubleAgent', accountOptions[0].accountid);
      } else {
        this.setValue('calcDoubleAgent', null);
        this.setOptions('selectCalcDoubleAgent', []);
      }
    });
  } else {
    this.setValue('calcDoubleAgent', null);
    this.setOptions('selectCalcDoubleAgent', []);
  }
}

export function fillCalcBroker(this: ICalculationStore, lead?: ILead) {
  if (lead?.evo_broker_accountid) {
    CrmService.getCRMOptions<'accountOptions', IAccount>({
      query: gql`
        query selectCalcBroker($evo_broker_accountid: Uuid!) {
          accountOptions: account(accountid: $evo_broker_accountid) {
            accountid
            name
          }
        }
      `,
      variables: {
        evo_broker_accountid: lead.evo_broker_accountid,
      },
    }).then(({ accountOptions }) => {
      if (accountOptions && accountOptions.length > 0) {
        this.setOptions('selectCalcBroker', accountOptions);
        this.setValue('calcBroker', accountOptions[0].accountid);
      }
    });
  } else {
    this.setValue('calcBroker', null);
    this.setOptions('selectCalcBroker', []);
  }
}

export function fillFinDepartment(this: ICalculationStore, lead?: ILead) {
  if (lead?.evo_fin_department_accountid) {
    CrmService.getCRMOptions<'accountOptions', IAccount>({
      query: gql`
        query selectCalcFinDepartment($evo_fin_department_accountid: Uuid!) {
          accountOptions: account(accountid: $evo_fin_department_accountid) {
            accountid
            name
          }
        }
      `,
      variables: {
        evo_fin_department_accountid: lead.evo_fin_department_accountid,
      },
    }).then(({ accountOptions }) => {
      if (accountOptions && accountOptions.length > 0) {
        this.setOptions('selectCalcFinDepartment', accountOptions);
        this.setValue('calcFinDepartment', accountOptions[0].accountid);
      }
    });
  } else {
    this.setValue('calcFinDepartment', null);
    this.setOptions('selectCalcFinDepartment', []);
  }
}
