# common/ui — the Vue binding of spr's UI contract.
#
# Paths are derived from where this file sits. The logic lives in pnpm and in
# common/slice, never here: every target is one line.
#
#   make install        node_modules, offline when the store has it
#   make check          everything that must be green before a change here lands
#   make docs           launch spr's docs site, open at this library's reference —
#                       in soleprint, `make docs` always launches the /docs minisite
#   make reference      regenerate that reference page from src/ (never hand-edited)
#   make reference-spots   every place a hand-written note (notes/) can go
#   make slices         what a consumer can take, and what each piece pulls in
#
# This folder is also copied whole into consumers by ctrl/spr.py. There, the
# pnpm targets still work; the docs/reference/slices/parts targets need spr around them
# and say so instead of failing with a traceback.

HERE      := $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
SOLEPRINT := $(abspath $(HERE)/../..)
PY        ?= python3
PNPM      ?= pnpm
SLICE     := cd $(SOLEPRINT) && $(PY) -m common.slice
IN_SPR    := $(wildcard $(HERE)/../slice/__init__.py)

# The one guard every spr-side target shares: a copy of this folder elsewhere has
# no common/slice beside it.
need-spr = $(if $(IN_SPR),,$(error $@ needs spr around this folder — docs, reference, slices and parts live in spr, not in a copy))

.DEFAULT_GOAL := help
.PHONY: help install typecheck test build check docs reference reference-check reference-spots reference-scaffold slices parts-check clean

help:  ## List every target
	@echo "common/ui — the Vue binding of spr's UI contract"
	@echo
	@grep -E '^[a-z-]+:.*?## .*$$' $(MAKEFILE_LIST) \
	  | awk 'BEGIN{FS=":.*?## "}{printf "  \033[1m%-19s\033[0m %s\n", $$1, $$2}'

install:  ## node_modules (pnpm, offline when the store already has it)
	cd $(HERE) && $(PNPM) install --prefer-offline

typecheck:  ## vue-tsc over src/
	cd $(HERE) && $(PNPM) typecheck

test:  ## vitest
	cd $(HERE) && $(PNPM) test

build:  ## dist/soleprint-ui.js + dist/style.css (the whole-library bundle; consumers take slices)
	cd $(HERE) && $(PNPM) build

check: typecheck test reference-check parts-check  ## typecheck, test, reference current, parts agree with their sources
	@echo "common/ui: all green"

docs:  ## launch spr's docs site (the /docs minisite), open at #ui-reference
	$(need-spr)
	@echo "the UI reference: http://localhost:$${DOCS_PORT:-8080}/#ui-reference"
	bash $(SOLEPRINT)/../ctrl/docs.sh serve

reference:  ## regenerate the ui-reference page from src/ (refuses to overwrite a hand edit; FORCE=1 to discard it)
	$(need-spr)
	$(SLICE) reference $(if $(FORCE),--force)

reference-check:  ## the page current, no hand edits, every note on a spot — writes nothing
	$(need-spr)
	$(SLICE) reference --check

reference-spots:  ## every spot a note (notes/) can go, and which have one
	$(need-spr)
	$(SLICE) reference --spots

reference-scaffold:  ## an empty stub in notes/ for every spot without a note (never overwrites)
	$(need-spr)
	$(SLICE) reference --scaffold

slices:  ## what can be sliced, and what each export or part pulls in
	$(need-spr)
	$(SLICE) list

parts-check:  ## the plain parts in common/theme still agree with the files here they came from
	$(need-spr)
	cd $(SOLEPRINT) && $(PY) common/theme/bake.py --check >/dev/null && echo "parts: agree with their sources; pages current"

clean:  ## remove dist/
	rm -rf $(HERE)/dist
