'use strict';

// Load requirements
const logger = require('../log');

// Define default formats
const defaultFormats = ['mkv', 'mp4', 'avi', 'flv', 'mov', 'wmv'];

module.exports = {

  // The default formats to support
  formats: defaultFormats.slice(0),

  // Regex pattern to handle extension matching of the above formats
  pattern: new RegExp('\.(' + defaultFormats.join('|') + ')$', 'i'),

  // Add a new format to the available list
  add: function(format) {

    // Check for an array and recurse
    if ( Array.isArray(format) ) {
      return format.every((item) => { return this.add(item); });

    // Not a string, can't do anything with it
    } else if ( typeof format !== 'string' ) {
      return false;
    }

    // Already in the list, skip but don't fail
    if ( this.formats.includes(format) ) {
      return true;
    }

    // DEBUG
    logger.verbose('Adding - ' + format);

    // Update formats and regex pattern
    this.formats.push(format);
    this.pattern = this.regex();

    return true;
  },

  // Remove a format 
  remove: function(format) {

    // Check for an array and recurse
    if ( Array.isArray(format) ) {
      return format.every((item) => { return this.remove(item); });

    // Not a string, can't do anything with it
    } else if ( typeof format !== 'string' ) {
      return false;
    }

    // Not in the list, skip but don't fail
    if ( ! this.formats.includes(format) ) {
      return true;
    }

    // DEBUG
    logger.verbose('Removing - ' + format);

    // Remove and update regex pattern
    this.formats.splice(this.formats.indexOf(format), 1);
    this.pattern = this.regex();

    return true;
  },

  // Clear the available formats
  clear: function() {

    // Empty the array
    this.formats = [];

    // Add a generic regex pattern
    this.pattern = /\.([a-z0-9]{2,5})$/i;

    // DEBUG
    logger.verbose('Clearing formats');

    return true;
  },

  // Resets the available formats
  reset: function() {

    // Reset and generate regex pattern
    this.formats = defaultFormats.slice(0);
    this.pattern = this.regex();

    // DEBUG
    logger.verbose('Reset formats');

    return true;
  },

  // Builds a regex pattern from the format arary
  regex: function() {
    return new RegExp('\.(' + this.formats.join('|') + ')$', 'i');
  },

  // Does a given filename/path match the available formats
  match: function(file) {

    // Check for an array and recurse
    if ( Array.isArray(file) ) {
      return file.every((item) => { return this.match(item); });

    // Not a string, can't do anything with it
    } else if ( typeof file !== 'string' ) {
      return false;
    }

    // Send back the result
    return file.trim().match(this.pattern);
  }

};

@@ -590,32 +590,38 @@
 s.add(item); %7D);
+%0A    %7D
 %0A%0A    // Not a s
@@ -649,39 +649,32 @@
 ing with it%0A    
-%7D else 
 if ( typeof form
@@ -1205,32 +1205,38 @@
 emove(item); %7D);
+%0A    %7D
 %0A%0A    // Not a s
@@ -1264,39 +1264,32 @@
 ing with it%0A    
-%7D else 
 if ( typeof form
@@ -2240,16 +2240,228 @@
 tion() %7B
+%0A%0A    // Run through formats and make them safe%0A    let cleanFormats = this.formats.map((item) =%3E %7B%0A      return item.replace(/%5B%5C-%5C%5B%5C%5D%5C/%5C%7B%5C%7D%5C(%5C)%5C*%5C+%5C?%5C.%5C%5C%5C%5E%5C$%5C%7C%5D/g, %22%5C%5C$&%22);%0A    %7D);%0A%0A    // Build the regex object
 %0A    ret
@@ -2483,22 +2483,22 @@
 '%5C.(' + 
-this.f
+cleanF
 ormats.j
@@ -2744,24 +2744,30 @@
 h(item); %7D);
+%0A    %7D
 %0A%0A    // Not
@@ -2811,15 +2811,8 @@
     
-%7D else 
 if (
@@ -2896,16 +2896,16 @@
  result%0A
-
     retu
@@ -2938,16 +2938,40 @@
 pattern)
+ !== null ? true : false
 ;%0A  %7D%0A%0A%7D

Add regex safe parsing of file formats