"use strict";

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

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

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

var _screen = _interopRequireDefault(require("../screen"));

var _docker = _interopRequireDefault(require("../docker"));

var _emphasize = _interopRequireDefault(require("../utils/emphasize"));

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

var _details = _interopRequireDefault(require("../store/details"));

var _loadingModal = _interopRequireDefault(require("../store/loadingModal"));

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 lockModal = message => {
  _loadingModal.default.message = message;
  _loadingModal.default.active = true;
  return () => {
    _loadingModal.default.active = false;
  };
};

class Shortcuts extends _react.Component {
  componentDidMount() {
    _screen.default.key('s', async () => {
      if (_loadingModal.default.active || !_containers.default.active) {
        return;
      }

      const container = await _docker.default.getContainer(_containers.default.active.id);
      const verb = _containers.default.active.running ? 'Stopping' : 'Starting';
      const unlockModal = lockModal(`${verb} container ${_containers.default.active.name}`);

      if (_containers.default.active.running) {
        _containers.default.active.running = false;
        await container.stop();
      } else {
        _containers.default.active.running = true;
        await container.start();
        (0, _containers.listenForLogs)();
      }

      await (0, _containers.refreshContainers)();
      unlockModal();
    });

    _screen.default.key('r', async () => {
      if (_loadingModal.default.active || !_containers.default.active) {
        return;
      }

      const unlockModal = lockModal(`Restarting container ${_containers.default.active.name}`);
      const container = await _docker.default.getContainer(_containers.default.active.id);
      await container.restart();
      await (0, _containers.refreshContainers)();
      unlockModal();
    });

    _screen.default.key('e', async () => {
      if (!_containers.default.active) {
        return;
      } // fixme: mouse listening screws screen.exec


      const unlockModal = lockModal(`Starting sh in container ${_containers.default.active.name}`);
      await _screen.default.exec('docker', ['exec', '-it', _containers.default.active.name, 'sh']);
      unlockModal();
    });

    _screen.default.key('enter', async () => {
      if (!_containers.default.active) {
        return;
      }

      _screen.default.unkey('s');

      _screen.default.unkey('r');

      _screen.default.unkey('e');

      _screen.default.unkey('enter');

      _details.default.detailed = true;
    });
  }

  render() {
    const bindings = [[(0, _emphasize.default)('S'), 'Start/Stop'], [(0, _emphasize.default)('R'), 'Reload'], [(0, _emphasize.default)('E'), 'Exec'], [(0, _emphasize.default)('↵') + ' ', 'Detailed view']].map(entry => entry.join(': ')).join(' | ');
    return _react.default.createElement("text", {
      left: "0%",
      top: "99%",
      width: "100%",
      height: "1",
      valign: "middle",
      tags: true,
      content: bindings
    });
  }

}

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

exports.default = _default;