# lt completion for Fish shell
# Auto-generated by lt CLI - supports arbitrary nesting depth

<%
// Recursively collect all nodes for completion generation
function collectAllNodes(nodes, result = [], parentPath = []) {
  for (const node of nodes) {
    result.push({ node, parentPath: [...parentPath] });
    if (node.children.length > 0) {
      collectAllNodes(node.children, result, [...parentPath, node.name]);
    }
  }
  return result;
}

// Escape single quotes in description for Fish
function escapeDesc(desc) {
  return desc.replace(/'/g, "\\'");
}

const allNodes = collectAllNodes(props.commandTree);
const rootCommands = props.commandTree.map(n => n.name).join(' ');
-%>
# Helper function to check command depth
function __lt_using_command
    set -l cmd (commandline -opc)
    set -l cmd_count (count $cmd)

    if test $cmd_count -eq 1
        return 0
    end

    for arg in $argv
        if contains -- $arg $cmd
            return 0
        end
    end
    return 1
end

# Helper to check exact command path
function __lt_at_path
    set -l cmd (commandline -opc)
    set -l expected_len (math (count $argv) + 1)

    if test (count $cmd) -ne $expected_len
        return 1
    end

    set -l i 2
    for expected in $argv
        if test "$cmd[$i]" != "$expected"
            return 1
        end
        set i (math $i + 1)
    end
    return 0
end

# Root level commands
set -l root_cmds <%- rootCommands %>

<% for (const { node, parentPath } of allNodes) { -%>
<% if (parentPath.length === 0) { -%>
# <%- node.name %> command
complete -f -c lt -n "not __fish_seen_subcommand_from $root_cmds" -a "<%- node.name %>" -d '<%- escapeDesc(node.description) %>'
<% } else { -%>
# <%- [...parentPath, node.name].join(' ') %> command
complete -f -c lt -n "__lt_at_path <%- parentPath.join(' ') %>" -a "<%- node.name %>" -d '<%- escapeDesc(node.description) %>'
<% } -%>

<% } -%>
# Common flags (available at any depth)
complete -f -c lt -l help -d 'Show help'
complete -f -c lt -l version -d 'Show version'
complete -f -c lt -l noConfirm -d 'Skip confirmations'
complete -f -c lt -l dry-run -d 'Show what would be done'
