/**
 * Created by jps123 on 8/2/16.
 */
"use strict";

var EX_CONSTANTS = {
    "documentExtensionNameMap": {
        "DOC": "Word 97-2003 Document",
        "DOCX": "Word Document",
        "PPT": "PowerPoint 97-2003 Presentation",
        "PPTX": "PowerPoint Presentation",
        "XLS": "Excel 97-2003 Workbook",
        "XLSX": "Excel Workbook"
    },
    "supportedImageExtensions": [
        "png",
        "jpg",
        "jpeg",
        "gif",
        "tif",
        "tiff",
        "bmp",
        "png",
        "psd",
        "psp",
        "svg"
    ],
    "supportedVideoExtensions": [
        "ogg",
        "mp4",
        "webm"
    ]
};


/**
 * Checks if file is an image.
 * @param file
 * @returns {boolean}
 */
function isImageFile(fileName) {
    var ext = getExtension(fileName).toLowerCase();
    return filterImage(ext);
};

/**
 * Returns true if the extension is an image
 * @param ext
 * @returns {boolean}
 */
function filterImage(ext) {
    return arrayContains(ext.toLowerCase(), EX_CONSTANTS.supportedImageExtensions);
};

/**
 * Looks for the needle in the array haystack
 * @param needle
 * @param arrHayStack
 * @returns {boolean}
 */
function arrayContains(needle, arrHayStack) {
    return getItemIndex(needle, arrHayStack) > -1;
}

/**
 * Looks for the needle in the array haystack and returns the index
 * @param needle
 * @param arrHayStack
 * @returns The index of needle in arrHayStack else -1
 */
function getItemIndex(needle, arrHayStack) {
    return arrHayStack.indexOf(needle);
}

/**
 * Given a file name, returns the extension in uppercase
 * @param fileName
 * @returns {string}
 */
function getExtension(fileName) {
    var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
    return ext.toUpperCase();
};

module.exports = {
    isImageFile: isImageFile,
    getExtension: getExtension,
    arrayContains: arrayContains,
    getItemIndex: getItemIndex
};