import isEqual from 'lodash.isequal';

const toggleFullScreen = () => {
  let doc = window.document;
  let docEl = doc.documentElement;

  let requestFullScreen = docEl.requestFullscreen || docEl.mozRequestFullScreen || docEl.webkitRequestFullScreen || docEl.msRequestFullscreen;
  let cancelFullScreen = doc.exitFullscreen || doc.mozCancelFullScreen || doc.webkitExitFullscreen || doc.msExitFullscreen;

  if (!doc.fullscreenElement && !doc.mozFullScreenElement && !doc.webkitFullscreenElement && !doc.msFullscreenElement) {
    requestFullScreen.call(docEl);
  }
  else {
    cancelFullScreen.call(doc);
  }
};

const arraysMatch = (arr1, arr2) => {

  // Check if the arrays are the same length
  if (arr1.length !== arr2.length) return false;

  // Make local copy
  const arrayTmp1 = [...arr1];
  const arrayTmp2 = [...arr2];

  // Sort arrays
  arrayTmp1.sort();
  arrayTmp2.sort();

  // Check if all items exist and are in the same order
  for (var i = 0; i < arrayTmp1.length; i++) {
    if (arrayTmp1[i] !== arrayTmp2[i]) return false;
  }

  // Otherwise, return true
  return true;
};

const arraysMatchNoSorting = (arr1, arr2) => {

  // Check if the arrays are the same length
  if (arr1.length !== arr2.length) return false;

  // Check if all items exist and are in the same order
  for (var i = 0; i < arr1.length; i++) {
    if (arr1[i] !== arr2[i]) return false;
  }

  // Otherwise, return true
  return true;
};

const areEqualInputs = (input1, input2, propertiesToExclude = []) => {

  const tmpInput1 = Object.assign({}, input1);
  const tmpInput2 = Object.assign({}, input2);

  if (propertiesToExclude.length) {
    propertiesToExclude.forEach(p => {
      delete tmpInput1[p]
      delete tmpInput2[p]
    })
  }

  return isEqual(tmpInput1, tmpInput2);
}

const arrayOfObjsSortByKey = (array, key) => {

  function compare(a, b) {
    if (a[key] < b[key]) return -1
    if (a[key] > b[key]) return 1
    return 0
  }

  return array.sort(compare)
}

const findCommonValuesInArray = (arr1, arr2) => arr1.some(item => arr2.includes(item))
const findItemsWithExactValuesInArray = (arr1, arr2) => arr1.every(item => arr2.includes(item))
const isNotEmptyObj = (obj) => Object.keys(obj).length > 0

export default {
  toggleFullScreen,
  arraysMatch,
  arraysMatchNoSorting,
  arrayOfObjsSortByKey,
  findCommonValuesInArray,
  findItemsWithExactValuesInArray,
  isNotEmptyObj,
  areEqualInputs
};