import { isBulletOrOrderedList } from './isBulletOrOrderedList';
import { isTaskList } from './isTaskList';
import { transformListRecursively } from './list-transforms';
import { getSupportedListTypesSet } from './list-utils';

/**
 * Transform list structure between different list types
 */
export var transformListStructure = function transformListStructure(context) {
  var tr = context.tr,
    sourceNode = context.sourceNode,
    sourcePos = context.sourcePos,
    targetNodeType = context.targetNodeType;
  var nodes = tr.doc.type.schema.nodes;
  var unsupportedContent = [];
  var onhandleUnsupportedContent = function onhandleUnsupportedContent(content) {
    unsupportedContent.push(content);
  };
  try {
    var listNode = {
      node: sourceNode,
      pos: sourcePos
    };
    var sourceList = listNode.node,
      listPos = listNode.pos;
    // const { taskList, listItem, taskItem, paragraph } = nodes;

    var isSourceBulletOrOrdered = isBulletOrOrderedList(sourceList.type);
    var isTargetTask = isTaskList(targetNodeType);
    var isSourceTask = isTaskList(sourceList.type);
    var isTargetBulletOrOrdered = isBulletOrOrderedList(targetNodeType);
    var supportedListTypes = getSupportedListTypesSet(nodes);
    var newList = transformListRecursively({
      isSourceBulletOrOrdered: isSourceBulletOrOrdered,
      isSourceTask: isSourceTask,
      isTargetBulletOrOrdered: isTargetBulletOrOrdered,
      isTargetTask: isTargetTask,
      listNode: sourceList,
      schema: tr.doc.type.schema,
      supportedListTypes: supportedListTypes,
      targetNodeType: targetNodeType
    }, onhandleUnsupportedContent);
    tr.replaceWith(listPos, listPos + sourceList.nodeSize, [newList].concat(unsupportedContent));
    return tr;
  } catch (_unused) {
    return tr;
  }
};