<commit_before>
import { Component, OnInit, Input, Inject } from '@angular/core';
import { EncounterResourceService } from '../../../openmrs-api/encounter-resource.service';
import { FormSchemaService } from './form-schema.service';
import { EncounterAdapter, FormFactory, Form, DataSources } from 'ngx-openmrs-formentry/dist/ngx-formentry';
import { FormDataSourceService } from './form-data-source.service';
import { FileUploadResourceService } from '../../../etl-api/file-upload-resource.service';
import { Patient } from 'src/app/models/patient.model';

import { flatMap, delay } from 'rxjs/operators';
import { take } from 'rxjs/operators';

@Component({
    selector: 'pretty-encounter-viewer',
    templateUrl: './pretty-encounter-viewer.component.html',
    styleUrls: []
})
export class PrettyEncounterViewerComponent implements OnInit {

    public selectedEncounter: any;
    @Input() set encounter(encounter) {
      if (encounter) {
        this.displayEncounterObs(encounter);
      }
    }
    public form: Form;
    public showLoader: boolean;
    public error: boolean;
    public errorMessage: string;
    public patient: Patient;
    private isHidden: any[];
    private loaderText: string;

    constructor(private encounterResourceService: EncounterResourceService,
        private formSchemaService: FormSchemaService,
        private encounterAdapter: EncounterAdapter,
        private formFactory: FormFactory,
        private fileUploadResourceService: FileUploadResourceService,
        @Inject(DataSources) private dataSources: DataSources,
        private formDataSourceService: FormDataSourceService) { }

    public ngOnInit() {}

    public wireDataSources() {
        this.dataSources.registerDataSource('file', {
            fileUpload: this.fileUploadResourceService.upload.bind(this.fileUploadResourceService),
            fetchFile: this.fileUploadResourceService.getFile.bind(this.fileUploadResourceService)
        });
        this.dataSources.registerDataSource('location',
            this.formDataSourceService.getDataSources()['location']);
        this.dataSources.registerDataSource('provider',
            this.formDataSourceService.getDataSources()['provider']);
        this.dataSources.registerDataSource('drug',
            this.formDataSourceService.getDataSources()['drug']);
        this.dataSources.registerDataSource('problem',
            this.formDataSourceService.getDataSources()['problem']);
        this.dataSources.registerDataSource('personAttribute',
            this.formDataSourceService.getDataSources()['location']);

        this.dataSources.registerDataSource('patientInfo',
            {
                name: this.patient.person.display,
                birthdate: this.patient.person.birthdate,
                mui: this.patient.searchIdentifiers.ampathMrsUId || '',
                nid: this.patient.searchIdentifiers.kenyaNationalId || '',
                age: this.patient.person.age
            });
    }

    public displayEncounterObs(encounter) {
        this.initializeLoader();
        const encounterUuid = encounter.uuid;
        if (this.selectedEncounter) {
            if (encounterUuid === this.selectedEncounter.uuid) { return; }
        }
        this.selectedEncounter = encounter;
        this.form = undefined;
        this.encounterResourceService.getEncounterByUuid(encounterUuid).pipe(
            flatMap((encounterWithObs) => {
                this.patient = new Patient(encounterWithObs.patient);
                this.selectedEncounter = encounterWithObs;
                this.wireDataSources();
                if (encounterWithObs.form) {<commit_msg>POCONC-65: Add MTRH Hospital number on the pdf download from pretty viewer<commit_after>import { Component, OnInit, Input, Inject } from '@angular/core';
import { EncounterResourceService } from '../../../openmrs-api/encounter-resource.service';
import { FormSchemaService } from './form-schema.service';
import { EncounterAdapter, FormFactory, Form, DataSources } from 'ngx-openmrs-formentry/dist/ngx-formentry';
import { FormDataSourceService } from './form-data-source.service';
import { FileUploadResourceService } from '../../../etl-api/file-upload-resource.service';
import { Patient } from 'src/app/models/patient.model';

import { flatMap, delay } from 'rxjs/operators';
import { take } from 'rxjs/operators';

@Component({
    selector: 'pretty-encounter-viewer',
    templateUrl: './pretty-encounter-viewer.component.html',
    styleUrls: []
})
export class PrettyEncounterViewerComponent implements OnInit {

    public selectedEncounter: any;
    @Input() set encounter(encounter) {
      if (encounter) {
        this.displayEncounterObs(encounter);
      }
    }
    public form: Form;
    public showLoader: boolean;
    public error: boolean;
    public errorMessage: string;
    public patient: Patient;
    private isHidden: any[];
    private loaderText: string;

    constructor(private encounterResourceService: EncounterResourceService,
        private formSchemaService: FormSchemaService,
        private encounterAdapter: EncounterAdapter,
        private formFactory: FormFactory,
        private fileUploadResourceService: FileUploadResourceService,
        @Inject(DataSources) private dataSources: DataSources,
        private formDataSourceService: FormDataSourceService) { }

    public ngOnInit() {}

    public wireDataSources() {
        this.dataSources.registerDataSource('file', {
            fileUpload: this.fileUploadResourceService.upload.bind(this.fileUploadResourceService),
            fetchFile: this.fileUploadResourceService.getFile.bind(this.fileUploadResourceService)
        });
        this.dataSources.registerDataSource('location',
            this.formDataSourceService.getDataSources()['location']);
        this.dataSources.registerDataSource('provider',
            this.formDataSourceService.getDataSources()['provider']);
        this.dataSources.registerDataSource('drug',
            this.formDataSourceService.getDataSources()['drug']);
        this.dataSources.registerDataSource('problem',
            this.formDataSourceService.getDataSources()['problem']);
        this.dataSources.registerDataSource('personAttribute',
            this.formDataSourceService.getDataSources()['location']);

        this.dataSources.registerDataSource('patientInfo',
            {
              name: this.patient.person.display || '',
              age: this.patient.person.age,
              birthdate: this.patient.person.birthdate,
              mui: this.patient.searchIdentifiers.ampathMrsUId || '',
              nid: this.patient.searchIdentifiers.kenyaNationalId || '',
              mhn: this.patient.openmrsModel.identifiers.find(identifier => identifier.identifierType.name === 'MTRH Hospital Number') ?
                // tslint:disable-next-line:max-line-length
                this.patient.openmrsModel.identifiers.find(identifier => identifier.identifierType.name === 'MTRH Hospital Number').identifier : ''
            });
    }

    public displayEncounterObs(encounter) {
        this.initializeLoader();
        const encounterUuid = encounter.uuid;
        if (this.selectedEncounter) {
            if (encounterUuid === this.selectedEncounter.uuid) { return; }
        }
        this.selectedEncounter = encounter;
        this.form = undefined;
        this.encounterResourceService.getEncounterByUuid(encounterUuid).pipe(
            flatMap((encounterWithObs) => {
                this.patient = new Patient(encounterWithObs.patient);
                this.selectedEncounter = encounterWithObs;
                this.wireDataSources();
                if (encounterWithObs.form) {
{'fraction_non_alphanumeric': 0.10971176317839522, 'fraction_numerical': 0.00025967281225655674, 'mean_word_length': 3.179598480737927, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4673203', 'n_tokens_mistral': 1988, 'n_tokens_neox': 1943, 'n_words': 348}