use oxc_allocator::ArenaVec;
use oxc_ast::ast::*;

use crate::{
    Format, FormatTrailingCommas,
    ast_nodes::AstNode,
    formatter::{prelude::*, trivia::format_dangling_comments},
    write,
};

use super::FormatWrite;

impl<'a> Format<'a, JsFormatContext<'a>> for AstNode<'a, ArenaVec<'a, TSTupleElement<'a>>> {
    fn fmt(&self, f: &mut JsFormatter<'_, 'a>) {
        let trailing_separator = FormatTrailingCommas::ES5.trailing_separator(f.options());
        f.join_nodes_with_soft_line().entries_with_trailing_separator(
            self.iter(),
            ",",
            trailing_separator,
        );
    }
}

impl<'a> FormatWrite<'a> for AstNode<'a, TSTupleType<'a>> {
    fn write(&self, f: &mut JsFormatter<'_, 'a>) {
        write!(f, "[");

        let element_types = self.element_types();
        if element_types.is_empty() {
            write!(f, [format_dangling_comments(self.span).with_soft_block_indent()]);
        } else {
            write!(f, [group(&soft_block_indent(&element_types))]);
        }

        write!(f, "]");
    }
}
