const { dialog } = require('electron').remote;

export const sleep = (ms: number) => {
  return new Promise(resolve => setTimeout(resolve, ms));
};

export const chooseFolder = async() => {
  const response = await dialog.showSaveDialog(null, {
    filters: [{ name: 'csv', extensions: ['csv'] }],
  });
  if (response.canceled)
    return false;
  return response.filePath;
};

export const displayTimeFromNumber = (time: number): string => {
  return new Date(time * 1000).toISOString().substr(11, 8);
}

export const displayTime = (date: Date) => {
  return `${date.getHours()}:${('0' + date.getMinutes()).slice(-2)}:${('0' + date.getSeconds()).slice(-2)}`; // .${('0' + date.getMilliseconds()).slice(-3)}
};

export const isImage = (ext: string) => {
  return ['png', 'jpg', 'jpeg', 'gif'].indexOf(ext.toLocaleLowerCase()) >= 0;
};

export function pathJoin(parts: string[], separator: string = '/'){
  var replace = new RegExp(separator+'{1,}', 'g');
  return parts.join(separator).replace(replace, separator);
}