import { FHIRValueGetter, NAPEventType, NAPMessage } from '../lib';
import { Context, Extension } from '../lib/event-catalogue/fhir';
import { FHIRExtensionURL, FHIRSystem } from '../lib/event-catalogue/fhir-value-setter';

const TEST_FAMILY_NAME = 'Navnsen';
const TEST_GIVEN_NAMES = ['Navn', 'Nickname'];
const TEST_SSN = '123456789';
const TEST_ID = 'b285d6d9-370d-4c75-8951-8a9268f3283';

const TEST_MESSAGE_PROJECTS_FILTERED_NO_CONTEXT: NAPMessage = {
  event: { type: NAPEventType.ProjectsFiltered, context: [] },
  date: new Date().toISOString(),
  id: TEST_ID,
};

const TEST_MESSAGE_PATIENT_OPEN_NO_CONTEXT: NAPMessage = {
  event: { type: NAPEventType.PatientOpen, context: [] },
  date: new Date().toISOString(),
  id: TEST_ID,
};

const TEST_CONTEXT_FOR_PATIENT_OPEN: Context = {
  resource: {
    resourceType: '',
    code: {
      coding: [
        {
          code: NAPEventType.PatientOpen,
          system: '',
        },
      ],
    },
    name: [
      {
        given: TEST_GIVEN_NAMES,
        family: TEST_FAMILY_NAME,
      },
    ],
    identifier: [{ value: TEST_SSN, system: '' }],
  },
};

const TEST_MESSAGE_PATIENT_OPEN_WITH_CONTEXT: NAPMessage = {
  event: {
    type: NAPEventType.PatientOpen,
    context: [TEST_CONTEXT_FOR_PATIENT_OPEN],
  },
  date: new Date().toISOString(),
  id: TEST_ID,
};

const TEST_FILTERED_PROJECTS: string[] = ['shortName1', 'shortName2'];
const TEST_FILTERED_PROJECTS_EXTENSION: Extension[] = [{
  url: FHIRExtensionURL.WEBAPPSHORTNAMES,
  valueStringArray: TEST_FILTERED_PROJECTS
}];

const TEST_CONTEXT_FOR_PROJECTS_FILTERED: Context = {
  resource: {
    resourceType: '',
    code: {
      coding: [
        {
          code: NAPEventType.ProjectsFiltered,
          system: FHIRSystem.NAP,
        },
      ],
    },
    extension: TEST_FILTERED_PROJECTS_EXTENSION
  },
};

const TEST_MESSAGE_FILTERED_PROJECTS_WITH_CONTEXT: NAPMessage = {
  event: {
    type: NAPEventType.ProjectsFiltered,
    context: [TEST_CONTEXT_FOR_PROJECTS_FILTERED],
  },
  date: new Date().toISOString(),
  id: TEST_ID,
};

describe('fhir-value-getter', () => {
  describe('getPatientInfo()', () => {
    it('should return undefined if event type is different from PatientOpen', () => {
      expect(
        FHIRValueGetter.getPatientInfo(TEST_MESSAGE_PROJECTS_FILTERED_NO_CONTEXT)
      ).toBeUndefined();
    });
    it('should return undefined if message type is PatientOpen but there is no context', () => {
      expect(
        FHIRValueGetter.getPatientInfo(TEST_MESSAGE_PATIENT_OPEN_NO_CONTEXT)
      ).toBeUndefined();
    });
    it('should return the context if message type is PatientOpen and there is a context', () => {
      expect(
        FHIRValueGetter.getPatientInfo(TEST_MESSAGE_PATIENT_OPEN_WITH_CONTEXT)
      ).toEqual({
        givenName: TEST_GIVEN_NAMES.join(' '),
        familyName: TEST_FAMILY_NAME,
        ssNo: TEST_SSN,
      });
    });
  }),
    describe('getFilteredProjects()', () => {
      it('should return undefined if event type is different from filteredProjects', () => {
        expect(
          FHIRValueGetter.getFilteredProjects(TEST_MESSAGE_PATIENT_OPEN_NO_CONTEXT)
        ).toBeUndefined();
      });
      it('should return undefined if message type is filteredProjects but there is no context', () => {
        expect(
          FHIRValueGetter.getFilteredProjects(TEST_MESSAGE_PROJECTS_FILTERED_NO_CONTEXT)
        ).toBeUndefined();
      });
      it('should return the context if message type is filteredProjects and there is a context', () => {
        expect(
          FHIRValueGetter.getFilteredProjects(
            TEST_MESSAGE_FILTERED_PROJECTS_WITH_CONTEXT
          )
        ).toEqual(TEST_FILTERED_PROJECTS);
      });
    });
});
