// function jsonReplacer(max = 256) {
//   let sizeof = 2;
//   return (key, value) => {
//     sizeof += key.length + 4; //"{key}": ,
//     if(value !== null && typeof value == "object") {
//       sizeof += 2 // {}
//       if(Buffer.isBuffer(value)) return undefined;
//       if(Array.isArray(value)) {
//         if(sizeof >= max) return "[...]";
//       } else if(value instanceof RegExp) {
//         value = value.toString();
//         sizeof += value.length + 2;
//         if(sizeof >= max) return value.length > 3 ? "..." : value;
//       } else if(sizeof >= max) return "{...}";
//     } else if(typeof value === "string") {
//       if(key.toLowerCase().indexOf("password") >= 0) return "..."
//       sizeof += value.length + 2;
//       if(sizeof >= max) return value.length > 3 ? "..." : value;
//     } else if(typeof value === "boolean") {
//       sizeof += 5;
//     } else if(typeof value === "number") {
//       sizeof += ("" + value).length + 2;
//     }

//     return value;
//   }
// }
const jsonReplacer = () => null;

const Logger = argv => ({
  info: (...data) => ({
    dir: async () => data.forEach($ => console.dir($, {depth: null, colors: true})),
    json: async () => console.log(...data.map($ => JSON.stringify($, jsonReplacer(), 2))),
    log: async () => console.log(...data),
    raw: async () => data.forEach($ => process.stdout.write(JSON.stringify($))),
  })[argv.console](),
  error: (...data) => ({
    dir: async () => data.forEach($ => console.dir($, {depth: null, colors: true})),
    json: async () => console.error(...data.map($ => JSON.stringify($, jsonReplacer(), 2))),
    log: async () => console.error(...data),
    raw: async () => data.forEach($ => process.stderr.write(JSON.stringify($))),
  })[argv.console]()
})

Logger.wrap = (funct, logInfo = true) => async argv => {
  const log = Logger(argv);
  try {
    const res = await funct(argv);
    res === undefined || (logInfo && log.info(res));
    return res;
  } catch(error) {
    log.error(error)
    // throw error;
  }
}

module.exports = Logger;
