'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _cliTable = require('cli-table2');

var _cliTable2 = _interopRequireDefault(_cliTable);

var _chalk = require('chalk');

var _chalk2 = _interopRequireDefault(_chalk);

var _printTools = require('../utils/print-tools');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

class SocketSearchCmd {
  constructor(context) {
    this.context = context;
    this.session = context.session;
    this.registry = new context.Registry();
  }

  run([keyword, cmd]) {
    this.keyword = keyword;
    this.table = new _cliTable2.default({
      head: ['', '', 'name', 'description', 'author', 'version', 'keywords'],
      colWidths: [2, 2, 20, 20, 25, 10],
      wordWrap: cmd.long,
      style: {
        'padding-left': 0,
        'padding-right': 0
      },
      chars: {
        top: '',
        'top-mid': '',
        'top-left': '',
        'top-right': '',
        bottom: '',
        'bottom-mid': '',
        'bottom-left': '',
        'bottom-right': '',
        left: '',
        'left-mid': '',
        mid: '',
        'mid-mid': '',
        right: '',
        'right-mid': '',
        middle: ' '
      }
    });

    return this.registry.searchSocketsByAll(keyword).then(sockets => {
      sockets.forEach(this.addRecord.bind(this));
      (0, _printTools.echo)();
      (0, _printTools.echo)(6)(`${_chalk2.default.cyan(sockets.length)} socket(s) found: `);
      (0, _printTools.echo)();
      (0, _printTools.echo)(this.table.toString());
      (0, _printTools.echo)();
      SocketSearchCmd.printLegend(sockets);
      (0, _printTools.echo)();
    }).catch(err => {
      if (err.response && err.response.status === 404) {
        (0, _printTools.echo)();
        (0, _printTools.echo)(4)('No sockets found 😕');
        (0, _printTools.echon)(4)(`Search takes ${_chalk2.default.cyan('name')}, ${_chalk2.default.cyan('description')} `);
        (0, _printTools.echo)(`and ${_chalk2.default.cyan('keywords')} into account. Try again!`);
        (0, _printTools.echo)();
        process.exit();
      } else {
        (0, _printTools.echo)();
        (0, _printTools.error)(4)(err);
        (0, _printTools.echo)();
        process.exit(1);
      }
    });
  }

  addRecord(socket) {
    const arrayData = [socket.is_mine ? ' 👷' : '', socket.private ? '🔒 ' : '', socket.name, socket.description, socket.author, socket.version, socket.keywords ? socket.keywords.join(', ') : ''];
    const socketData = arrayData.map(item => this.colorResponse(item));
    this.table.push(socketData);
  }

  colorResponse(item) {
    const foundTerm = item && item.match(new RegExp(this.keyword, 'i'));
    if (foundTerm) {
      return item.replace(foundTerm, _chalk2.default.green(foundTerm));
    }
    return item;
  }

  static printLegend(sockets) {
    const privateSockets = _lodash2.default.filter(sockets, { private: true });
    const mineSockets = _lodash2.default.filter(sockets, { is_mine: true });
    (0, _printTools.echon)(6)();
    if (mineSockets.length > 0) {
      (0, _printTools.echon)('👷  your socket');
    }
    if (privateSockets.length > 0) {
      if (mineSockets.length > 0) {
        (0, _printTools.echon)(', ');
      }
      (0, _printTools.echon)('🔒  private socket');
    }
    (0, _printTools.echo)();
  }
}
exports.default = SocketSearchCmd;
module.exports = exports['default'];
//# sourceMappingURL=socket-search.js.map