"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

function _defineProperty2() {
  const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

  _defineProperty2 = function () {
    return data;
  };

  return data;
}

function _ttyTable() {
  const data = _interopRequireDefault(require("tty-table"));

  _ttyTable = function () {
    return data;
  };

  return data;
}

function _command() {
  const data = _interopRequireDefault(require("../../command"));

  _command = function () {
    return data;
  };

  return data;
}

function _scope() {
  const data = require("../../../api/scope");

  _scope = function () {
    return data;
  };

  return data;
}

function _modelComponent() {
  const data = _interopRequireDefault(require("../../../scope/models/model-component"));

  _modelComponent = function () {
    return data;
  };

  return data;
}

class CatScope extends _command().default {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2().default)(this, "name", 'cat-scope [scopePath]');
    (0, _defineProperty2().default)(this, "description", 'cat a scope and show all the contents');
    (0, _defineProperty2().default)(this, "private", true);
    (0, _defineProperty2().default)(this, "alias", '');
    (0, _defineProperty2().default)(this, "opts", [['f', 'full', 'show all of the objects in the scope'], ['j', 'json', 'print the objects as a json format'], ['e', 'json-extra', 'add hash and object type to the json']]);
  }

  action([scopePath], {
    full,
    json,
    jsonExtra
  }) {
    return (0, _scope().catScope)(scopePath || process.cwd(), full).then(payload => ({
      payload,
      full,
      json,
      jsonExtra
    }));
  }

  report({
    payload,
    full,
    json,
    jsonExtra
  }) {
    if (jsonExtra) {
      payload.forEach(obj => {
        // $FlowFixMe
        obj.hash = obj.hash().toString(); // $FlowFixMe

        obj.type = obj.constructor.name;
      });
      return JSON.stringify(payload, null, 2);
    }

    if (json) {
      return JSON.stringify(payload, null, 2);
    }

    if (!full) {
      const header = [{
        value: 'Id',
        width: 70,
        headerColor: 'cyan'
      }, {
        value: 'Object',
        width: 50,
        headerColor: 'cyan'
      }];
      const opts = {
        align: 'left'
      };
      const table = new (_ttyTable().default)(header, [], opts);
      payload.forEach(co => table.push([co.id(), `obj: ${co.hash().toString()}`]));
      return table.render();
    }

    return payload.map(co => `> ${co.hash().toString()}\n\n${co.id()}\n`).join('\n');
  }

}

exports.default = CatScope;