text: "use strict";
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.default = void 0;
  
  var _isEqual2 = _interopRequireDefault(require("lodash/isEqual"));
  
  var _get2 = _interopRequireDefault(require("lodash/get"));
  
  var _react = _interopRequireDefault(require("react"));
  
  var _default = _interopRequireDefault(require("part:@sanity/components/dialogs/default"));
  
  var _content = _interopRequireDefault(require("part:@sanity/components/dialogs/content"));
  
  var _fullscreen = _interopRequireDefault(require("part:@sanity/components/dialogs/fullscreen"));
  
  var _fold = _interopRequireDefault(require("part:@sanity/components/edititem/fold"));
  
  var _popover = _interopRequireDefault(require("part:@sanity/components/dialogs/popover"));
  
  var _stacked = _interopRequireDefault(require("part:@sanity/components/utilities/stacked"));
  
  var _escapable = _interopRequireDefault(require("part:@sanity/components/utilities/escapable"));
  
  var _slateReact = require("slate-react");
  
  var _FormBuilderInput = require("../../FormBuilderInput");
  
  var _PatchEvent = require("../../PatchEvent");
  
  var _EditNode = _interopRequireDefault(require("./styles/EditNode.css"));
  
  var _getKey = require("./utils/getKey");
  
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  
  class EditNode extends _react.default.Component {
    constructor() {
      super(...arguments);
  
      _defineProperty(this, "handleChange", patchEvent => {
        var _this$props = this.props,
            onPatch = _this$props.onPatch,
            path = _this$props.path,
            value = _this$props.value,
            onFocus = _this$props.onFocus,
            focusPath = _this$props.focusPath;
        var _patchEvent = patchEvent;
        path.slice(0).reverse().forEach(segment => {
          _patchEvent = _patchEvent.prefixAll(segment);
        }); // Intercept patches that unsets markDefs.
        // The child using the markDef must have that mark removed,
        // so insert patches that rewrite that block without the mark
  
        _patchEvent.patches.forEach((patch, index) => {
          if (patch.path.length === 3 && patch.path[1] === 'markDefs' && patch.type === 'unset') {
            var block = value && value.find(blk => blk._key === (0, _getKey.getKey)(patch.path[0]));
  
            var _block = _objectSpread({}, block);
  
            var markKey = (0, _getKey.getKey)(patch.path[2]);
  
            _block.children.forEach(child => {
              if (child.marks) {
                child.marks = child.marks.filter(mark => mark !== markKey);
              }
            });
  
            var blockPath = [{
              _key: _block._key
            }];
            _block.markDefs = _block.markDefs.filter(def => def._key !== markKey);
  
            _patchEvent.patches.splice(index + 1, 0, (0, _PatchEvent.set)(_block, blockPath)); // Set focus away from the annotation, and to the block itself
  
  
            if (focusPath && (0, _isEqual2.default)(patch.path, focusPath.slice(0, patch.path.length))) {
              onFocus(blockPath);
            }
          }
        });
  
        onPatch(_patchEvent);
      });
  
      _defineProperty(this, "handleClose", () => {
        var _this$props2 = this.props,
            focusPath = _this$props2.focusPath,
            onFocus = _this$props2.onFocus,
            editor = _this$props2.editor;
        onFocus(focusPath.slice(0, 1));
  
        if (!this.props.readOnly) {
          editor.command('focusNoScroll');
        }
      });
  
      _defineProperty(this, "handleDialogAction", () => {// NOOP
      });
    }
  
    renderInput() {
      var _this$props3 = this.props,
          nodeValue = _this$props3.nodeValue,
          type = _this$props3.type,
          onFocus = _this$props3.onFocus,
          readOnly = _this$props3.readOnly,
          focusPath = _this$props3.focusPath,
          path = _this$props3.path,
          markers = _this$props3.markers;
      return /*#__PURE__*/_react.default.createElement("div", {
        className: _EditNode.default.formBuilderInputWrapper
      }, /*#__PURE__*/_react.default.createElement(_FormBuilderInput.FormBuilderInput // @ts-ignore (fixme: this is another type than the FormBuilderInput expects - turn into an common interface instead)
      , {
        type: type,
        level: 0,
        readOnly: readOnly || type.readOnly,
        value: nodeValue,
        onChange: this.handleChange,
        onFocus: onFocus,
        focusPath: focusPath,
        path: path,
        markers: markers
      }));
    }
  
    renderWrapper() {
      var _this$props4 = this.props,
          type = _this$props4.type,
          node = _this$props4.node;
      var nodeRef = (0, _slateReact.findDOMNode)(node);
      var editModalLayout = (0, _get2.default)(type.options, 'editModal');
      var title = type.title;
  
      if (editModalLayout === 'fullscreen') {
        return /*#__PURE__*/_react.default.createElement(_fullscreen.default, {
          isOpen: true,
          title: title,
          onClose: this.handleClose
        }, this.renderInput());
      }
  
      if (editModalLayout === 'fold') {
        return /*#__PURE__*/_react.default.createElement("div", {
          className: _EditNode.default.editBlockContainerFold
        }, /*#__PURE__*/_react.default.createElement(_fold.default, {
          isOpen: true,
          title: title,
          onClose: this.handleClose
        }, this.renderInput()));
      }
  
      if (editModalLayout === 'popover') {
        return /*#__PURE__*/_react.default.createElement(_popover.default, {
          placement: "bottom",
          referenceElement: nodeRef,
          onClickOutside: this.handleClose,
          onEscape: this.handleClose,
          onClose: this.handleClose,
          title: title,
          padding: "none"
        }, /*#__PURE__*/_react.default.createElement(_content.default, {
          size: "medium",
          padding: "small"
        }, this.renderInput()));
      }
  
      return /*#__PURE__*/_react.default.createElement(_default.default, {
        isOpen: true,
        title: title,
        onClose: this.handleClose,
        showCloseButton: true,
        onAction: this.handleDialogAction
      }, /*#__PURE__*/_react.default.createElement(_content.default, {
        size: "medium"
      }, this.renderInput()));
    }
  
    render() {
      var nodeValue = this.props.nodeValue;
  
      if (!nodeValue) {
        return /*#__PURE__*/_react.default.createElement("div", null, "No value???");
      }
  
      return /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_stacked.default, null, isActive => /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_escapable.default, {
        onEscape: event => isActive && this.handleClose()
      }), this.renderWrapper())));
    }
  
  }
  
  exports.default = EditNode;
  
  _defineProperty(EditNode, "defaultProps", {
    readOnly: false
  });