import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { SERVICES } from "../../constants";
import { MessageTemplateModel } from "../../models/messageTemplate.model";
import { SearchService } from '../../oneit/services/search.service';
import { UtilsService } from '../../oneit/services/utils.service';

@Injectable()
export class EditWorkflowTemplateService {

    constructor(
        private searchService: SearchService,
        private utilsService: UtilsService
    ) {
    }

    getMessageTemplates(assocs, queryParam): Observable<Array<MessageTemplateModel>> {
        return this.searchService.getObjects(SERVICES.MESSAGE_TEMPLATES, 'All', queryParam, assocs, 'Autocomplete', null, null, null)
            .map(
                data => this.utilsService.convertResponseToObjects(data, assocs)
            );
    }

    getmessagetemplate_withdrawsForAutocomplete(searchTerm, assocs): Observable<any> {
        return this.searchService.getObjects('', 'All', {}, assocs, null, null, null, null)
            .map(
                data =>
                    this.utilsService.convertResponseToObjects(data, assocs)
            );
    }

    getmessagetemplatesForAutocomplete(searchTerm, assocs): Observable<any> {
        return this.searchService.getObjects('', 'All', {}, assocs, null, null, null, null)
            .map(
                data => {
                    return this.utilsService.convertResponseToObjects(data, assocs);
                }
            );
    }

    getWorkflowTemplateByID(id, assocs): Observable<any> {
        return this.searchService.getObjectById('', id, assocs)
            .map(
                data => {
                    const objs = this.utilsService.convertResponseToObjects(data, assocs);

                    if (objs && objs.length > 0) {
                        return objs[0];
                    }
                }
            );
    }

}
