//! Command registry for the command palette.
//!
//! Provides a centralized registry of all available commands, organized by category
//! in the `registry_commands` module.

use crate::palette::command::CommandDef;
use crate::app::AppState;
use once_cell::sync::Lazy;

mod registry_commands;

static REGISTRY: Lazy<CommandRegistry> = Lazy::new(|| {
    let commands = all_commands();
    CommandRegistry { commands }
});

pub struct CommandRegistry {
    commands: &'static [CommandDef],
}

impl CommandRegistry {
    pub fn instance() -> &'static CommandRegistry {
        &REGISTRY
    }
    
    pub fn enabled_commands(&self, state: &AppState) -> Vec<&'static CommandDef> {
        self.commands.iter().filter(|cmd| cmd.enabled(state)).collect()
    }
    
    /// Find a command by its shortcut key (case-sensitive)
    pub fn by_key(&self, key: &str, state: &AppState) -> Option<&'static CommandDef> {
        self.commands.iter()
            .find(|cmd| cmd.key == key && cmd.enabled(state))
    }
}

fn all_commands() -> &'static [CommandDef] {
    static COMMANDS: Lazy<Vec<CommandDef>> = Lazy::new(|| {
        registry_commands::all_commands()
    });
    
    COMMANDS.as_slice()
}
