{ pkgs, lib, config, ... }:
{
  imports = [
    ./marker.nix
  ];

  options = {
    scripts = {
      output = lib.mkOption {
        type = lib.types.package;
      };

      geocode = lib.mkOption {
        type = lib.types.package;
      };
    };

    map = {
      zoom = lib.mkOption {
        type = lib.types.nullOr lib.types.int;
        default = 2;
      };

      center = lib.mkOption {
        type = lib.types.nullOr lib.types.str;
        default = "Switzerland";
      };
    };

    requestParams = lib.mkOption {
      type = lib.types.listOf lib.types.str;
    };
  };

  config = {
    scripts = {
      output = pkgs.writeShellApplication {
        name = "map";

        runtimeInputs = with pkgs; [
          curl
          imagemagick
        ];

        text = ''
          convert <(${./map} ${lib.concatStringsSep " " config.requestParams}) sixel:-
        '';
      };

      geocode = pkgs.writeShellApplication {
        name = "geocode";

        runtimeInputs = with pkgs; [
          curl
          jq
        ];

        text = ''exec ${./geocode} "$@"'';
      };
    };

    requestParams = [
      "size=640x300"
      "scale=2"
      (lib.mkIf (config.map.zoom != null)
        "zoom=${toString config.map.zoom}")
      (lib.mkIf (config.map.center != null)
        "center=\"${config.scripts.geocode}/bin/geocode ${
          lib.escapeShellArg config.map.center
        }\"")
    ];
  };
}