"use strict";

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

exports.__esModule = true;
exports.TestAutomationPanel = void 0;

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

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

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

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

var _copyToClipboard = require("../../utils/copyToClipboard");

var _getCoordsByDocument = require("../../utils/getCoordsByDocument");

var TestAutomationPanel =
/*#__PURE__*/
function (_React$Component) {
  (0, _inheritsLoose2.default)(TestAutomationPanel, _React$Component);

  function TestAutomationPanel() {
    var _this;

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "state", {
      highlightedStyle: {},
      parentTestIds: [],
      isEnable: false
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "keyMap", {});
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleKeyDown", function (e) {
      _this.keyMap[e.key] = true;

      if (_this.keyMap.Control && _this.keyMap.c) {
        (0, _copyToClipboard.copyToClipboard)(_this.state.parentTestIds.join('.'));
      }
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleKeyUp", function (e) {
      if (e.key in _this.keyMap) {
        _this.keyMap[e.key] = false;
      }
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleMouseover", function (e) {
      var highlightedElement = e.target.closest('[data-test-automation-id]');

      if (highlightedElement) {
        _this.setState({
          parentTestIds: _this.findUpTestIds(highlightedElement).reverse()
        });

        var rect = (0, _getCoordsByDocument.getCoordsByDocument)(highlightedElement);
        var top = rect.top,
            left = rect.left;

        _this.setState({
          highlightedStyle: Object.assign({}, _this.state.highlightedStyle, {
            height: highlightedElement.offsetHeight,
            width: highlightedElement.offsetWidth,
            top: top,
            left: left
          })
        });
      }
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleClick", function (e) {
      // eslint-disable-next-line no-console
      console.log('Click on ' + _this.state.parentTestIds.join('.'));
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "enablePanel", function () {
      document.addEventListener('mouseover', _this.handleMouseover);
      document.addEventListener('click', _this.handleClick);
      document.body.addEventListener('keydown', _this.handleKeyDown);
      document.body.addEventListener('keyup', _this.handleKeyUp);

      _this.setState({
        isEnable: true
      }); // eslint-disable-next-line no-console


      console.log('You can copy element test ids when it is highlighted (ctrl + c)');
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "disablePanel", function () {
      document.removeEventListener('mouseover', _this.handleMouseover);
      document.removeEventListener('click', _this.handleClick);
      document.body.removeEventListener('keydown', _this.handleKeyDown);
      document.body.removeEventListener('keyup', _this.handleKeyUp);

      _this.setState({
        isEnable: false
      });
    });
    return _this;
  }

  var _proto = TestAutomationPanel.prototype;

  _proto.componentDidMount = function componentDidMount() {
    window.TestAutomationPanel = {
      enablePanel: this.enablePanel,
      disablePanel: this.disablePanel
    };
  };

  _proto.findUpTestIds = function findUpTestIds(element) {
    var parentTestIds = [];

    while (element && element.tagName) {
      var testId = element.getAttribute('data-test-automation-id');

      if (testId) {
        parentTestIds.push(testId);
      }

      element = element.parentNode;
    }

    return parentTestIds;
  };

  _proto.render = function render() {
    if (!this.state.isEnable) {
      return null;
    }

    return _react.default.createElement("div", {
      className: "test-automation-panel"
    }, _react.default.createElement("div", {
      className: "highlighted",
      style: this.state.highlightedStyle
    }), _react.default.createElement("div", {
      className: "list"
    }, this.state.parentTestIds.join(' -> ')));
  };

  return TestAutomationPanel;
}(_react.default.Component);

exports.TestAutomationPanel = TestAutomationPanel;