var date = require('../../lib/sort/index').date;
var order = require('../../lib/sort/index').order;

var should = require('chai').should();

describe('sort.date', function () {
    var resource = {
        contentType: 'Bundle',
        entry: [
            {
                content: {
                    resourceType: 'Patient',
                    birthDate: '1980-04-01'
                }
            },
            {
                content: {
                    resourceType: 'MedicationPrescription',
                    dateWritten: '2000-03-08'//T09:00:00.000Z'
                }
            },
            {
                content: {
                    resourceType: 'Condition',
                    dateAsserted: '2002-03-08'//T09:00:00.000Z'
                }
            },
            {
                content: {
                    resourceType: 'Condition',
                    dateAsserted: '2001-03-08'//T09:00:00.000Z'
                }
            }
        ]
    };

    it('should return a sorted list of entries in ascending order', function () {

        var result = resource.entry.sort(date(order.ASC));

        should.exist(result);
        result.length.should.equal(4);
        result[3].content.dateAsserted.should.equal('2002-03-08');
    });

    it('should return a sorted list of entries in descending order', function () {

        var result = resource.entry.sort(date(order.DESC));

        should.exist(result);
        result.length.should.equal(4);
        result[3].content.dateWritten.should.equal('2000-03-08');
    });

    it('should return a sorted list of entries in ascending order when order not specified', function () {

        var result = resource.entry.sort(date());

        should.exist(result);
        result.length.should.equal(4);
        result[3].content.dateAsserted.should.equal('2002-03-08');
    });

    it('should return a sorted list of entries when using ISO dates', function () {

        var entry = [
            {
                id: 1,
                content: {
                    resourceType: 'Observation',
                    name: {coding: [
                        { system: 'READ2', code: '2469' }
                    ]},
                    issued: '2012-03-08T09:00:00.000Z'
                }
            },
            {
                id: 2,
                content: {
                    resourceType: 'Observation',
                    name: {coding: [
                        { system: 'READ2', code: '246A' }
                    ]},
                    issued: '2012-03-08T09:00:00.000Z'
                }
            },
            {
                id: 3,
                content: {
                    resourceType: 'Observation',
                    name: {coding: [
                        { system: 'READ2', code: '2469' }
                    ]},
                    issued: '2014-03-08T09:00:00.000Z'
                }
            },
            {
                id: 4,
                content: {
                    resourceType: 'Observation',
                    name: {coding: [
                        { system: 'READ2', code: '246A' }
                    ]},
                    issued: '2014-03-08T09:00:00.000Z'
                }
            },
            {
                id: 5,
                content: {
                    resourceType: 'Observation',
                    name: {coding: [
                        { system: 'READ2', code: '2469' }
                    ]},
                    issued: '2010-03-08T09:00:00.000Z'
                }
            },
            {
                id: 6,
                content: {
                    resourceType: 'Observation',
                    name: {coding: [
                        { system: 'READ2', code: '246A' }
                    ]},
                    issued: '2010-03-08T09:00:00.000Z'
                }
            },
            {
                id: 7,
                content: {
                    resourceType: 'Observation',
                    name: {coding: [
                        { system: 'READ2', code: '2469' }
                    ]},
                    issued: '2008-03-08T09:00:00.000Z'
                }
            },
            {
                id: 8,
                content: {
                    resourceType: 'Observation',
                    name: {coding: [
                        { system: 'READ2', code: '246A' }
                    ]},
                    issued: '2008-03-08T09:00:00.000Z'
                }
            }
        ];

        var result = entry.sort(date());

        should.exist(result);
        result.length.should.equal(8);
        result[0].content.issued.should.equal('2008-03-08T09:00:00.000Z');
        result[7].content.issued.should.equal('2014-03-08T09:00:00.000Z');
    });
});