"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.transformTaskListToBlockNodes = void 0;
var _model = require("@atlaskit/editor-prosemirror/model");
var _utils = require("@atlaskit/editor-prosemirror/utils");
var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals");
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
var transformTaskListToBlockNodes = exports.transformTaskListToBlockNodes = function transformTaskListToBlockNodes(context) {
  var tr = context.tr,
    targetNodeType = context.targetNodeType,
    targetAttrs = context.targetAttrs,
    sourceNode = context.sourceNode,
    sourcePos = context.sourcePos;
  var selection = tr.selection;
  var schema = selection.$from.doc.type.schema;
  var blockTaskItem = schema.nodes.blockTaskItem;

  // gating behind platform_editor_small_font_size to support task lists with font size applied,
  // but keep this solution general
  var isBlockTaskItemEnabled = !!blockTaskItem && (0, _expValEquals.expValEquals)('platform_editor_small_font_size', 'isEnabled', true);
  if (isBlockTaskItemEnabled) {
    var blockTaskItemsResult = (0, _utils.findChildrenByType)(sourceNode, blockTaskItem);
    if (blockTaskItemsResult.length > 0 && targetNodeType === schema.nodes.paragraph) {
      // blockTaskItem content is (paragraph | extension)+
      // Extract paragraph children directly — they may carry block marks (e.g. fontSize)
      var _targetNodes = [];
      var _iterator = _createForOfIteratorHelper(blockTaskItemsResult),
        _step;
      try {
        for (_iterator.s(); !(_step = _iterator.n()).done;) {
          var blockItem = _step.value.node;
          blockItem.forEach(function (child) {
            if (child.type === schema.nodes.paragraph) {
              _targetNodes.push(child);
            }
          });
        }
      } catch (err) {
        _iterator.e(err);
      } finally {
        _iterator.f();
      }
      if (_targetNodes.length === 0) {
        return null;
      }
      var _slice = new _model.Slice(_model.Fragment.fromArray(_targetNodes), 0, 0);
      var _rangeStart = sourcePos !== null ? sourcePos : selection.from;
      tr.replaceRange(_rangeStart, _rangeStart + sourceNode.nodeSize, _slice);
      return tr;
    }
  }

  // Original logic for regular taskItem children
  var taskItemsResult = (0, _utils.findChildrenByType)(sourceNode, schema.nodes.taskItem);
  var taskItems = taskItemsResult.map(function (item) {
    return item.node;
  });
  var taskItemFragments = taskItems.map(function (taskItem) {
    return taskItem.content;
  });
  var targetNodes = [];

  // Convert fragments to headings if target is heading
  if (targetNodeType === schema.nodes.heading && targetAttrs) {
    // convert the fragments to headings
    var targetHeadingLevel = targetAttrs.level;
    targetNodes = taskItemFragments.map(function (fragment) {
      return schema.nodes.heading.createChecked({
        level: targetHeadingLevel
      }, fragment.content);
    });
  }

  // Convert fragments to paragraphs if target is paragraphs
  if (targetNodeType === schema.nodes.paragraph) {
    // convert the fragments to paragraphs
    targetNodes = taskItemFragments.map(function (fragment) {
      return schema.nodes.paragraph.createChecked({}, fragment.content);
    });
  }

  // Convert fragments to code block if target is code block
  if (targetNodeType === schema.nodes.codeBlock) {
    // convert the fragments to one code block
    var codeBlockContent = taskItemFragments.map(function (fragment) {
      return fragment.textBetween(0, fragment.size, '\n');
    }).join('\n');
    targetNodes = [schema.nodes.codeBlock.createChecked({}, schema.text(codeBlockContent))];
  }

  // Replace the task list node with the new content in the transaction
  var slice = new _model.Slice(_model.Fragment.fromArray(targetNodes), 0, 0);
  var rangeStart = sourcePos !== null ? sourcePos : selection.from;
  tr.replaceRange(rangeStart, rangeStart + sourceNode.nodeSize, slice);
  return tr;
};