"use strict";

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

(function () {

  var chalk = require('chalk');
  var csslint = require('csslint').CSSLint;
  var fs = require('fs');
  var table = require('text-table');

  var CssLinting = function () {
    function CssLinting() {
      _classCallCheck(this, CssLinting);
    }

    _createClass(CssLinting, [{
      key: 'reporter',
      value: function reporter(messages, file) {
        if (!messages.length) {
          return;
        }

        var errorLen = 0;
        var warnLen = 0;

        var output = messages.map(function (message) {
          var out = [];

          message.warn ? warnLen++ : errorLen++;

          out.push(message.line);
          out.push(message.col);
          out.push(message.warn ? chalk.yellow('warning') : chalk.red('error'));
          out.push(message.message.slice(0, -1));
          out.push(chalk.grey(message.rule.id));
          return out;
        }).filter(function (v, i) {
          if (i > 20) {
            return false;
          }
          return true;
        });

        output = table(output, {
          align: ['r', 'l']
        });
        output = output.split('\n').map(function (el) {
          return el.replace(/(\d+)\s+(\d+)/, function (m, p1, p2) {
            return chalk.grey(p1 + ':' + p2);
          });
        }).join('\n');
        console.log('\n' + file + '\n');
        console.log(output);
        warnLen && console.log(chalk.yellow.bold('\n✖ ' + warnLen + ' lint warning'));
        errorLen && console.log(chalk.red.bold('\n✖ ' + errorLen + ' lint problems'));
      }
    }, {
      key: 'linter',
      value: function linter(file, config) {

        var code = fs.readFileSync(file, {
          encoding: 'utf8'
        });
        var configs = JSON.parse(fs.readFileSync(config, {
          encoding: 'utf8'
        }));

        var messages = csslint.verify(code, configs).messages;
        var errorLen = 0;
        messages = messages.map(function (message) {
          var warn = message.type === "warning";
          if (!warn) {
            errorLen += 1;
          }
          return {
            line: message.line || 0,
            col: message.col || 0,
            message: message.message.replace(/\.$/, ''),
            rule: {
              id: message.rule.id
            },
            warn: warn
          };
        });
        this.reporter(messages, file);
        return errorLen;
      }
    }]);

    return CssLinting;
  }();

  var linting = new CssLinting();

  module.exports = function (file, config) {
    return linting.linter(file, config);
  };
})();