/*
SPDX-FileCopyrightText: 2025–2026 toastal
SPDX-FileCopyrightText: 2026 Nixtamal contributors
SPDX-License-Identifier: ISC

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice & this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED “AS IS” & ISC DISCLAIMS ALL WARRANTIES WITH REGARD
TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY &
FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
OF THIS SOFTWARE.

──────────────────────────────────────────────────────────────────────────────
┏┓╻+╻ ╱┏┳┓┏┓┏┳┓┏┓╻
┃┃┃┃┗━┓╹┃╹┣┫┃┃┃┣┫┃   This file was generated by Nixtamal.
╹┗┛╹╱ ╹ ╹ ╹╹╹ ╹╹╹┗┛  Do not edit as it will be overwritten.
──────────────────────────────────────────────────────────────────────────────
*/
{
	system ? builtins.currentSystem,
	bootstrap-nixpkgs ? null,
	bootstrap-nixpkgs-lock-name ? null,
}:

let lock = builtins.fromJSON (builtins.readFile ./lock.json); in
assert (lock.v == "1.2.0");
let
	local-patches = {
		"adios" = ./patches/adios.patch;
		"kservice_fix" = ./patches/kservice_fix.patch;
	};

	hash-token = {
		"0" = "sha256";
		"1" = "sha512";
		"2" = "blake3";
	};

	try-fetch = input-name: fetcher:
		let
			try-fetch' = failed-urls: url: urls:
				let result = builtins.tryEval (fetcher url); in
				if result.success then
					result.value
				else
					let failed-urls' = [ url ] ++ failed-urls; in
					if builtins.length urls <= 0 then
						let fus = builtins.concatStringsSep " " failed-urls'; in
						throw "Input 「${input-name}」fetchable @ [ ${fus} ]"
					else
						try-fetch' failed-urls' (builtins.head urls) (builtins.tail urls);
		in
		try-fetch' [ ];

	builtin-fetch-url = {input-name, name, kind, hash}:
		try-fetch input-name (url:
			builtins.fetchurl ({
				inherit url;
				${hash-token.${builtins.toString hash.al}} = hash.vl;
			}
			// (if name != null then {inherit name;} else {}))
		) kind.ur kind.ms;

	builtin-fetch-tarball = {input-name, name, kind, hash}:
		try-fetch input-name (url:
			builtins.fetchTarball ({
				inherit url;
				${hash-token.${builtins.toString hash.al}} = hash.vl;
			}
			// (if name != null then {inherit name;} else {}))
		) kind.ur kind.ms;

	builtin-to-input = input-name: input:
		let
			name = input.sn;
			hash = input.ha;
			k = builtins.head input.kd;
		in
		if k == 0 then
			builtin-fetch-url {
				inherit name;
				input-name = input-name;
				kind = builtins.elemAt input.kd 1;
				hash = input.ha;
			}
		else if k == 1 then
			builtin-fetch-tarball {
				inherit name;
				input-name = input-name;
				kind = builtins.elemAt input.kd 1;
				hash = input.ha;
			}
		else
			throw "Unsupported input kind “${builtins.toString k}”.";

	nixpkgs' =
		if builtins.isNull bootstrap-nixpkgs then
			builtin-to-input "nixpkgs-for-nixtamal" (
				if builtins.isString bootstrap-nixpkgs-lock-name then
					lock.i.${bootstrap-nixpkgs-lock-name}
				else
					lock.i.nixpkgs-nixtamal or lock.i.nixpkgs
			)
		else
			bootstrap-nixpkgs;

	pkgs = import nixpkgs' {inherit system;};

	inherit (pkgs) lib;

	fetch-url = {input-name, name, kind, hash}: pkgs.fetchurl ({
		url = kind.ur;
		hash = hash.vl;
	}
	// lib.optionalAttrs (name != null) {inherit name;}
	// lib.optionalAttrs (builtins.length kind.ms > 0) {urls = kind.ms;});

	fetch-zip = {input-name, name, kind, hash}: pkgs.fetchzip ({
		url = kind.ur;
		hash = hash.vl;
	}
	// lib.optionalAttrs (name != null) {inherit name;}
	// lib.optionalAttrs (builtins.length kind.ms > 0) {urls = kind.ms;});

	fetch-patch = patch-name: {ur, ha}:
		pkgs.fetchpatch2 {
			url = ur;
			hash = ha.vl;
		};

	to-input = input-name: input:
		let
			name = input.sn;
			hash = input.ha;
			k = builtins.head input.kd;
			raw-input =
				if k == 0 then
					let
						kind = builtins.elemAt input.kd 1;
						fetch_time = kind.ft;
					in
					if fetch_time == 0 then
						fetch-url {inherit input-name name kind hash;}
					else if fetch_time == 1 then
						builtin-fetch-url {inherit input-name name kind hash;}
					else
						throw "Unsupported fetch time ${fetch_time}."
				else if k == 1 then
					let
						kind = builtins.elemAt input.kd 1;
						fetch_time = kind.ft;
					in
					if fetch_time == 0 then
						fetch-zip {inherit input-name name kind hash;}
					else if fetch_time == 1 then
						builtin-fetch-tarball {inherit input-name name kind hash;}
					else
						throw "Unsupported fetch time ${fetch_time}."
				else
					throw "Unsupported input kind “${builtins.toString k}”.";
		in
		if builtins.length input.ps == 0 then
			raw-input
		else
			pkgs.applyPatches {
				src = raw-input;
				name = "${if name != null then name else "src"}-patched";
				patches = map (p:
					if local-patches ? "${p}" then
						local-patches."${p}"
					else
						fetch-patch p lock.p."${p}"
				) input.ps;
			};
in
builtins.mapAttrs to-input lock.i
