const path = require('path');
const chalk = require('chalk');

var utils = {
  getKey: (str) => {
    return str.toLowerCase()
      .replace(/[^\w\s-]/g, '') // remove non-word [a-z0-9_], non-whitespace, non-hyphen characters
      .replace(/[\s_-]+/g, '-') // swap any length of whitespace, underscore, hyphen characters with a single -
      .replace(/^-+|-+$/g, ''); // remove leading, trailing -
  },

  getFilenameKey: (file) => {
    return utils.getKey(path.basename(file, path.extname(file)));
  },

  wrap: (content, tagname, id) => {
    return `<${tagname} id="${id}" class="<%-styles['section-item']-%>">${content}</${tagname}>`;
  },

  isExternal: (path) => {
    const pattern = /^https?:\/\//i;
    return (pattern.test(path))
  },

  titlefy: (str) => {
    str = str.replace(/^\d+/g, '') // remove leading numbers
    .replace(/^[-_]+/g, '') // remove and delimiter after number
    .replace(/[-_]/g, ' '); // convert hyphens and underscores to spaces

    return str.replace(/\w\S*/g, function (txt) {
      return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
    });
  },

  logTask: (task) => {
    console.log(chalk.blue(task));
  },

  logSuccess: (msg) => {
    console.log(chalk.green(msg));
  },

  logError: (err) => {
    console.log(chalk.red(err));
  }

};

module.exports = utils;
