{ lib, ... }:
with lib;
let
  inherit (lib.nixvim) defaultNullOpts mkNullOrOption mkNullOrStr;
  mkExtension = import ./_mk-extension.nix;
in
mkExtension {
  name = "frecency";
  package = "telescope-frecency-nvim";

  settingsOptions = {
    auto_validate = defaultNullOpts.mkBool true ''
      If true, it removes stale entries count over than `db_validate_threshold`.
    '';

    db_root = defaultNullOpts.mkStr (lib.nixvim.literalLua "vim.fn.stdpath 'data'") ''
      Path to parent directory of custom database location.
      Defaults to `$XDG_DATA_HOME/nvim` if unset.
    '';

    db_safe_mode = defaultNullOpts.mkBool true ''
      If true, it shows confirmation dialog by `vim.ui.select()` before validating DB.
    '';

    db_validate_threshold = defaultNullOpts.mkUnsignedInt 10 ''
      It will removes over than this count in validating DB.
    '';

    default_workspace = mkNullOrStr ''
      Default workspace tag to filter by e.g. `'CWD'` to filter by default to the current
      directory.
      Can be overridden at query time by specifying another filter like `':*:'`.
    '';

    disable_devicons = defaultNullOpts.mkBool false ''
      Disable devicons (if available).
    '';

    hide_current_buffer = defaultNullOpts.mkBool false ''
      If true, it does not show the current buffer in candidates.
    '';

    filter_delimiter = defaultNullOpts.mkStr ":" ''
      Delimiters to indicate the filter like `:CWD:`.
    '';

    ignore_patterns =
      defaultNullOpts.mkListOf types.str
        [
          "*.git/*"
          "*/tmp/*"
          "term://*"
        ]
        ''
          Patterns in this table control which files are indexed (and subsequently which you'll see
          in the finder results).
        '';

    max_timestamps = defaultNullOpts.mkPositiveInt 10 ''
      Set the max count of timestamps DB keeps when you open files.
      It ignores the value and use `10` if you set less than or equal to `0`.

      CAUTION: When you reduce the value of this option, it removes old timestamps when you open
      the file.
      It is reasonable to set this value more than or equal to the default value: `10`.
    '';

    show_filter_column = defaultNullOpts.mkNullable (with types; either bool (listOf str)) true ''
      Show the path of the active filter before file paths.
      In default, it uses the tail of paths for `'LSP'` and `'CWD'` tags.
      You can configure this by setting a table for this option.
    '';

    show_scores = defaultNullOpts.mkBool false ''
      To see the scores generated by the algorithm in the results, set this to true.
    '';

    show_unindexed = defaultNullOpts.mkBool true ''
      Determines if non-indexed files are included in workspace filter results.
    '';

    workspace_scan_cmd = mkNullOrOption (with types; either rawLua (listOf str)) ''
      This option can be set values as `"LUA"|string[]|null`.
      With the default value: `null`, it uses these way below to make entries for workspace
      files.
      It tries in order until it works successfully.

      1. `rg -.g '!.git' --files`
      2. `fdfind -Htf`
      3. `fd -Htf`
      4. Native Lua code (old way)

      If you like another commands, set them to this option, like
      ```nix
        workspace_scan_cmd = ["find" "." "-type" "f"];
      ```

      If you prefer Native Lua code, set `workspace_scan_cmd.__raw = "LUA"`.
    '';

    workspaces = defaultNullOpts.mkAttrsOf types.str { } ''
      This attrs contains mappings of `workspace_tag` -> `workspace_directory`.
      The key corresponds to the `:tag_name` used to select the filter in queries.
      The value corresponds to the top level directory by which results will be filtered.
    '';
  };

  settingsExample = {
    db_root = "/home/my_username/path/to/db_root";
    show_scores = false;
    show_unindexed = true;
    ignore_patterns = [
      "*.git/*"
      "*/tmp/*"
    ];
    disable_devicons = false;
    workspaces = {
      conf = "/home/my_username/.config";
      data = "/home/my_username/.local/share";
      project = "/home/my_username/projects";
      wiki = "/home/my_username/wiki";
    };
  };
}
