import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import { copyToClipboard } from '../../utils/copyToClipboard';
import { getCoordsByDocument } from '../../utils/getCoordsByDocument';
export var TestAutomationPanel =
/*#__PURE__*/
function (_React$Component) {
  _inheritsLoose(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;

    _defineProperty(_assertThisInitialized(_this), "state", {
      highlightedStyle: {},
      parentTestIds: [],
      isEnable: false
    });

    _defineProperty(_assertThisInitialized(_this), "keyMap", {});

    _defineProperty(_assertThisInitialized(_this), "handleKeyDown", function (e) {
      _this.keyMap[e.key] = true;

      if (_this.keyMap.Control && _this.keyMap.c) {
        copyToClipboard(_this.state.parentTestIds.join('.'));
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleKeyUp", function (e) {
      if (e.key in _this.keyMap) {
        _this.keyMap[e.key] = false;
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleMouseover", function (e) {
      var highlightedElement = e.target.closest('[data-test-automation-id]');

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

        var rect = 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
          })
        });
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleClick", function (e) {
      // eslint-disable-next-line no-console
      console.log('Click on ' + _this.state.parentTestIds.join('.'));
    });

    _defineProperty(_assertThisInitialized(_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)');
    });

    _defineProperty(_assertThisInitialized(_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.createElement("div", {
      className: "test-automation-panel"
    }, React.createElement("div", {
      className: "highlighted",
      style: this.state.highlightedStyle
    }), React.createElement("div", {
      className: "list"
    }, this.state.parentTestIds.join(' -> ')));
  };

  return TestAutomationPanel;
}(React.Component);