using System;
using HellionChat.Ui;

namespace HellionChat._Helpers;

// Extracted submit logic from ChatInputBar.SubmitCompact to allow unit testing
// without a sealed ChatLogWindow dependency.
// TEST-MIRROR: ../../../Hellion Build test/Ui/CompactInputSubmitterTests.cs
public static class CompactInputSubmitter
{
    public static bool TrySubmit(InputState state, Tab tab, Action<Tab, string> sender)
    {
        ArgumentNullException.ThrowIfNull(state);
        ArgumentNullException.ThrowIfNull(tab);
        ArgumentNullException.ThrowIfNull(sender);

        if (string.IsNullOrWhiteSpace(state.Buffer))
            return false;

        var text = state.Buffer;
        state.Buffer = string.Empty;
        state.HistoryCursor = -1;
        sender(tab, text);
        return true;
    }
}
