"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "createRule", {
  enumerable: true,
  get: function get() {
    return _createRule.createRule;
  }
});
exports.inputRuleWithAnalytics = exports.createWrappingJoinRule = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _transform = require("@atlaskit/editor-prosemirror/transform");
var _analytics = require("../analytics");
var _createRule = require("./createRule");
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
// Roughly based on atlassian-frontend/packages/editor/editor-core/src/utils/input-rules.ts but with the Editor Analytics API that's injected in plugins
var inputRuleWithAnalytics = exports.inputRuleWithAnalytics = function inputRuleWithAnalytics(getPayload, analyticsApi) {
  return function (originalRule) {
    var onHandlerApply = function onHandlerApply(state, tr, matchResult) {
      var payload = typeof getPayload === 'function' ? getPayload(state, matchResult) : getPayload;
      if (payload && payload.attributes) {
        payload.attributes.formatSize = typeof matchResult[0] === 'string' ? matchResult[0].length : 0;
      }
      analyticsApi === null || analyticsApi === void 0 || analyticsApi.attachAnalyticsEvent(payload)(tr);
      if (originalRule.onHandlerApply) {
        originalRule.onHandlerApply(state, tr, matchResult);
      }
    };
    return _objectSpread(_objectSpread({}, originalRule), {}, {
      onHandlerApply: onHandlerApply
    });
  };
};
// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
var createWrappingJoinRule = exports.createWrappingJoinRule = function createWrappingJoinRule(_ref) {
  var match = _ref.match,
    nodeType = _ref.nodeType,
    getAttrs = _ref.getAttrs,
    joinPredicate = _ref.joinPredicate;
  var handler = function handler(state, match, start, end) {
    var attrs = (getAttrs instanceof Function ? getAttrs(match) : getAttrs) || {};
    var tr = state.tr;
    var fixedStart = Math.max(start, 1);
    tr.delete(fixedStart, end);
    var $start = tr.doc.resolve(fixedStart);
    var range = $start.blockRange();
    var wrapping = range && (0, _transform.findWrapping)(range, nodeType, attrs);
    if (!wrapping || !range) {
      return null;
    }
    var parentNodePosMapped = tr.mapping.map(range.start);
    var parentNode = tr.doc.nodeAt(parentNodePosMapped);
    var lastWrap = wrapping[wrapping.length - 1];
    if (parentNode && lastWrap) {
      var allowedMarks = lastWrap.type.allowedMarks(parentNode.marks) || [];
      tr.setNodeMarkup(parentNodePosMapped, parentNode.type, parentNode.attrs, allowedMarks);
    }
    tr.wrap(range, wrapping);
    var before = tr.doc.resolve(fixedStart - 1).nodeBefore;
    if (before && before.type === nodeType && (0, _transform.canJoin)(tr.doc, fixedStart - 1) && (!joinPredicate || joinPredicate(match, before, _analytics.JOIN_SCENARIOS_WHEN_TYPING_TO_INSERT_LIST.JOINED_TO_LIST_ABOVE))) {
      tr.join(fixedStart - 1);
    }
    return tr;
  };
  return (0, _createRule.createRule)(match, handler);
};
// eslint-disable-next-line @atlaskit/editor/no-re-export