import { QueryOptions } from '@apollo/client';
import axios from 'axios';
import { CRM_PROXY_URL } from 'core/constants/urls';
import GQLClient from './graphql/client';
import { optionizeEntities } from './tools/entity';
import { IQuote } from './types/entities';

export default class {
  public static getCRMEntities<K extends string, R>(
    queryOptions: QueryOptions<
      Partial<Record<keyof R, any>> | Record<string, any>,
      Record<K, R>
    >,
  ) {
    return GQLClient.query<
      Record<K, R>,
      Partial<Record<keyof R, any> | Record<string, any>>
    >(queryOptions).then(res => res.data);
  }

  public static getCRMOptions<K extends string, R>(
    queryOptions: QueryOptions<
      Partial<Record<keyof R, any>> | Record<string, any>,
      Record<K, R>
    >,
  ) {
    return this.getCRMEntities<K, R | R[]>(queryOptions).then(entities =>
      optionizeEntities<K, R>(entities),
    );
  }

  public static createKP(data) {
    return axios.post<IQuote>(
      String.prototype.concat(CRM_PROXY_URL, '/offer', '/leasingcalculator'),
      data,
    );
  }
}
