import _defineProperty from "@babel/runtime/helpers/defineProperty";
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) { _defineProperty(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; }
import { traverse } from '@atlaskit/adf-utils/traverse';
import { Fragment } from '@atlaskit/editor-prosemirror/model';
/**
 * Promotes `panel` nodes to `panel_c1` wherever their content makes them invalid
 * as a standard `panel` but valid as a `panel_c1` (e.g. when a panel contains a table).
 *
 * Useful for insertion paths where content is being inserted as ADF and we want to
 * use the standard panel type where possible, but upgrade when required by the content.
 */
export function transformContainerNodesWhenRequired(adf, schema) {
  var isTransformed = false;
  var _schema$nodes = schema.nodes,
    _panel = _schema$nodes.panel,
    panel_c1 = _schema$nodes.panel_c1;
  if (!_panel || !panel_c1) {
    return {
      transformedAdf: adf,
      isTransformed: false
    };
  }
  var transformedAdf = traverse(adf, {
    panel: function panel(node) {
      if (!node.content) {
        return;
      }
      var fragment = Fragment.fromJSON(schema, node.content);
      if (!_panel.validContent(fragment) && panel_c1.validContent(fragment)) {
        isTransformed = true;
        return _objectSpread(_objectSpread({}, node), {}, {
          type: 'panel_c1'
        });
      }
      return;
    }
  });
  return {
    transformedAdf: transformedAdf,
    isTransformed: isTransformed
  };
}