"""Copy secret to clipboard."""

import mm_pymac
import typer

from mb_stash.cli.context import password_prompt, use_context


def get(
    ctx: typer.Context,
    key: str,
    *,
    stdout: bool = typer.Option(default=False, help="Print to stdout instead of copying to clipboard"),
) -> None:
    """Copy secret to clipboard (or --stdout for stdout)."""
    app = use_context(ctx)
    value = app.core.service.get(key, password_prompt=password_prompt)
    if stdout:
        app.out.print_secret_stdout(key, value)
    else:
        mm_pymac.set_clipboard(value)
        timeout = app.core.service.schedule_clipboard_clear(value)
        app.out.print_secret_copied(key, timeout=timeout)
