#!/usr/bin/make -f

TSC    := tsc
ROLLUP := rollup

CORE_PACKAGES := packages/reactive-element packages/lit-html packages/lit-element \
	packages/lit packages/context

# Helper: install root-level JS/TS files and chosen subdirs for one package
# $(call install-pkg, src-dir, dest-dir, subdirs...)
define install-pkg
	install -d $(2)
	cp $(1)/package.json $(2)/
	find $(1) -maxdepth 1 \
		\( -name '*.js' -o -name '*.js.map' -o -name '*.d.ts' -o -name '*.d.ts.map' \) \
		-exec cp {} $(2)/ \;
	for d in $(3); do \
		if [ -d $(1)/$$d ]; then cp -r $(1)/$$d $(2)/; fi; \
	done
	rm -rf $(2)/development/test
endef

%:
	dh $@

override_dh_auto_configure:
	# Create minimal node_modules with workspace-package symlinks so that
	# tsc and rollup can resolve cross-package imports without npm ci.
	mkdir -p node_modules/@lit node_modules/@lit-labs \
	         node_modules/@webcomponents node_modules/@types
	for pkg in reactive-element context; do \
		ln -sfT ../../packages/$$pkg node_modules/@lit/$$pkg; \
	done
	# Copy (not symlink) ssr-dom-shim so we can compile it without modifying
	# the original source tree (tsc outDir "./" writes to the package root).
	cp -r packages/labs/ssr-dom-shim node_modules/@lit-labs/ssr-dom-shim
	python3 debian/patch-tsconfig.py \
	        node_modules/@lit-labs/ssr-dom-shim/tsconfig.json
	for pkg in lit lit-html lit-element; do \
		ln -sfT ../packages/$$pkg node_modules/$$pkg; \
	done
	# Minimal type declarations for optional webcomponents polyfill globals.
	cp -r debian/webcomponentsjs-stub node_modules/@webcomponents/webcomponentsjs
	ln -sfT /usr/share/nodejs/@types/mocha node_modules/@types/mocha
	ln -sfT /usr/share/nodejs/@types/node  node_modules/@types/node
	# Rollup plugins (ESM resolution requires these in node_modules)
	mkdir -p node_modules/@rollup
	for plugin in replace inject node-resolve; do \
		ln -sfT /usr/share/nodejs/@rollup/plugin-$$plugin \
		        node_modules/@rollup/plugin-$$plugin; \
	done
	for plugin in sourcemaps terser; do \
		ln -sfT /usr/share/nodejs/rollup-plugin-$$plugin \
		        node_modules/rollup-plugin-$$plugin; \
	done
	# trusted-types stub (not packaged in Debian; TS DOM lib has the globals,
	# but lit-html imports them as named exports from 'trusted-types/lib/index.js')
	mkdir -p node_modules/trusted-types/lib
	cp debian/trusted-types-stub/package.json node_modules/trusted-types/
	cp debian/trusted-types-stub/lib/index.d.ts node_modules/trusted-types/lib/
	# Patch tsconfigs to exclude test files (avoids @lit-labs/testing dep)
	for pkg in $(CORE_PACKAGES); do \
		python3 debian/patch-tsconfig.py $$pkg/tsconfig.json; \
	done
	# Replace rollup-common.js with the Debian-friendly simplified version
	cp rollup-common.js rollup-common.js.debian-orig
	cp debian/rollup-debian.js rollup-common.js

override_dh_auto_build:
	# Build @lit-labs/ssr-dom-shim (compiled from the node_modules copy so
	# tsc's "outDir: ./" output stays out of the original source tree).
	# The '-' prefix ignores the non-zero exit: TS 5.2.2 lacks ariaBrailleLabel
	# (added in TS 5.3) and import-with (TS 5.3+), producing type errors in
	# lib files we don't use at runtime. --noEmitOnError false ensures JS is
	# still emitted; the '-' tells make to continue past the non-zero exit.
	-cd node_modules/@lit-labs/ssr-dom-shim && \
	    $(TSC) --noEmitOnError false --pretty
	test -f node_modules/@lit-labs/ssr-dom-shim/index.js
	for pkg in $(CORE_PACKAGES); do \
		(cd $$pkg && $(TSC) --build --pretty && $(ROLLUP) -c); \
	done

override_dh_auto_test:
	# Tests skipped; they require a browser environment

override_dh_auto_install:
	# Run pkg-js-tools' installer into a throwaway tree so it can compute
	# ${nodejs:Provides}; the real installed files are copied below.
	dh_auto_install --buildsystem=nodejs_no_lerna --destdir=debian/pkgjs-provides
	rm -rf debian/pkgjs-provides
	# Install all core packages into node-lit
	$(call install-pkg,\
		packages/reactive-element,\
		debian/node-lit/usr/share/nodejs/@lit/reactive-element,\
		decorators development node)
	install -d debian/node-lit/usr/share/nodejs/@lit/reactive-element/node/development
	for f in polyfill-support.js polyfill-support.js.map; do \
		cp packages/reactive-element/$$f \
		   debian/node-lit/usr/share/nodejs/@lit/reactive-element/node/; \
		cp packages/reactive-element/development/$$f \
		   debian/node-lit/usr/share/nodejs/@lit/reactive-element/node/development/; \
	done
	$(call install-pkg,\
		packages/lit-html,\
		debian/node-lit/usr/share/nodejs/lit-html,\
		directives development node)
	$(call install-pkg,\
		packages/lit-element,\
		debian/node-lit/usr/share/nodejs/lit-element,\
		decorators development)
	$(call install-pkg,\
		packages/lit,\
		debian/node-lit/usr/share/nodejs/lit,\
		decorators directives development)
	find packages/lit -maxdepth 1 -name '*.svg' \
		-exec cp {} debian/node-lit/usr/share/nodejs/lit/ \;

	# Install @lit/context
	$(call install-pkg,\
		packages/context,\
		debian/node-lit/usr/share/nodejs/@lit/context,\
		lib development)

	# Runtime dependency of the Node-condition @lit/reactive-element build.
	$(call install-pkg,\
		node_modules/@lit-labs/ssr-dom-shim,\
		debian/node-lit/usr/share/nodejs/@lit-labs/ssr-dom-shim,\
		lib)

override_dh_installexamples:
	dh_installexamples
	find debian/node-lit/usr/share/doc/node-lit/examples \
		\( -name .eslintrc.json -o -name .gitignore \) -delete

override_dh_clean:
	dh_clean
	rm -rf node_modules
	# Restore rollup-common.js
	if [ -f rollup-common.js.debian-orig ]; then \
		mv rollup-common.js.debian-orig rollup-common.js; \
	fi
	for pkg in $(CORE_PACKAGES); do \
		if [ -f $$pkg/tsconfig.json.debian-orig ]; then \
			mv $$pkg/tsconfig.json.debian-orig $$pkg/tsconfig.json; \
		fi; \
		rm -rf $$pkg/development $$pkg/node; \
		find $$pkg -maxdepth 1 -name '*.tsbuildinfo' -delete; \
		find $$pkg -maxdepth 1 \( \
			-name '*.js.map' -o \
			-name '*.d.ts' -o \
			-name '*.d.ts.map' \
		\) -delete; \
		find $$pkg -maxdepth 1 -name '*.js' \
			! -name 'rollup*.js' ! -name 'babel.config.js' \
			-delete; \
	done
	# Remove named output subdirectories created by rollup preserveModules
	rm -rf packages/reactive-element/decorators \
	       packages/lit-html/directives \
	       packages/lit-element/decorators \
	       packages/lit/decorators packages/lit/directives \
	       packages/context/lib
