# PYTHON_ARGCOMPLETE_OK

# Copyright (c) 2025 Marcin Zdun
# This code is licensed under MIT license (see LICENSE for details)

"""
The **cxx_flow.flow.cli** provides command-line entry for the *C++ flow*.
"""

import argparse
import os
import sys
from typing import List, cast

from cxx_flow import __version__
from cxx_flow.api import env
from cxx_flow.flow import steps
from cxx_flow.flow.cli import cmds, finder


def _change_dir():
    root = argparse.ArgumentParser(
        prog="cxx-flow",
        usage="cxx-flow [-h] [--version] [-C [dir]] {command} ...",
        add_help=False,
    )
    root.add_argument("-C", dest="cd", nargs="?")

    args, _ = root.parse_known_args()
    if args.cd:
        os.chdir(args.cd)


def _expand_shortcuts(parser: argparse.ArgumentParser, args: argparse.Namespace):
    args_kwargs = dict(args._get_kwargs())
    for key in parser.shortcuts:
        try:
            if not args_kwargs[key]:
                continue
            cast(List[List[str]], args.configs).append(parser.shortcuts[key])
            break
        except KeyError:
            continue


def __main():
    _change_dir()

    flow_cfg = env.FlowConfig(root=finder.autocomplete.find_project())
    steps.clean_aliases(flow_cfg)

    parser = cmds.build_argparser(flow_cfg)
    finder.autocomplete(parser)
    args = parser.parse_args()
    _expand_shortcuts(parser, args)

    sys.exit(cmds.BuiltinEntry.run_entry(args, flow_cfg))


def main():
    """Entry point for *C++ flow* tool."""
    try:
        __main()
    except KeyboardInterrupt:
        sys.exit(1)
