"use strict";

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

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

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

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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _reactDom = _interopRequireDefault(require("react-dom"));

var _classnames = _interopRequireDefault(require("classnames"));

var _functions = require("../utils/functions");

var PrintArea =
/*#__PURE__*/
function (_React$Component) {
  (0, _inheritsLoose2.default)(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.default.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 = (0, _classnames.default)('rc-print-area', {
        'rc-print-area-wide': element.props.wide
      });
      document.body.appendChild(PrintArea.container);
    }

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

    if (!(0, _functions.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.default.createElement("span", null, this.props.children);
  };

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

exports.PrintArea = PrintArea;
(0, _defineProperty2.default)(PrintArea, "propTypes", {
  id: _propTypes.default.string.isRequired,
  children: _propTypes.default.node.isRequired,
  wide: _propTypes.default.bool
});
(0, _defineProperty2.default)(PrintArea, "defaultProps", {
  wide: true
});
(0, _defineProperty2.default)(PrintArea, "areas", {});