import assert from "node:assert/strict";
import { describe, it } from "node:test";
import { layoutBuildingGlyph } from "../engine/buildingGlyph.ts";
import type { BuildingGlyph } from "../engine/types.ts";

function glyph(overrides: Partial<BuildingGlyph> = {}): BuildingGlyph {
  return {
    kind: "building",
    width: 40,
    depth: 30,
    height: 100,
    storeys: 24,
    heading: 0,
    profile: "block",
    ...overrides,
  };
}

describe("map building glyph layouts", () => {
  it("steps a tower inward without changing its declared height", () => {
    const segments = layoutBuildingGlyph(glyph({ profile: "tower" }));
    assert.equal(segments.length, 3);
    assert.ok(segments[1]!.width < segments[0]!.width);
    assert.ok(segments[2]!.width < segments[1]!.width);
    assert.equal(Math.max(...segments.map((s) => s.base + s.height)), 100);
  });

  it("leaves a real hole inside a courtyard ring", () => {
    const segments = layoutBuildingGlyph(
      glyph({ profile: "courtyard", width: 36, depth: 26, height: 9 }),
    );
    assert.equal(segments.length, 4);

    const north = segments[0]!;
    const west = segments[2]!;
    const openWidth = 36 - west.width * 2;
    const openDepth = 26 - north.depth * 2;
    assert.ok(openWidth > 0, `courtyard closes across its width: ${openWidth}`);
    assert.ok(openDepth > 0, `courtyard closes across its depth: ${openDepth}`);
  });

  it("reserves the top of a hangar for its pitched roof", () => {
    const [body] = layoutBuildingGlyph(glyph({ profile: "hangar", height: 11 }));
    assert.ok(body);
    assert.equal(body.base, 0);
    assert.equal(body.height, 11 * 0.72);
  });

  it("keeps undersized authored footprints renderable", () => {
    const [segment] = layoutBuildingGlyph(
      glyph({ width: 0, depth: -2, height: 0, profile: "block" }),
    );
    assert.ok(segment);
    assert.deepEqual(
      { width: segment.width, depth: segment.depth, height: segment.height },
      { width: 4, depth: 4, height: 3 },
    );
  });
});
