'use strict';

const kimlikApi = require('./kimlikApi-85c6d3ac.js');

const colorMap = {
  'turquoise': '#1abc9c',
  'emerald': '#2ecc71',
  'river': '#3498db',
  'amethyst': '#9b59b6',
  'asphalt': '#34495e',
  'sun': '#f1c40f',
  'carrot': '#e67e22',
  'alizarin': '#e74c3c',
  'cloud': '#ecf0f1',
  'concrete': '#95a5a6',
};
const methodColorMap = {
  'info': '#3498db',
  'debug': '#9b59b6',
  'warn': '#e67e22',
  'error': '#e74c3c',
  'log': '#95a5a6',
};
const methodLevel = {
  'debug': 0,
  'log': 1,
  'info': 2,
  'warn': 3,
  'error': 4,
};
class Logger {
  constructor(prefix) {
    this.level = 1;
    this.new = (prefix) => {
      const logger = new Logger(prefix);
      logger.level = this.level;
      logger.color = this.color;
      return logger;
    };
    this.setColor = (color) => {
      this.color = colorMap[color];
      return this;
    };
    this.setLevel = (method) => {
      this.level = methodLevel[method];
      return this;
    };
    this.debug = (...msg) => this.print('debug', ...msg);
    this.debugGroup = (msg, ...groupedMessage) => this.printGroup('debug', msg, ...groupedMessage);
    this.log = (...msg) => this.print('log', ...msg);
    this.logGroup = (msg, ...groupedMessage) => this.printGroup('log', msg, ...groupedMessage);
    this.info = (...msg) => this.print('info', ...msg);
    this.infoGroup = (msg, ...groupedMessage) => this.printGroup('info', msg, ...groupedMessage);
    this.warn = (...msg) => this.print('warn', ...msg);
    this.warnGroup = (msg, ...groupedMessage) => this.printGroup('warn', msg, ...groupedMessage);
    this.error = (...msg) => this.print('error', ...msg);
    this.errorGroup = (msg, ...groupedMessage) => this.printGroup('error', msg, ...groupedMessage);
    this.print = (method, ...msg) => {
      if (methodLevel[method] < this.level)
        return;
      console[method](...this.getColoredMessage(method, msg));
    };
    this.printGroup = (method, msg, ...groupedMessage) => {
      if (methodLevel[method] < this.level)
        return;
      console.groupCollapsed(...this.getColoredMessage(method, msg));
      console.log(...groupedMessage);
      console.groupEnd();
    };
    this.getColoredMessage = (method, ...msg) => {
      return [`%c${method.toUpperCase()} %c${this.prefix} %c${msg.join(' ')}`, `color:${methodColorMap[method]}; font-weight: bold;`, `color:${this.color}; font-weight: bold;`, `color:${'white'}; font-weight: normal;`];
    };
    this.prefix = prefix;
  }
}

function unauthorized() {
  const error = new kimlikApi.ApiError('network');
  error.status = 401;
  return error;
}
function notFound() {
  const error = new kimlikApi.ApiError('network');
  error.status = 404;
  return error;
}
function notImplemented(message = '') {
  const error = new kimlikApi.ApiError('network');
  error.status = 501;
  error.statusText = message;
  return error;
}

exports.Logger = Logger;
exports.notFound = notFound;
exports.notImplemented = notImplemented;
exports.unauthorized = unauthorized;
