// SPDX-FileCopyrightText: 2024 David Runge <dave@sleepmap.de>
// SPDX-License-Identifier: MIT OR Apache-2.0

use anyhow::Result;
use clap::Parser;

use crate::versioned_output::{OutputBuilder, OutputFormat, OutputVersion};

#[derive(Parser, Debug)]
pub struct ListCommand {
    #[arg(
        name = "idents_only",
        short = 'i',
        long = "idents-only",
        help = "Only show card idents"
    )]
    pub idents_only: bool,
}

pub fn print_card_list(
    format: OutputFormat,
    output_version: OutputVersion,
    command: Option<ListCommand>,
) -> Result<()> {
    let mut output = crate::output::List::default();
    if let Some(command) = command {
        output.idents_only(command.idents_only);
    }

    for mut open in crate::util::cards()? {
        output.push(open.transaction()?.application_identifier()?.ident());
    }

    print!("{}", output.print(format, output_version)?);
    Ok(())
}
