;;; dag-draw-ascii-grid.el --- ASCII grid management for dag-draw -*- lexical-binding: t -*-

;; Copyright (C) 2024, 2025

;; Author: Generated by Claude
;; Keywords: internal

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;;; Commentary:

;; ASCII Grid Rendering - GKNV Adaptation
;;
;; BOUNDED CONTEXT: ASCII Rendering Context
;; UPSTREAM CONTEXT: GKNV Algorithm Context (via Coordinate Transform Layer)
;; LAYER: Rendering Implementation
;; AUTHORITY: doc/implementation-decisions.md (D5.1-D5.8)
;;
;; ASCII Grid Rendering - GKNV Adaptation:
;;
;; This module adapts the GKNV graph drawing algorithm for ASCII character grid
;; output.  The GKNV paper describes graphical (PostScript) output, so this module
;; implements ASCII-specific decisions from doc/implementation-decisions.md.
;;
;; ASCII Decisions: D5.1-D5.8 (Character-grid specific)
;; Algorithm: Junction character algorithm with context analysis
;;
;; Key Requirements:
;; - Unicode box-drawing characters (D5.2)
;; - 5 junction types: start-port, end-port, corner, merge/split, cross (D5.4)
;; - Walk-based local analysis for correct junction selection (D5.4)
;; - Arrows at port boundary (D5.5)
;; - Coordinate scaling X and Y independently (D5.1)
;;
;; Baseline Status: ✅ Compliant with ASCII adaptation decisions
;;
;; Note: GKNV paper does not cover ASCII rendering.  This module implements
;; the ASCII-specific decisions documented in implementation-decisions.md
;; while maintaining GKNV algorithm correctness for the underlying layout.
;;
;; See doc/implementation-decisions.md (D5.1-D5.8) for ASCII decision rationale.

;;; Code:

(require 'ht)
(require 'dag-draw)
(require 'dag-draw-core)
(require 'dag-draw-coord-transform)
(require 'dag-draw-ascii-junctions)










;;; ASCII Coordinate Context Layer (moved to dag-draw-coord-transform.el)
;; All coordinate transformation functions have been extracted to dag-draw-coord-transform.el

;;; ASCII Grid Creation

(defun dag-draw--create-ascii-grid (width height)
  "Create empty ASCII grid of given WIDTH and HEIGHT.

WIDTH and HEIGHT are integers representing grid dimensions in characters.

Returns a 2D vector where each row is a vector of characters, all initialized
to space characters."
  (let ((grid (make-vector height nil)))
    (dotimes (y height)
      (aset grid y (make-vector width ?\s)))  ; Fill with spaces
    grid))


 


(defun dag-draw--ascii-grid-to-string (grid)
  "Convert ASCII GRID to string representation.

GRID is a 2D vector of characters.

Converts each row to a string and joins with newlines.  Handles CJK/wide
characters correctly by normalizing display width across all rows.

Returns a string containing the complete ASCII representation."
  ;; Handle empty grid case
  (if (= (length grid) 0)
      ""
    ;; Convert each row to a string with trailing whitespace stripped, then
    ;; drop trailing blank rows so the output carries no padding noise.
    (let ((row-strings (mapcar (lambda (row)
                                 (string-trim-right (apply #'string (append row nil))))
                               grid)))
      (setq row-strings (nreverse row-strings))
      (while (and row-strings (zerop (length (car row-strings))))
        (setq row-strings (cdr row-strings)))
      (string-join (nreverse row-strings) "\n"))))

;;; Junction Character Enhancement (moved to dag-draw-ascii-junctions.el)
;; All junction-related functions have been extracted to dag-draw-ascii-junctions.el

;;; Edge Analysis for Junction Detection (moved to dag-draw-ascii-junctions.el)

;;; Local Grid Context Analysis (moved to dag-draw-ascii-junctions.el)

(provide 'dag-draw-ascii-grid)

;;; dag-draw-ascii-grid.el ends here
