use super::{EngineState, Stack, StateWorkingSet};
use crate::{ast::Call, Alias, BlockId, Example, OutDest, PipelineData, ShellError, Signature};
use std::fmt::Display;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CommandType {
    Builtin,
    Custom,
    Keyword,
    External,
    Alias,
    Plugin,
}
impl Display for CommandType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let str = match self {
            CommandType::Builtin => "built-in",
            CommandType::Custom => "custom",
            CommandType::Keyword => "keyword",
            CommandType::External => "external",
            CommandType::Alias => "alias",
            CommandType::Plugin => "plugin",
        };
        write!(f, "{str}")
    }
}
pub trait Command: Send + Sync + CommandClone {
    fn name(&self) -> &str;
    fn signature(&self) -> Signature;
    fn usage(&self) -> &str;
    fn extra_usage(&self) -> &str {
        ""
    }
    fn run(
        &self,
        engine_state: &EngineState,
        stack: &mut Stack,
        call: &Call,
        input: PipelineData,
    ) -> Result<PipelineData, ShellError>;
    #[allow(unused_variables)]
    fn run_const(
        &self,
        working_set: &StateWorkingSet,
        call: &Call,
        input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        Err(ShellError::MissingConstEvalImpl { span: call.head })
    }
    fn examples(&self) -> Vec<Example> {
        Vec::new()
    }
    fn search_terms(&self) -> Vec<&str> {
        vec![]
    }
    fn is_const(&self) -> bool {
        false
    }
    fn is_sub(&self) -> bool {
        self.name().contains(' ')
    }
    fn block_id(&self) -> Option<BlockId> {
        None
    }
    fn as_alias(&self) -> Option<&Alias> {
        None
    }
    #[cfg(feature = "plugin")]
    fn plugin_identity(&self) -> Option<&crate::PluginIdentity> {
        None
    }
    fn command_type(&self) -> CommandType {
        CommandType::Builtin
    }
    fn is_builtin(&self) -> bool {
        self.command_type() == CommandType::Builtin
    }
    fn is_custom(&self) -> bool {
        self.command_type() == CommandType::Custom
    }
    fn is_keyword(&self) -> bool {
        self.command_type() == CommandType::Keyword
    }
    fn is_known_external(&self) -> bool {
        self.command_type() == CommandType::External
    }
    fn is_alias(&self) -> bool {
        self.command_type() == CommandType::Alias
    }
    fn is_plugin(&self) -> bool {
        self.command_type() == CommandType::Plugin
    }
    fn pipe_redirection(&self) -> (Option<OutDest>, Option<OutDest>) {
        (None, None)
    }
}
pub trait CommandClone {
    fn clone_box(&self) -> Box<dyn Command>;
}
impl<T> CommandClone for T
where
    T: 'static + Command + Clone,
{
    fn clone_box(&self) -> Box<dyn Command> {
        Box::new(self.clone())
    }
}
impl Clone for Box<dyn Command> {
    fn clone(&self) -> Box<dyn Command> {
        self.clone_box()
    }
}