;; Tile Grid effect - tiles image in grid
(require-primitives "geometry" "image")

(define-effect tile_grid
  :params (
    (rows :type int :default 2 :range [1 10])
    (cols :type int :default 2 :range [1 10])
    (gap :type int :default 0 :range [0 50])
  )
  (let* ((w (image:width frame))
         (h (image:height frame))
         (tile-w (floor (/ (- w (* gap (- cols 1))) cols)))
         (tile-h (floor (/ (- h (* gap (- rows 1))) rows)))
         (tile (image:resize frame tile-w tile-h "area"))
         (result (make-image w h (list 0 0 0))))
    (begin
      ;; Manually place tiles using nested iteration
      ;; This is a simplified version - full version would loop
      (paste result tile 0 0)
      (if (> cols 1)
          (paste result tile (+ tile-w gap) 0)
          nil)
      (if (> rows 1)
          (paste result tile 0 (+ tile-h gap))
          nil)
      (if (and (> cols 1) (> rows 1))
          (paste result tile (+ tile-w gap) (+ tile-h gap))
          nil)
      result)))
