"use strict";

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

var _react = _interopRequireWildcard(require("react"));

var _reactEasyState = require("react-easy-state");

var _lodash = _interopRequireDefault(require("lodash.throttle"));

var _env = _interopRequireDefault(require("../env"));

var _containers = _interopRequireWildcard(require("../store/containers"));

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

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

const theme = _env.default.KTRM_UI_THEME_BG;
const themeFg = _env.default.KTRM_UI_THEME_FG;

class ContainersList extends _react.Component {
  constructor() {
    super();
    this.onSelect = this.onSelect.bind(this);
  }

  componentDidMount() {
    this.list.focus();
    this.list.on('keypress', (0, _lodash.default)((_, key) => {
      if (key.name === 'up' || key.name === 'down') {
        this.list.enterSelected();
      }
    }, 200));
  }

  onSelect() {
    _containers.default.active = _containers.default.list[this.list.selected];
    (0, _containers.listenForLogs)();
  }

  containerStr(c) {
    const name = c.running ? c.name : `[stopped] ${c.name}`;
    const maxWidth = process.stdout.columns / 4 - 2;
    const separator = maxWidth < name.length ? ' ' : '\n';
    return [name, `{grey-fg}${c.image}{/grey-fg}`].join(separator);
  }

  render() {
    return _react.default.createElement("list", {
      ref: c => {
        this.list = c;
      },
      label: " Containers ",
      top: "0",
      left: "0",
      onSelect: this.onSelect,
      width: "25%",
      height: "99%",
      interactive: true,
      border: {
        type: 'line'
      },
      keys: true,
      style: {
        border: {
          fg: theme
        },
        selected: {
          fg: themeFg,
          bg: theme
        },
        item: {
          height: 2
        }
      },
      tags: true,
      items: _containers.default.list.map(this.containerStr)
    });
  }

}

var _default = (0, _reactEasyState.view)(ContainersList);

exports.default = _default;