"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.LazyNodeView = void 0;
Object.defineProperty(exports, "makeNodePlaceholderId", {
  enumerable: true,
  get: function get() {
    return _makeNodePlaceholderId.makeNodePlaceholderId;
  }
});
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _memoize = _interopRequireDefault(require("lodash/memoize"));
var _model = require("@atlaskit/editor-prosemirror/model");
var _makeNodePlaceholderId = require("./makeNodePlaceholderId");
var getEditorLineWidth = (0, _memoize.default)(function (view) {
  var _view$dom;
  return (_view$dom = view.dom) === null || _view$dom === void 0 ? void 0 : _view$dom.clientWidth;
});

// Copied from ProseMirror NodeView
// https://github.com/ProseMirror/prosemirror-view/blob/cfa73eb969777f63bcb39972594fd4a9110f5a93/src/viewdesc.ts#L1095-L1099
function sameOuterDeco(a, b) {
  if (a.length !== b.length) {
    return false;
  }
  for (var i = 0; i < a.length; i++) {
    // @ts-expect-error type actually exist on decoration at runtime
    if (!a[i].type) {
      return false;
    }

    // @ts-expect-error type actually exist on decoration at runtime
    if (!a[i].type.eq(b[i].type)) {
      return false;
    }
  }
  return true;
}

/**
 * 🧱 Internal: Editor FE Platform
 *
 * A NodeView that serves as a placeholder until the actual NodeView is loaded.
 */
var LazyNodeView = exports.LazyNodeView = /*#__PURE__*/function () {
  function LazyNodeView(_node, view, _getPos, _outerDeco) {
    var _this = this,
      _node$type;
    (0, _classCallCheck2.default)(this, LazyNodeView);
    (0, _defineProperty2.default)(this, "update", function (node, outerDeco) {
      var prevNode = _this.node;
      _this.node = node;
      if (!sameOuterDeco(outerDeco, _this.outerDeco)) {
        return false;
      }

      // Copying some of the default NodeView update behaviour
      // https://github.com/ProseMirror/prosemirror-view/blob/cfa73eb969777f63bcb39972594fd4a9110f5a93/src/viewdesc.ts#L803
      return !_this.node.sameMarkup(prevNode);
    });
    this.node = _node;
    this.outerDeco = _outerDeco;
    if (typeof ((_node$type = _node.type) === null || _node$type === void 0 || (_node$type = _node$type.spec) === null || _node$type === void 0 ? void 0 : _node$type.toDOM) !== 'function') {
      this.dom = document.createElement('div');
      return;
    }
    var toDOMConfiguration = {
      editorLineWidth: getEditorLineWidth(view)
    };
    var fallback = _model.DOMSerializer.renderSpec(document, _node.type.spec.toDOM(_node,
    // We are injecting a second parameter to be used by the toDOM lazy node view implementations
    // @ts-expect-error
    toDOMConfiguration));
    this.dom = fallback.dom;
    this.contentDOM = fallback.contentDOM;
    if (this.dom instanceof HTMLElement) {
      // This attribute is mostly used for debugging purposed
      // It will help us to found out when the node was replaced
      this.dom.setAttribute('data-lazy-node-view', _node.type.name);
      // This is used on Libra tests
      // We are using this to make sure all lazy noded were replaced
      // before the test started
      this.dom.setAttribute('data-lazy-node-view-fallback', 'true');

      // This is used by the UFO to identify the node
      this.dom.setAttribute('data-vc', "editor-lnv-fallback--".concat(_node.type.name));
      var pos = _getPos();
      if (pos !== undefined) {
        // Used for UFO to identify this as a placeholder
        this.dom.setAttribute('data-editor-lnv-placeholder', (0, _makeNodePlaceholderId.makeNodePlaceholderId)(_node.type.name, pos));
      }
    }
  }
  return (0, _createClass2.default)(LazyNodeView, [{
    key: "ignoreMutation",
    value: function ignoreMutation() {
      if (this.node.type.isTextblock) {
        return false;
      }
      return true;
    }
  }]);
}(); // eslint-disable-next-line @atlaskit/editor/no-re-export