use std::collections::HashMap;

use crate::error::ParseResult;
use crate::functions::{define_function_full, ArgType, FunctionContext, FunctionSpec};
use crate::parse_node::ParseNode;

pub fn register(map: &mut HashMap<&'static str, FunctionSpec>) {
    define_function_full(
        map,
        &["\\fbox"],
        "enclose",
        1,
        0,
        Some(vec![ArgType::HBox]),
        false,
        true,
        true,
        false,
        false,
        handle_fbox,
    );

    define_function_full(
        map,
        &["\\cancel", "\\bcancel", "\\xcancel", "\\phase"],
        "enclose",
        1,
        0,
        None,
        false,
        false,
        true,
        false,
        false,
        handle_cancel,
    );

    // \sout originates as a LaTeX text-mode command but KaTeX accepts it in
    // both modes (allowedInText: true, no argType override → arg inherits
    // surrounding mode). We mirror that so `\text{\sout{abc}}` parses while
    // `\sout{x^2}` still keeps its argument in math mode.
    define_function_full(
        map,
        &["\\sout"],
        "enclose",
        1,
        0,
        None,
        false,
        true,
        true,
        false,
        false,
        handle_cancel,
    );

    define_function_full(
        map,
        &["\\angl"],
        "enclose",
        1,
        0,
        Some(vec![ArgType::HBox]),
        false,
        false,
        true,
        false,
        false,
        handle_angl,
    );
}

fn handle_fbox(
    ctx: &mut FunctionContext,
    args: Vec<ParseNode>,
    _opt_args: Vec<Option<ParseNode>>,
) -> ParseResult<ParseNode> {
    Ok(ParseNode::Enclose {
        mode: ctx.parser.mode,
        label: "\\fbox".to_string(),
        background_color: None,
        border_color: None,
        body: Box::new(args.into_iter().next().unwrap()),
        loc: None,
    })
}

fn handle_cancel(
    ctx: &mut FunctionContext,
    args: Vec<ParseNode>,
    _opt_args: Vec<Option<ParseNode>>,
) -> ParseResult<ParseNode> {
    Ok(ParseNode::Enclose {
        mode: ctx.parser.mode,
        label: ctx.func_name.clone(),
        background_color: None,
        border_color: None,
        body: Box::new(args.into_iter().next().unwrap()),
        loc: None,
    })
}

fn handle_angl(
    ctx: &mut FunctionContext,
    args: Vec<ParseNode>,
    _opt_args: Vec<Option<ParseNode>>,
) -> ParseResult<ParseNode> {
    Ok(ParseNode::Enclose {
        mode: ctx.parser.mode,
        label: "\\angl".to_string(),
        background_color: None,
        border_color: None,
        body: Box::new(args.into_iter().next().unwrap()),
        loc: None,
    })
}
