"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

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

var _tslib = require("tslib");

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

var _xterm = require("xterm");

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

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

// ported from hterm.Terminal.IO.prototype.writeUTF8
// https://chromium.googlesource.com/apps/libapps/+/master/hterm/js/hterm_terminal_io.js
var opt = {
  // lineHeight: 1.5,
  fontSize: 13,
  fontFamily: '"Menlo for Powerline", Menlo, Consolas, "Liberation Mono", Courier, monospace',
  theme: {
    foreground: '#d2d2d2',
    background: '#000000',
    cursor: '#adadad',
    black: '#000000',
    red: '#d81e00',
    green: '#5ea702',
    yellow: '#cfae00',
    blue: '#427ab3',
    magenta: '#89658e',
    cyan: '#00a7aa',
    white: '#ffffff',
    brightBlack: '#686a66',
    brightRed: '#f54235',
    brightGreen: '#99e343',
    brightYellow: '#fdeb61',
    brightBlue: '#84b0d8',
    brightMagenta: '#bc94b7',
    brightCyan: '#37e6e8',
    brightWhite: '#f1f1f0'
  }
};

var LPTerminal =
/** @class */
function (_super) {
  (0, _tslib.__extends)(LPTerminal, _super);

  function LPTerminal(props, context) {
    var _this = _super.call(this, props, context) || this;

    _this.applyAddon = function (addon) {
      _xterm.Terminal.applyAddon(addon);
    };

    _this.decodeUTF8 = function (str) {
      var bytesLeft = 0;
      var codePoint = 0;
      var lowerBound = 0;
      var ret = '';

      for (var i = 0; i < str.length; i++) {
        var c = str.charCodeAt(i);

        if (bytesLeft === 0) {
          if (c <= 0x7F) {
            ret += str.charAt(i);
          } else if (c >= 0xC0 && c <= 0xDF) {
            codePoint = c - 0xC0;
            bytesLeft = 1;
            lowerBound = 0x80;
          } else if (c >= 0xE0 && c <= 0xEF) {
            codePoint = c - 0xE0;
            bytesLeft = 2;
            lowerBound = 0x800;
          } else if (c >= 0xF0 && c <= 0xF7) {
            codePoint = c - 0xF0;
            bytesLeft = 3;
            lowerBound = 0x10000;
          } else if (c >= 0xF8 && c <= 0xFB) {
            codePoint = c - 0xF8;
            bytesLeft = 4;
            lowerBound = 0x200000;
          } else if (c >= 0xFC && c <= 0xFD) {
            codePoint = c - 0xFC;
            bytesLeft = 5;
            lowerBound = 0x4000000;
          } else {
            ret += "\uFFFD";
          }
        } else if (c >= 0x80 && c <= 0xBF) {
          // eslint-disable-next-line no-plusplus
          bytesLeft--; // eslint-disable-next-line no-bitwise

          codePoint = (codePoint << 6) + (c - 0x80);

          if (bytesLeft === 0) {
            var codePointItem = codePoint;

            if (codePointItem < lowerBound || codePointItem >= 0xD800 && codePointItem <= 0xDFFF || codePointItem > 0x10FFFF) {
              ret += "\uFFFD";
            } else if (codePointItem < 0x10000) {
              ret += String.fromCharCode(codePointItem);
            } else {
              codePointItem -= 0x10000;
              ret += String.fromCharCode( // eslint-disable-next-line no-bitwise
              0xD800 + (codePointItem >>> 10 & 0x3FF), // eslint-disable-next-line no-bitwise
              0xDC00 + (codePointItem & 0x3FF));
            }
          }
        } else {
          ret += "\uFFFD";
          bytesLeft = 0; // eslint-disable-next-line no-plusplus

          i--;
        }
      }

      return ret;
    };

    _this.encodeUTF8 = function (str) {
      var ret = '';

      for (var i = 0; i < str.length; i++) {
        var c = str.charCodeAt(i);

        if (c >= 0xDC00 && c <= 0xDFFF) {
          c = 0xFFFD;
        } else if (c >= 0xD800 && c <= 0xDBFF) {
          if (i + 1 < str.length) {
            var d = str.charCodeAt(i + 1);

            if (d >= 0xDC00 && d <= 0xDFFF) {
              // eslint-disable-next-line no-bitwise
              c = 0x10000 + ((c & 0x3FF) << 10) + (d & 0x3FF); // eslint-disable-next-line no-plusplus

              i++;
            } else {
              c = 0xFFFD;
            }
          } else {
            c = 0xFFFD;
          }
        } // eslint-disable-next-line no-var


        var bytesLeft;

        if (c <= 0x7F) {
          ret += str.charAt(i); // eslint-disable-next-line no-continue

          continue;
        } else if (c <= 0x7FF) {
          // eslint-disable-next-line no-bitwise
          ret += String.fromCharCode(0xC0 | c >>> 6);
          bytesLeft = 1;
        } else if (c <= 0xFFFF) {
          // eslint-disable-next-line no-bitwise
          ret += String.fromCharCode(0xE0 | c >>> 12);
          bytesLeft = 2;
        } else {
          // eslint-disable-next-line no-bitwise
          ret += String.fromCharCode(0xF0 | c >>> 18);
          bytesLeft = 3;
        }

        while (bytesLeft > 0) {
          // eslint-disable-next-line no-plusplus
          bytesLeft--; // eslint-disable-next-line no-bitwise

          ret += String.fromCharCode(0x80 | c >>> 6 * bytesLeft & 0x3F);
        }
      }

      return ret;
    };

    _this.getTerminal = function () {
      return _this.xterm;
    };

    _this.writeUTF8 = function (data) {
      _this.write(_this.decodeUTF8(data));
    };

    _this.write = function (data) {
      _this.xterm && _this.xterm.write(data);
    };

    _this.writeln = function (data) {
      _this.xterm && _this.xterm.writeln(data);
    };

    _this.focus = function () {
      _this.xterm && _this.xterm.focus();
    };

    _this.fit = function () {
      _this.xterm && _this.xterm.fit();
    };

    _this.focusChanged = function (focused) {
      _this.focused = focused;
      _this.props.onFocusChange && _this.props.onFocusChange(focused);
    };

    _this.onResize = function (data) {
      _this.props.onResize && _this.props.onResize(data);
    };

    _this.onKeypress = function (key, ev) {
      _this.props.onKeypress && _this.props.onKeypress(key, ev);
    };

    _this.onInput = function (data) {
      _this.props.onInput && _this.props.onInput(data);
    };

    _this.resize = function (cols, rows) {
      _this.xterm && _this.xterm.resize(Math.round(cols), Math.round(rows));
    };

    _this.setOption = function (key, value) {
      _this.xterm && _this.xterm.setOption(key, value);
    };

    _this.refresh = function () {
      _this.xterm && _this.xterm.refresh(0, _this.xterm.rows - 1);
    };

    _this.xterm = null;
    _this.container = null;
    _this.options = (0, _tslib.__assign)({
      opt: opt
    }, props.options);
    _this.focused = false;
    return _this;
  }

  LPTerminal.prototype.componentDidMount = function () {
    var _a = this.props,
        addons = _a.addons,
        onInput = _a.onInput,
        onKeypress = _a.onKeypress,
        onResize = _a.onResize;

    if (addons && addons instanceof Array) {
      addons.forEach(function (s) {
        var addon = require("xterm/dist/addons/" + s + "/" + s + ".js");

        _xterm.Terminal.applyAddon(addon);
      });
    }

    this.xterm = new _xterm.Terminal(this.options);
    this.xterm.open(this.container);
    this.xterm.on('focus', this.focusChanged.bind(this, true));
    this.xterm.on('blur', this.focusChanged.bind(this, false));

    if (onInput) {
      this.xterm.on('data', this.onInput);
    }

    if (onKeypress) {
      this.xterm.on('key', this.onKeypress);
    }

    if (onResize) {
      this.xterm.on('resize', this.onResize);
    }
  };

  LPTerminal.prototype.componentWillUnmount = function () {
    if (this.xterm) {
      this.xterm.destroy();
      this.xterm = null;
    }
  };

  LPTerminal.prototype.render = function () {
    var _this = this;

    var _a = this.props,
        style = _a.style,
        className = _a.className,
        visible = _a.visible;
    return /*#__PURE__*/_react["default"].createElement("div", {
      ref: function ref(_ref) {
        return _this.container = _ref;
      },
      className: className,
      style: (0, _tslib.__assign)((0, _tslib.__assign)({}, style), {
        visibility: visible ? 'visible' : 'hidden'
      })
    });
  };

  return LPTerminal;
}(_react.PureComponent);

LPTerminal.defaultProps = {
  visible: true,
  addons: []
};
;
var _default = LPTerminal;
exports["default"] = _default;