function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import React from "react";
import { findDOMNode } from "react-dom";
import PropTypes from "prop-types";
import { reduxForm } from "redux-form";
import { wrapDialog } from "teselagen-react-components";
import { compose } from "redux";
import { Button, Classes, ButtonGroup } from "@blueprintjs/core";
import classNames from "classnames";
import withEditorProps from "../../withEditorProps";
import CircularView from "../../CircularView";
import LinearView from "../../LinearView";
import "./style.css";
var PrintDialog = /*#__PURE__*/function (_React$Component) {
  _inheritsLoose(PrintDialog, _React$Component);
  function PrintDialog() {
    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", {
      circular: null
    });
    return _this;
  }
  var _proto = PrintDialog.prototype;
  _proto.render = function render() {
    var _this2 = this;
    var _this$props = this.props,
      hideModal = _this$props.hideModal,
      hideLinearCircularToggle = _this$props.hideLinearCircularToggle,
      hidePrintButton = _this$props.hidePrintButton,
      addPaddingBottom = _this$props.addPaddingBottom,
      sequenceData = _this$props.sequenceData,
      editorName = _this$props.editorName;
    // const sequenceLength = sequenceData.sequence.length;
    var isCirc = sequenceData.circular;
    if (this.state.circular !== null) {
      isCirc = this.state.circular;
    }
    return /*#__PURE__*/React.createElement("div", {
      style: addPaddingBottom ? {
        paddingBottom: 20
      } : {},
      className: classNames(Classes.DIALOG_BODY, "tg-min-width-dialog")
    }, !hideLinearCircularToggle && /*#__PURE__*/React.createElement("div", {
      style: {
        display: "flex",
        justifyContent: "center"
      }
    }, /*#__PURE__*/React.createElement(ButtonGroup, null, /*#__PURE__*/React.createElement(Button, {
      onClick: function onClick() {
        _this2.setState({
          circular: true
        });
      },
      active: isCirc
    }, "Circular"), /*#__PURE__*/React.createElement(Button, {
      onClick: function onClick() {
        _this2.setState({
          circular: false
        });
      },
      active: !isCirc
    }, "Linear"))), /*#__PURE__*/React.createElement("br", null), /*#__PURE__*/React.createElement(ComponentToPrint, {
      fullscreen: this.state && this.state.fullscreen,
      circular: isCirc,
      editorName: editorName || "StandaloneEditor",
      ref: function ref(el) {
        return _this2.componentRef = el;
      }
    }), /*#__PURE__*/React.createElement("br", null), !hidePrintButton && /*#__PURE__*/React.createElement("div", {
      style: {
        display: "flex",
        justifyContent: "flex-end"
      }
    }, /*#__PURE__*/React.createElement(ReactToPrint, {
      trigger: function trigger() {
        return /*#__PURE__*/React.createElement(Button, {
          intent: "primary"
        }, "Print");
      },
      content: function content() {
        return _this2.componentRef;
      },
      onBeforePrint: function onBeforePrint() {
        _this2.setState({
          fullscreen: true
        });
      },
      bodyClass: "ve-print"
      // printPreview
      ,
      pageStyle: "@page { size: auto;  margin: 0mm; } @media print { \n              body { \n                 -webkit-print-color-adjust: exact; page-break-after: always; \n              } }",
      ignoreLinks: true //needed because some css is linked to but is not loading..
      ,
      onAfterPrint: function onAfterPrint() {
        _this2.setState({
          fullscreen: false
        });
        hideModal();
      }
    })));
  };
  return PrintDialog;
}(React.Component);
export default compose(wrapDialog({
  title: "Print"
}), withEditorProps, reduxForm({
  form: "PrintDialog"
}))(PrintDialog);
var ReactToPrint = /*#__PURE__*/function (_React$Component2) {
  _inheritsLoose(ReactToPrint, _React$Component2);
  function ReactToPrint() {
    var _this3;
    for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
      args[_key2] = arguments[_key2];
    }
    _this3 = _React$Component2.call.apply(_React$Component2, [this].concat(args)) || this;
    _defineProperty(_assertThisInitialized(_this3), "startPrint", function () {
      var _this3$props = _this3.props,
        bodyClass = _this3$props.bodyClass,
        content = _this3$props.content,
        copyStyles = _this3$props.copyStyles,
        ignoreLinks = _this3$props.ignoreLinks,
        pageStyle = _this3$props.pageStyle,
        printPreview = _this3$props.printPreview;
      var contentEl = content();
      if (contentEl === undefined) {
        console.error("Refs are not available for stateless components. For 'react-to-print' to work only Class based components can be printed");
        return false;
      }
      var printWindow = document.createElement("iframe");
      if (printPreview) {
        var printOutline = document.createElement("div");
        var closeButton = document.createElement("button");
        var printButton = document.createElement("button");
        var elsToRemoveFromDomEventually = [printWindow, printOutline, closeButton, printButton];
        printOutline.style.position = "absolute";
        printOutline.style.top = "0px";
        printOutline.style.left = "0px";
        printOutline.style.width = "1210px";
        printOutline.style.height = "1056px";
        printOutline.style.background = "grey";
        printOutline.style.zIndex = 100000300;
        document.body.appendChild(printOutline);
        printButton.style.position = "absolute";
        printButton.style.top = "0px";
        printButton.style.left = "0px";
        printButton.style.width = "100px";
        printButton.style.background = "lightblue";
        printButton.innerText = "print";
        printButton.onclick = function () {
          _this3.triggerPrint(printWindow, true);
        };
        printButton.style.zIndex = 100000500;
        document.body.appendChild(printButton);
        closeButton.style.position = "absolute";
        closeButton.style.top = "0px";
        closeButton.style.left = "250px";
        closeButton.style.width = "100px";
        closeButton.style.background = "lightblue";
        closeButton.innerText = "close";
        closeButton.onclick = function () {
          _this3.removeWindow(elsToRemoveFromDomEventually);
        };
        closeButton.style.zIndex = 100000500;
        document.body.appendChild(closeButton);
      }

      // printWindow.style.height = height
      // printWindow.style.width = width

      // todo: find the max height/width of the print window and fit it to that!

      // if (printPreview)  defaultPageStyle += " "

      var contentNodes = findDOMNode(contentEl);
      var linkNodes = document.querySelectorAll('link[rel="stylesheet"]');
      _this3.linkTotal = linkNodes.length || 0;
      _this3.linkLoaded = 0;
      var markLoaded = function markLoaded() {
        _this3.linkLoaded++;
        if (_this3.linkLoaded === _this3.linkTotal) {
          if (printPreview) {
            return;
          }
          _this3.triggerPrint(printWindow);
        }
      };
      printWindow.onload = function () {
        /* IE11 support */
        if (window.navigator && window.navigator.userAgent.indexOf("Trident/7.0") > -1) {
          printWindow.onload = null;
        }
        var domDoc = printWindow.contentDocument || printWindow.contentWindow.document;
        var srcCanvasEls = [].concat(contentNodes.querySelectorAll("canvas"));
        domDoc.open();
        domDoc.write(contentNodes.outerHTML);
        domDoc.close();
        printWindow.style.position = "absolute";
        // printWindow.style.width = "100%";
        // printWindow.style.height = "100%";
        printWindow.style.top = "-1000px";
        printWindow.style.left = "-1000px";
        printWindow.style.width = domDoc.body.scrollWidth + "px";
        printWindow.style.height = domDoc.body.scrollHeight + "px";
        if (printPreview) {
          printWindow.style.top = "0px";
          printWindow.style.left = "0px";
          printWindow.style.width = "1210px";
          printWindow.style.height = "1056px";
          printWindow.style.background = "white";
          printWindow.style.zIndex = 100000400;
        }

        /* remove date/time from top */
        var defaultPageStyle = pageStyle === undefined ? "@page { size: auto;  margin: 0mm; } @media print { body { -webkit-print-color-adjust: exact;} }" : pageStyle;
        var styleEl = domDoc.createElement("style");
        styleEl.appendChild(domDoc.createTextNode(defaultPageStyle));
        domDoc.head.appendChild(styleEl);

        // domDoc.body.style.transform = ""
        if (bodyClass.length) {
          domDoc.body.classList.add(bodyClass);
        }
        try {
          var canvasEls = domDoc.querySelectorAll("canvas");
          [].concat(canvasEls).forEach(function (node, index) {
            if (node.getContext) {
              node.getContext("2d").drawImage(srcCanvasEls[index], 0, 0);
            }
          });
        } catch (e) {
          console.warn("An error occurred trying to get the canvas elements for printing:");
          console.warn("gg2g20011 error:", e);
        }
        if (copyStyles !== false) {
          var headEls = document.querySelectorAll('style, link[rel="stylesheet"]');
          [].concat(headEls).forEach(function (node, index) {
            var newHeadEl = domDoc.createElement(node.tagName);
            var styleCSS = "";
            if (node.tagName === "STYLE") {
              if (node.sheet) {
                for (var i = 0; i < node.sheet.cssRules.length; i++) {
                  styleCSS += node.sheet.cssRules[i].cssText + "\r\n";
                }
                newHeadEl.setAttribute("id", "react-to-print-" + index);
                newHeadEl.appendChild(domDoc.createTextNode(styleCSS));
              }
            } else {
              var attributes = [].concat(node.attributes || []);
              attributes.forEach(function (attr) {
                newHeadEl.setAttribute(attr.nodeName, attr.nodeValue);
              });
              newHeadEl.readystatechange = markLoaded.bind(null, "link");
              newHeadEl.onload = markLoaded.bind(null, "link");
              newHeadEl.onerror = markLoaded.bind(null, "link");
            }
            domDoc.head.appendChild(newHeadEl);
          });
        }
        if (_this3.linkTotal === 0 || copyStyles === false || ignoreLinks) {
          if (printPreview) {
            return;
          }
          _this3.triggerPrint(printWindow);
        }
      };
      document.body.appendChild(printWindow);
    });
    return _this3;
  }
  var _proto2 = ReactToPrint.prototype;
  _proto2.triggerPrint = function triggerPrint(target, noRemove) {
    var _this4 = this;
    var _this$props2 = this.props,
      onBeforePrint = _this$props2.onBeforePrint,
      onAfterPrint = _this$props2.onAfterPrint;
    if (onBeforePrint) {
      onBeforePrint();
    }
    setTimeout(function () {
      target.contentWindow.focus();
      target.contentWindow.print();
      !noRemove && _this4.removeWindow(target);
      if (onAfterPrint) {
        onAfterPrint();
      }
    }, 500);
  };
  _proto2.removeWindow = function removeWindow(targets) {
    targets && setTimeout(function () {
      (Array.isArray(targets) ? targets : [targets]).forEach(function (target) {
        target.parentNode.removeChild(target);
      });
    }, 500);
  };
  _proto2.render = function render() {
    var _this5 = this;
    return React.cloneElement(this.props.trigger(), {
      ref: function ref(el) {
        return _this5.triggerRef = el;
      },
      onClick: this.startPrint
    });
  };
  return ReactToPrint;
}(React.Component);
_defineProperty(ReactToPrint, "defaultProps", {
  copyStyles: true,
  closeAfterPrint: true,
  bodyClass: ""
});
ReactToPrint.propTypes = process.env.NODE_ENV !== "production" ? {
  /** Preview the print without actually triggering the print dialog */
  printPreview: PropTypes.bool,
  /** Copy styles over into print window. default: true */
  copyStyles: PropTypes.bool,
  /** Ignore link styles. Necessary because sometime links don't load.., default: false */
  ignoreLinks: PropTypes.bool,
  /** Trigger action used to open browser print */
  trigger: PropTypes.func.isRequired,
  /** Content to be printed */
  content: PropTypes.func.isRequired,
  /** Callback function to trigger before print */
  onBeforePrint: PropTypes.func,
  /** Callback function to trigger after print */
  onAfterPrint: PropTypes.func,
  /** Override default print window styling */
  pageStyle: PropTypes.string,
  /** Optional class to pass to the print window body */
  bodyClass: PropTypes.string
} : {};
var ComponentToPrint = /*#__PURE__*/function (_React$Component3) {
  _inheritsLoose(ComponentToPrint, _React$Component3);
  function ComponentToPrint() {
    return _React$Component3.apply(this, arguments) || this;
  }
  var _proto3 = ComponentToPrint.prototype;
  // componentDidMount() {
  //   let ctx = this.canvasEl.getContext("2d");
  //   ctx.beginPath();
  //   ctx.arc(95, 50, 40, 0, 2 * Math.PI);
  //   ctx.stroke();
  // }
  _proto3.render = function render() {
    var _this$props3 = this.props,
      editorName = _this$props3.editorName,
      circular = _this$props3.circular;

    // let w = window,
    //     d = document,
    //     e = d.documentElement,
    //     g = d.getElementsByTagName("body")[0],
    //     width = w.innerWidth || e.clientWidth || g.clientWidth,
    //     height = w.innerHeight || e.clientHeight || g.clientHeight;

    // const width =  670;
    // const height = 900;
    return circular ? /*#__PURE__*/React.createElement(CircularView, {
      noWarnings: true,
      noInteractions: true,
      editorName: editorName
    }) : /*#__PURE__*/React.createElement(LinearView, {
      noWarnings: true,
      noInteractions: true,
      editorName: editorName
    });
  };
  return ComponentToPrint;
}(React.Component);