# As our modules have nonstandard properties, we need to have some way of
# properly intepreting them
# This function takes a list of modules, as well as arguments to import them
# with, and returns a list of modules, each with the standard NixOS module
# properties as well as with custom properties as described in /README.md
lib: transformArgs: modules: args @ { pkgs, ... }:
let
  resolver = module:
    let
      importedModule =
        if builtins.typeOf module == "path"
        then import module
        else module;
      resolvedModule =
        if builtins.typeOf importedModule == "lambda"
        then
          importedModule
            (transformArgs args)
        else importedModule;
      moduleName =
        if builtins.typeOf module == "lambda"
        then "<AnonFunction>"
        else if builtins.typeOf module == "path"
        then builtins.toString module
        else builtins.toJSON module;
    in
    lib.warnIfNot
      ((lib.pipe resolvedModule [
        builtins.attrNames
        (lib.subtractLists [ "home" "config" "imports" "options" "traces" ])
      ])
      == [ ])
      "Module ${moduleName} had attribute names ${builtins.toJSON (builtins.attrNames resolvedModule)} but only home, config, imports and options are resolved"
      [
        {
          config = lib.recursiveUpdate (resolvedModule.config or { }) {
            home-manager.users."${args.username}".imports =
              (resolvedModule.config.home-manager.users."${args.username}".imports or [ ])
              ++ [ resolvedModule.home or { } ];
          };
          imports = resolvedModule.imports or [ ];
          options = resolvedModule.options or { };
        }
      ]
    ++ (builtins.map
      (trace:
        let
          splitTrace = lib.splitString "." trace;
          traceHead = builtins.head splitTrace;
          traceTail = builtins.tail splitTrace;
          resolvedTrace =
            (
              if traceHead == "home"
              then [ "home-manager" "users" args.username ]
              else lib.errorIfNot (traceHead == "config") [ ]
            )
            ++ traceTail;
        in
        { config, ... }: (builtins.seq
          (
            lib.pipe resolvedTrace [
              (lib.foldl
                ({ value
                 , error
                 ,
                 }: key:
                  if builtins.hasAttr key value
                  then {
                    value = value.${key};
                    inherit error;
                  }
                  else {
                    value = { };
                    error = true;
                  })
                {
                  value = { };
                  error = false;
                })
              (data: lib.warnIf data.error "trace@${moduleName}/${trace} is invalid; the key does not exist" data)
              ({ value
               , error
               ,
               }: {
                value = builtins.toJSON value;
                inherit error;
              })
              ({ value
               , error
               ,
               }: {
                value = "trace@${moduleName}/${trace}: ${value}";
                inherit error;
              })
              ({ value
               , error
               ,
               }:
                lib.traceIf (!error) value null)
            ]
          )
          { }))
      (resolvedModule.traces or [ ]));
in
{
  imports = (
    if builtins.typeOf modules == "list"
    then builtins.concatLists (builtins.map resolver modules)
    else resolver modules
  );
}
