function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { Plugin, PluginKey } from 'prosemirror-state';
import { EditorView } from 'prosemirror-view';
import * as React from 'react';
import ReactDOM from 'react-dom';
import isEditorStateEmpty from './isEditorStateEmpty.js';
var CLASS_NAME_HAS_PLACEHOLDER = 'czi-has-placeholder';
var ContentPlaceholderView = /*#__PURE__*/function () {
  function ContentPlaceholderView(editorView) {
    var _this = this;
    _classCallCheck(this, ContentPlaceholderView);
    _defineProperty(this, "_el", null);
    _defineProperty(this, "_focused", null);
    _defineProperty(this, "_view", null);
    _defineProperty(this, "_visible", null);
    _defineProperty(this, "_checkFocus", function () {
      var el = _this._el;
      var view = _this._view;
      if (!view || !el) {
        return;
      }
      var doc = document;
      var activeElement = doc.activeElement;
      var bodyEl = _this._getBodyElement();
      if (!activeElement || !bodyEl || doc.hasFocus && !doc.hasFocus()) {
        _this._onBlur();
      } else if (activeElement === bodyEl || bodyEl.contains(activeElement) || activeElement === bodyEl.parentNode) {
        _this._onFocus();
      } else {
        _this._onBlur();
      }
    });
    var _el = document.createElement('div');
    this._el = _el;
    this._view = editorView;
    _el.className = 'czi-editor-content-placeholder';
    editorView.dom.parentNode.appendChild(_el);
    document.addEventListener('focusin', this._checkFocus, true);
    document.addEventListener('focusout', this._checkFocus, false);
    this.update(editorView);

    // We don't know whether view is focused at this moment yet. Manually
    // calls `this._checkFocus` which will set `_focused` accordingly.
    this._checkFocus();
  }
  return _createClass(ContentPlaceholderView, [{
    key: "update",
    value: function update(view) {
      this._view = view;
      var el = this._el;
      if (!el || !view) {
        return;
      }
      if (this._focused || !isEditorStateEmpty(view.state)) {
        this._hide();
        return;
      }
      var parentEl = el.parentNode;
      var bodyEl = this._getBodyElement();
      var placeholder = view.placeholder;
      if (!parentEl || !bodyEl || !placeholder) {
        return;
      }
      this._visible = true;
      view.dom.classList.add(CLASS_NAME_HAS_PLACEHOLDER);
      var parentElRect = parentEl.getBoundingClientRect();
      var bodyRect = bodyEl.getBoundingClientRect();
      var bodyStyle = window.getComputedStyle(bodyEl);
      var left = bodyRect.left - parentElRect.left;
      var top = bodyRect.top - parentElRect.top;
      el.style.left = left + 'px';
      el.style.top = top + 'px';
      el.style.padding = bodyStyle.padding;
      el.style.display = 'block';
      el.style.width = bodyEl.offsetWidth + 'px';
      ReactDOM.render(/*#__PURE__*/React.createElement("div", null, placeholder), el);
    }
  }, {
    key: "destroy",
    value: function destroy() {
      var _el$parentNode;
      this._hide();
      var el = this._el;
      el === null || el === void 0 || (_el$parentNode = el.parentNode) === null || _el$parentNode === void 0 || _el$parentNode.removeChild(el);
      if (el) {
        ReactDOM.unmountComponentAtNode(el);
      }
      document.removeEventListener('focusin', this._checkFocus, true);
      document.removeEventListener('focusout', this._checkFocus, false);
      this._view = null;
      this._el = null;
      this._focused = false;
      this._visible = false;
    }
  }, {
    key: "_onFocus",
    value: function _onFocus() {
      if (this._focused !== true) {
        this._focused = true;
        this._hide();
      }
    }
  }, {
    key: "_onBlur",
    value: function _onBlur() {
      var view = this._view;
      if (this._focused !== false) {
        this._focused = false;
        if (view && isEditorStateEmpty(view.state)) {
          this._show();
        } else {
          this._hide();
        }
      }
    }
  }, {
    key: "_getBodyElement",
    value: function _getBodyElement() {
      var _view$docView;
      var view = this._view;
      return view === null || view === void 0 || (_view$docView = view.docView) === null || _view$docView === void 0 || (_view$docView = _view$docView.dom) === null || _view$docView === void 0 ? void 0 : _view$docView.firstChild;
    }
  }, {
    key: "_show",
    value: function _show() {
      if (this._visible !== true) {
        var el = this._el;
        var view = this._view;
        this._visible = true;
        if (el) {
          el.style.display = 'block';
        }
        if (view) {
          this.update(view);
          view.dom.classList.add(CLASS_NAME_HAS_PLACEHOLDER);
        }
      }
    }
  }, {
    key: "_hide",
    value: function _hide() {
      if (this._visible !== false) {
        var el = this._el;
        var view = this._view;
        this._visible = false;
        if (el) {
          el.style.display = 'none';
        }
        if (view) {
          view.dom.classList.remove(CLASS_NAME_HAS_PLACEHOLDER);
        }
      }
    }
  }]);
}();
var ContentPlaceholderPlugin = /*#__PURE__*/function (_Plugin) {
  function ContentPlaceholderPlugin() {
    _classCallCheck(this, ContentPlaceholderPlugin);
    return _callSuper(this, ContentPlaceholderPlugin, [{
      // [FS] IRAD-1005 2020-07-07
      // Upgrade outdated packages.
      key: new PluginKey('ContentPlaceholderPlugin'),
      view: function view(editorView) {
        return new ContentPlaceholderView(editorView);
      }
    }]);
  }
  _inherits(ContentPlaceholderPlugin, _Plugin);
  return _createClass(ContentPlaceholderPlugin);
}(Plugin);
export default ContentPlaceholderPlugin;