'use strict';
var moment = require("moment");

var defaultComparator = function(value1, value2) {
    return value1 === value2;
};

module.exports = {
    intersection: function (array1, array2, comparator) {
        var temp = [];

        var thisComparator = comparator ? comparator : defaultComparator;

        for (var i = 0; i < array1.length; i ++) {
            for (var j = 0; j < array2.length; j ++) {
                if (thisComparator(array1[i], array2[j])) {
                    temp.push(array1[i]);
                    break;
                }
            }
        }

        return temp;
    },

    union: function (array1, array2, comparator) {
        var temp = array1.concat([]);

        if (!temp || !temp.length) {
            return array2.concat([]);
        }

        var thisComparator = comparator ? comparator : defaultComparator;

        for (var i = 0; i < array2.length; i++) {
            if(!this.contains(temp, array2[i], thisComparator)) {
                temp.push(array2[i]);
            }
        }

        return temp;
    },

    exclude: function(sourceArr, excludedArr, comparator) {
        var temp = [];

        if (!excludedArr || !excludedArr.length) {
            return sourceArr;
        }

        var thisComparator = comparator ? comparator : defaultComparator;

        for (var i = 0; i < sourceArr.length; i++) {
            if (!this.contains(excludedArr, sourceArr[i], thisComparator)) {
                temp.push(sourceArr[i]);
            }
        }

        return temp;
    },

    containsIndex: function (array, value, comparator) {
        var thisComparator = comparator ? comparator : defaultComparator;

        for (var i = 0; i < array.length; i++) {
            if(thisComparator(array[i], value)) {
                return i;
            }
        }

        return -1;
    },

    contains: function (array, value, comparator) {
        return this.containsIndex(array, value, comparator) !== -1;
    },

    move: function (arr, src, dest){
        var toMoveVal = arr[src];
        arr[dest] = toMoveVal;
        arr[src] = null;
    },

    deleteSplice: function (arr, i){
        arr.splice(i, 1);
    },

    deleteSetNull: function (arr, i){
        arr[i] = null;
    },

    swap: function (arr, i1, i2){
        var tmp1 = arr[i1];
        var tmp2 = arr[i2];
        arr[i1] = tmp2;
        arr[i2] = tmp1;
    },

    add: function(array, item, excludeDuplicated, comparator) {
        if (excludeDuplicated && this.contains(array, item, comparator)) {
            return false;
        }
        array.push(item);

        return true;
    },

    addOrReplace: function(array, item, comparator) {
        var idx = this.containsIndex(array, item, comparator);
        if (idx !== -1) {
            array[idx] = item;
        } else {
            array.push(item);
        }
    },

    get: function(array, value, comparator) {
        var idx = this.containsIndex(array, value, comparator);
        if (value !== -1) {
            return array[idx];
        }
        return null;
    }

};
