import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';
import classnames from 'classnames';
import { isAutomationTestActive } from '../utils/functions';
export var PrintArea =
/*#__PURE__*/
function (_React$Component) {
  _inheritsLoose(PrintArea, _React$Component);

  function PrintArea() {
    return _React$Component.apply(this, arguments) || this;
  }

  PrintArea.print = function print(id) {
    var element = PrintArea.areas[id];

    if (!element) {
      throw new Error("PrintArea with id \"" + id + "\" not found");
    }

    var node = ReactDOM.findDOMNode(element);

    if (!node) {
      throw new Error("PrintArea with id \"" + id + "\" not found");
    }

    if (!PrintArea.container) {
      PrintArea.container = document.createElement('div');
      PrintArea.container.setAttribute('data-test-automation-id', 'printArea');
      PrintArea.container.className = classnames('rc-print-area', {
        'rc-print-area-wide': element.props.wide
      });
      document.body.appendChild(PrintArea.container);
    }

    PrintArea.container.innerHTML = PrintArea.getNodeTreeHTML(node);

    if (!isAutomationTestActive()) {
      window.print();
    }
  };

  PrintArea.getNodeTreeHTML = function getNodeTreeHTML(node) {
    var html = node.innerHTML;

    while (node) {
      node = node.parentNode;

      if (node === document.body) {
        break;
      }

      html = "<" + node.tagName + " class=\"rc-print-area-parent " + node.className + "\">" + html + "</" + node.tagName + ">";
    }

    return html;
  };

  var _proto = PrintArea.prototype;

  _proto.componentDidMount = function componentDidMount() {
    if (PrintArea.areas[this.props.id]) {
      // eslint-disable-next-line
      console.error("PrintArea with id \"" + this.props.id + "\" is already exists on page");
    }

    PrintArea.areas[this.props.id] = this;
  };

  _proto.componentWillUnmount = function componentWillUnmount() {
    delete PrintArea.areas[this.props.id];
  };

  _proto.render = function render() {
    return React.createElement("span", null, this.props.children);
  };

  return PrintArea;
}(React.Component);

_defineProperty(PrintArea, "propTypes", {
  id: PropTypes.string.isRequired,
  children: PropTypes.node.isRequired,
  wide: PropTypes.bool
});

_defineProperty(PrintArea, "defaultProps", {
  wide: true
});

_defineProperty(PrintArea, "areas", {});