/* eslint-disable prettier/prettier */
/* eslint-disable @typescript-eslint/no-inferrable-types */


import { HttpService } from "@nestjs/axios";
import { Observable } from "rxjs";
import { QueryClient } from "./QueryClient";

export class RegularClientQuery extends QueryClient {

  clientInstance: any
  private http: HttpService = new HttpService();
  constructor() {
    super();
  }

  sendQuery(queryString: string): Observable<any> {

    let queryObservable: Observable<any>;

    queryObservable = new Observable((subscriber) => {

      const obs = this.http.post(`${process.env.GRAPHQL_HOST}/graphql`, { query: `${queryString}` })
      
      obs.subscribe({
        next: (x) => {
          Object.keys(x.data.data).forEach(y => {
            Object.keys(x.data.data[y]).forEach(z => {
              subscriber.next(x.data.data[y][z])

            })
          })

          subscriber.complete();
        },
        error: (e) => {
          subscriber.error("error subscribe graphql " + e)
        },
        complete: () => {
          subscriber.complete();
        }
      })

    });
    return queryObservable;
  }

  setCacheOn() {
    throw new Error('No cache feature implemented for regular client.');
  }

  setCacheOff() {
    throw new Error('No cache feature implemented for regular client.');
  }
}

