var sort = require('../sort');
var util = require('../util');

function latest() {
    function getLatest(array) {
        var sortedArray = array.sort(sort.date(sort.order.DESC));
        var latest = {};
        return sortedArray.filter(function (item) {
            var code = util.entry.effectiveCode(item);

            if (!latest[code]) {
                latest[code] = true;
                return true;
            }
            else {
                return false;
            }
        });
    }

    return function (prev, item, index, array) {
        return index < 2 ? getLatest(array) : prev;
    }
}

module.exports = latest;