<%_
const pkType = getPkType(databaseType) === 'Long' ? 'number' : 'string';
const namedImports = [
    `Create${entityAngularName}GQL`,
    `Get${entityNamePlural}GQL`,
    `Get${entityAngularName}GQL`,
    `Delete${entityAngularName}GQL`,
    `Update${entityAngularName}GQL`
];
let importSpecifier;
let createVars, updateVars;
if (typeDefinition === 'TypeScript') {
    createVars = `Create${entityAngularName}Vars`;
    updateVars = `Update${entityAngularName}Vars`;
    importSpecifier = `../${entityFileName}.gql`;
} else {
    createVars = `MutationCreate${entityAngularName}Args`;
    updateVars = `MutationUpdate${entityAngularName}Args`;
    importSpecifier = '../../../graphql';
}
namedImports.push(createVars);
namedImports.push(updateVars);
_%>
import { Injectable } from '@angular/core';
import { HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { I<%= entityAngularName %>, get<%= entityAngularName %>Identifier } from 'app/entities/<%= entityFileName %>/<%= entityFileName %>.model';
import { GraphQLUtils } from 'app/core/util/graphql-util.service';
import { <%= namedImports.join(', ') %> } from '<%= importSpecifier %>';
import { isPresent } from 'app/core/util/operators';

export type EntityResponseType = HttpResponse<I<%= entityAngularName %>>;
export type EntityArrayResponseType = HttpResponse<I<%= entityAngularName %>[]>;

@Injectable({ providedIn: 'root' })
export class <%= entityAngularName %>GraphQLService {

  constructor(private graphQLUtils: GraphQLUtils,
              private create<%= entityAngularName %>GQL: Create<%= entityAngularName %>GQL,
              private update<%= entityAngularName %>GQL: Update<%= entityAngularName %>GQL,
              private get<%= entityNamePlural %>GQL: Get<%= entityNamePlural %>GQL,
              private get<%= entityAngularName %>GQL: Get<%= entityAngularName %>GQL,
              private delete<%= entityAngularName %>GQL: Delete<%= entityAngularName %>GQL) {}

  query(req?: any): Observable<EntityArrayResponseType> {
    return this.get<%= entityNamePlural %>GQL
        .fetch(this.graphQLUtils.createGraphQlOption(req), { fetchPolicy: req?.bypassCache ? 'no-cache' : 'cache-first' })
        .pipe(map(result => this.graphQLUtils.toPagedHttpResponse(result.data.result)));
  }

  create(<%= entityInstance %>: I<%= entityAngularName %>): Observable<EntityResponseType> {
    return this.create<%= entityAngularName %>GQL
        .mutate({<%= entityInstance %>} as <%= createVars %>)
        .pipe(map(result => this.graphQLUtils.toHttpResponse(result)));
  }

  delete(_id: string): Observable<HttpResponse<{}>> {
    const id = this.getId(_id);
    return this.delete<%= entityAngularName %>GQL.mutate({id}).pipe(map(result => this.graphQLUtils.toHttpResponse(result)));
  }

  find(_id: string): Observable<EntityResponseType> {
    const id = this.getId(_id);
    return this.get<%= entityAngularName %>GQL.fetch({ id }).pipe(map(result => this.graphQLUtils.toHttpResponse(result)));
  }

  partialUpdate(<%= entityInstance %>: I<%= entityAngularName %>): Observable<EntityResponseType> {
    return this.update(<%= entityInstance %>);
  }

  update(<%= entityInstance %>: I<%= entityAngularName %>): Observable<EntityResponseType> {
    return this.update<%= entityAngularName %>GQL
        .mutate({<%= entityInstance %> } as <%= updateVars %>)
        .pipe(map(result => this.graphQLUtils.toHttpResponse(result)));
  }

  add<%= entityAngularName %>ToCollectionIfMissing(<%= entityInstance %>Collection: I<%= entityAngularName %>[], ...<%= entityInstancePlural %>ToCheck: (I<%= entityAngularName %> | null | undefined)[]): I<%= entityAngularName %>[] {
    const <%= entityInstancePlural %>: I<%= entityAngularName %>[] = <%= entityInstancePlural %>ToCheck.filter(isPresent);
    if (<%= entityInstancePlural %>.length > 0) {
        const <%= entityInstance %>CollectionIdentifiers = <%= entityInstance %>Collection.map(<%= entityInstance %>Item => get<%= entityAngularName %>Identifier(<%= entityInstance %>Item)!);
        const <%= entityInstancePlural %>ToAdd = <%= entityInstancePlural %>.filter(<%= entityInstance %>Item => {
            const <%= entityInstance %>Identifier = get<%= entityAngularName %>Identifier(<%= entityInstance %>Item);
            if (<%= entityInstance %>Identifier == null || <%= entityInstance %>CollectionIdentifiers.includes(<%= entityInstance %>Identifier)) {
                return false;
            }
            <%= entityInstance %>CollectionIdentifiers.push(<%= entityInstance %>Identifier);
            return true;
        });
        return [...<%= entityInstancePlural %>ToAdd, ...<%= entityInstance %>Collection];
    }
    return <%= entityInstance %>Collection;
  }

  private getId(id: string): <%= pkType %> {
    <%_ if(pkType === 'number') { _%>
    return parseInt(id, 10);
    <%_ } else { _%>
    return id;
    <% } _%>
  }
}
