import test from "node:test";
import assert from "node:assert/strict";
import {
    buildDropIndicatorText,
    computeSpanTargetStart,
    mergeBlockTexts,
    moveContiguousSpan,
} from "./editor-block-ops";

test("mergeBlockTexts joins with blank lines", () => {
    assert.equal(mergeBlockTexts(["A", "B", "C"]), "A\n\nB\n\nC");
});

test("computeSpanTargetStart keeps position when dropping into current span", () => {
    const next = computeSpanTargetStart(6, 2, 3, 3);
    assert.equal(next, 2);
});

test("computeSpanTargetStart moves span upward", () => {
    const next = computeSpanTargetStart(6, 3, 4, 1);
    assert.equal(next, 1);
});

test("computeSpanTargetStart moves span downward", () => {
    const next = computeSpanTargetStart(6, 1, 2, 5);
    assert.equal(next, 3);
});

test("moveContiguousSpan reorders blocks upward", () => {
    const source = ["A", "B", "C", "D", "E"];
    const moved = moveContiguousSpan(source, 2, 3, 1);
    assert.equal(moved.changed, true);
    assert.equal(moved.targetStart, 1);
    assert.deepEqual(moved.reordered, ["A", "C", "D", "B", "E"]);
});

test("moveContiguousSpan reorders blocks to document end", () => {
    const source = ["A", "B", "C", "D", "E"];
    const moved = moveContiguousSpan(source, 1, 2, 5);
    assert.equal(moved.changed, true);
    assert.equal(moved.targetStart, 3);
    assert.deepEqual(moved.reordered, ["A", "D", "E", "B", "C"]);
});

test("moveContiguousSpan does not change on internal drop", () => {
    const source = ["A", "B", "C", "D", "E"];
    const moved = moveContiguousSpan(source, 1, 3, 2);
    assert.equal(moved.changed, false);
    assert.equal(moved.targetStart, 1);
    assert.deepEqual(moved.reordered, source);
});

test("buildDropIndicatorText covers start, middle and end", () => {
    assert.equal(buildDropIndicatorText(0, 4), "将插入到第 1 块之前");
    assert.equal(buildDropIndicatorText(2, 4), "将插入到第 3 块之前");
    assert.equal(buildDropIndicatorText(9, 4), "将插入到文档末尾");
});
