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 _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 _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
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); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
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(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _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); }
import { EditorState, Plugin, PluginKey } from 'prosemirror-state';
import { TextSelection } from 'prosemirror-state';
import { EditorView } from 'prosemirror-view';
import { MARK_LINK } from './MarkNames.js';
import { hideSelectionPlaceholder } from './SelectionPlaceholderPlugin.js';
import { applyMark } from '@modusoperandi/licit-ui-commands';
import { findNodesWithSameMark } from '@modusoperandi/licit-ui-commands';
import lookUpElement from './lookUpElement.js';
import LinkTooltip from './ui/LinkTooltip.js';
import LinkURLEditor from './ui/LinkURLEditor.js';
import { atAnchorTopCenter } from '@modusoperandi/licit-ui-commands';
import { createPopUp } from '@modusoperandi/licit-ui-commands';
import '@modusoperandi/licit-ui-commands/ui/czi-pop-up.css';

// https://prosemirror.net/examples/tooltip/
var SPEC = {
  // [FS] IRAD-1005 2020-07-07
  // Upgrade outdated packages.
  key: new PluginKey('LinkTooltipPlugin'),
  view: function view(editorView) {
    return new LinkTooltipView(editorView);
  }
};
var LinkTooltipPlugin = /*#__PURE__*/function (_Plugin) {
  function LinkTooltipPlugin() {
    _classCallCheck(this, LinkTooltipPlugin);
    return _callSuper(this, LinkTooltipPlugin, [SPEC]);
  }
  _inherits(LinkTooltipPlugin, _Plugin);
  return _createClass(LinkTooltipPlugin);
}(Plugin);
var LinkTooltipView = /*#__PURE__*/function () {
  function LinkTooltipView(editorView) {
    var _this = this;
    _classCallCheck(this, LinkTooltipView);
    _defineProperty(this, "_anchorEl", null);
    _defineProperty(this, "_popup", null);
    _defineProperty(this, "_editor", null);
    _defineProperty(this, "_onCancel", function (view) {
      _this.destroy();
      view.focus();
    });
    _defineProperty(this, "_onClose", function () {
      _this._anchorEl = null;
      _this._editor = null;
      _this._popup = null;
    });
    _defineProperty(this, "_onEdit", function (view) {
      if (_this._editor) {
        return;
      }
      var state = view.state;
      var schema = state.schema,
        doc = state.doc,
        selection = state.selection;
      var from = selection.from,
        to = selection.to;
      var markType = schema.marks[MARK_LINK];
      var result = findNodesWithSameMark(doc, from, to, markType);
      if (!result) {
        return;
      }
      var href = result.mark.attrs.href;
      _this._editor = createPopUp(LinkURLEditor, {
        href: href
      }, {
        onClose: function onClose(value) {
          _this._editor = null;
          _this._onEditEnd(view, selection, value);
        }
      });
    });
    _defineProperty(this, "_onRemove", function (view) {
      _this._onEditEnd(view, view.state.selection, null);
    });
    _defineProperty(this, "_onEditEnd", function (view, initialSelection, href) {
      var state = view.state,
        dispatch = view.dispatch;
      var tr = hideSelectionPlaceholder(state);
      if (href !== undefined) {
        var schema = state.schema;
        var markType = schema.marks[MARK_LINK];
        if (markType) {
          var result = findNodesWithSameMark(tr.doc, initialSelection.from, initialSelection.to, markType);
          if (result) {
            var linkSelection = TextSelection.create(tr.doc, result.from.pos, result.to.pos + 1);
            tr = tr.setSelection(linkSelection);
            var attrs = href ? {
              href: href
            } : null;
            tr = applyMark(tr, schema, markType, attrs);

            // [FS] IRAD-1005 2020-07-09
            // Upgrade outdated packages.
            // reset selection to original using the latest doc.
            var origSelection = TextSelection.create(tr.doc, initialSelection.from, initialSelection.to);
            tr = tr.setSelection(origSelection);
          }
        }
      }
      dispatch(tr);
      view.focus();
    });
    this.update(editorView, null);
  }
  return _createClass(LinkTooltipView, [{
    key: "update",
    value: function update(view, lastState) {
      if (view.readOnly) {
        this.destroy();
        return;
      }
      var state = view.state;
      var doc = state.doc,
        selection = state.selection,
        schema = state.schema;
      var markType = schema.marks[MARK_LINK];
      if (!markType) {
        return;
      }
      var from = selection.from,
        to = selection.to;
      var result = findNodesWithSameMark(doc, from, to, markType);
      if (!result) {
        this.destroy();
        return;
      }
      var domFound = view.domAtPos(from);
      if (!domFound) {
        this.destroy();
        return;
      }
      var anchorEl = lookUpElement(domFound.node, function (el) {
        return el.nodeName === 'A';
      });
      if (!anchorEl) {
        this.destroy();
        return;
      }
      var popup = this._popup;
      var viewPops = {
        editorState: state,
        editorView: view,
        href: result.mark.attrs.href,
        onCancel: this._onCancel,
        onEdit: this._onEdit,
        onRemove: this._onRemove
      };
      if (popup && anchorEl === this._anchorEl) {
        popup.update(viewPops);
      } else {
        popup && popup.close();
        this._anchorEl = anchorEl;
        this._popup = createPopUp(LinkTooltip, viewPops, {
          anchor: anchorEl,
          autoDismiss: false,
          onClose: this._onClose,
          position: atAnchorTopCenter
        });
      }
    }
  }, {
    key: "destroy",
    value: function destroy() {
      this._popup && this._popup.close();
      this._editor && this._editor.close();
    }
  }]);
}();
export default LinkTooltipPlugin;