#──────────────────────────────────────────────────────────────────────────────┐
# SPDX-FileCopyrightText: 2026 toastal <https://toast.al/contact/>             │
# SPDX-License-Identifier: MPL-2.0                                             │
#──────────────────────────────────────────────────────────────────────────────┘
{
	coreutils,
	curl,
	lib,
	libxslt,
	nickel,
	writers,
	writeText,
   url ? "https://www.six-group.com/dam/download/financial-information/data-center/iso-currrency/lists/list-one.xml",
}:

let
	stylesheet = writeText "iso-4217-currency-codes.xsl" /* xslt */ ''
<?xml version="1.0" encoding="UTF-8"?>
		<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
			<xsl:output method="text" encoding="UTF-8"/>

			<xsl:key name="currency-code" match="/ISO_4217/CcyTbl/CcyNtry/Ccy" use="normalize-space(.)"/>

			<xsl:template match="/">
				<xsl:text>#──────────────────────────────────────────────────────────────────────────────┐&#10;</xsl:text>
				<xsl:text># SPDX-FileCopyrightText: 2026 toastal &lt;https://toast.al/contact/&gt;             │&#10;</xsl:text>
				<xsl:text># SPDX-License-Identifier: MPL-2.0                                             │&#10;</xsl:text>
				<xsl:text>#──────────────────────────────────────────────────────────────────────────────┘&#10;</xsl:text>
				<xsl:text># Generated from ISO 4217 list-one.xml (published </xsl:text>
				<xsl:value-of select="/ISO_4217/@Pblshd"/>
				<xsl:text>). Do not edit by hand.&#10;</xsl:text>
				<xsl:text>[|&#10;</xsl:text>
				<xsl:for-each select="/ISO_4217/CcyTbl/CcyNtry/Ccy[
					generate-id() = generate-id(key('currency-code', normalize-space(.))[1])
				]">
					<xsl:sort select="normalize-space(.)" data-type="text" order="ascending"/>
					<xsl:text>	'</xsl:text>
					<xsl:value-of select="normalize-space(.)"/>
					<xsl:if test="position() != last()">
						<xsl:text>,</xsl:text>
					</xsl:if>
					<xsl:text>&#10;</xsl:text>
				</xsl:for-each>
				<xsl:text>|]&#10;</xsl:text>
			</xsl:template>
		</xsl:stylesheet>
	'';
in
writers.writeDashBin "beget-ISO-4217-currency-codes" {
	makeWrapperArgs = [
		"--prefix" "PATH" ":" (lib.makeBinPath [
			coreutils
			(lib.getBin curl)
			(lib.getBin libxslt)
			(lib.getBin nickel)
		])
	];
}
/* sh */ ''
	set -eu

	usage() {
		echo "Usage: beget-ISO-4217-currency-codes -o/--output FILE"
		echo
		echo "Generate a Nickel enum of unique ISO 4217 currency codes."
	}

	output=
	while [ "$#" -gt 0 ]; do
		case "$1" in
			-o|--output)
				[ "$#" -ge 2 ] || { echo "missing argument for $1" >&2; exit 2; }
				output=$2
				shift 2
				;;
			-h|--help)
				usage
				exit 0
				;;
			--)
				shift
				break
				;;
			*)
				echo "unexpected argument: $1" >&2
				usage >&2
				exit 2
				;;
		esac
	done

	if [ "$#" -gt 0 ]; then
		echo "unexpected argument: $1" >&2
		usage >&2
		exit 2
	fi

	if [ -z "$output" ]; then
		echo "missing required -o/--output" >&2
		usage >&2
		exit 2
	fi

	tmpdir=$(mktemp -d)
	trap 'rm -rf "$tmpdir"' EXIT HUP INT TERM

	curl --fail --silent --show-error --location --output "$tmpdir/list-one.xml" ${lib.escapeShellArg url}
	xsltproc "${stylesheet}" "$tmpdir/list-one.xml" >"$tmpdir/currency-codes.ncl"
	nickel eval "$tmpdir/currency-codes.ncl" >/dev/null
	mkdir -p "$(dirname "$output")"
	mv "$tmpdir/currency-codes.ncl" "$output"
''
