const assert = require("assert");

const {
  splitTextIntoGraphemes,
  normalizeTextLayoutMode,
  isCurvedTextLayout,
  verticalizeText,
  buildArcGlyphLayout,
} = require("../static/text-layouts.js");

assert.deepStrictEqual(splitTextIntoGraphemes("AB光盘"), ["A", "B", "光", "盘"]);

assert.strictEqual(normalizeTextLayoutMode("vertical"), "vertical");
assert.strictEqual(normalizeTextLayoutMode("arcDown"), "arcDown");
assert.strictEqual(normalizeTextLayoutMode("unknown"), "horizontal");
assert.strictEqual(isCurvedTextLayout("arcUp"), true);
assert.strictEqual(isCurvedTextLayout("arcDown"), true);
assert.strictEqual(isCurvedTextLayout("vertical"), false);

assert.strictEqual(verticalizeText("ABC"), "A\nB\nC");
assert.strictEqual(verticalizeText("AB\nCD"), "A\nB\n\nC\nD");

{
  const layout = buildArcGlyphLayout({
    widths: [10, 10, 10],
    radius: 100,
    letterSpacing: 2,
    bend: "up",
  });
  assert.strictEqual(layout.glyphs.length, 3);
  assert(Math.abs(layout.glyphs[1].x) < 0.001, "middle glyph should stay centered");
  assert(layout.glyphs[0].y > 0, "up arc should place side glyphs lower than center");
  assert(layout.glyphs[2].y > 0, "up arc should place side glyphs lower than center");
  assert(layout.glyphs[0].angle < 0, "left glyph should rotate counter-clockwise on up arc");
  assert(layout.glyphs[2].angle > 0, "right glyph should rotate clockwise on up arc");
}

{
  const layout = buildArcGlyphLayout({
    widths: [10, 10, 10],
    radius: 100,
    letterSpacing: 2,
    bend: "down",
  });
  assert(layout.glyphs[0].y < 0, "down arc should place side glyphs above center");
  assert(layout.glyphs[2].y < 0, "down arc should place side glyphs above center");
}

console.log("text-layout tests passed");
