using System.Text;
using MMoneyCoderSharp.Data;

namespace MBN_STOCK_WEBVIEW.Core.Tests;

public sealed class HangulSearchMatcherTests
{
    [Theory]
    [InlineData("삼성전자", "ㅅㅅㅈㅈ")]
    [InlineData("홍길동 위원", "ㅎㄱㄷ ㅇㅇ")]
    [InlineData("까치산", "ㄲㅊㅅ")]
    [InlineData("삼성전자(NXT)", "ㅅㅅㅈㅈ")]
    [InlineData("삼성전자", "ᄉᄉᄌᄌ")]
    public void IsMatch_MatchesCompatibilityOrLeadingInitialConsonants(
        string candidate,
        string query)
    {
        Assert.True(HangulSearchMatcher.IsMatch(candidate, query));
        Assert.True(HangulSearchMatcher.IsInitialConsonantQuery(query));
    }

    [Fact]
    public void IsMatch_NormalizesDecomposedHangulAndKeepsOrdinarySubstringSearch()
    {
        var decomposed = "삼성전자".Normalize(NormalizationForm.FormD);

        Assert.True(HangulSearchMatcher.IsMatch(decomposed, "ㅅㅅㅈㅈ"));
        Assert.True(HangulSearchMatcher.IsMatch("김전문 연구원", "전문"));
        Assert.True(HangulSearchMatcher.IsMatch("Alpha Analyst", "analyst"));
    }

    [Theory]
    [InlineData("삼성전자", "ㅅ성")]
    [InlineData("삼성전자", "ㅏ")]
    public void IsMatch_DoesNotTreatMixedOrNonInitialJamoAsFuzzyInput(
        string candidate,
        string query)
    {
        Assert.False(HangulSearchMatcher.IsMatch(candidate, query));
        Assert.False(HangulSearchMatcher.IsInitialConsonantQuery(query));
    }

    [Fact]
    public void IsMatch_RecognizesAValidDoubleInitialWithoutCreatingAFalseMatch()
    {
        Assert.True(HangulSearchMatcher.IsInitialConsonantQuery("ㄲㅅ"));
        Assert.False(HangulSearchMatcher.IsMatch("삼성전자", "ㄲㅅ"));
    }

    [Fact]
    public void IsMatch_BlankQueryRetainsExistingInitialListBehavior()
    {
        Assert.True(HangulSearchMatcher.IsMatch("아무 이름", "   "));
        Assert.False(HangulSearchMatcher.IsInitialConsonantQuery("   "));
    }
}
