using PanGu;
using System.Text.RegularExpressions;

namespace Hotline.Share.Tools;
public static class StringExtensions
{
    public static bool IsNullOrEmpty(this string? str)
    {
        return string.IsNullOrEmpty(str);
    }

    public static bool NotNullOrEmpty(this string? str)
    {
        return !string.IsNullOrEmpty(str);
    }

    /// <summary>
    /// 将字符串转换为指定的枚举类型。
    /// </summary>
    /// <typeparam name="TEnum">枚举类型</typeparam>
    /// <param name="value">要转换的字符串</param>
    /// <returns>枚举值</returns>
    public static TEnum ToEnum<TEnum>(this string value) where TEnum : struct
    {
        if (!typeof(TEnum).IsEnum)
        {
            throw new ArgumentException("TEnum must be an enumerated type");
        }

        // 尝试解析字符串为枚举
        return (TEnum)Enum.Parse(typeof(TEnum), value);
    }

    /// <summary>
    /// 是否手机号码
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static bool IsPhoneNumber(this string value)
    {
        if (string.IsNullOrWhiteSpace(value))
            return false;

        var phoneNumberPattern = @"^1[3-9]\d{9}$";
        return Regex.IsMatch(value, phoneNumberPattern);
    }

    /// <summary>
    /// 通过 PanGu 进行分词
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static IList<string> GetSegment(this string value)
    {
        return new Segment()
            .DoSegment(value)
            .Where(word => word.WordType == WordType.SimplifiedChinese && word.Word.Length > 1)
            .Select(word => word.Word)
            .ToList();
    }

        public static IList<string> SplitKeywords(this string value)
    {
        var regex = new Regex(@"[ ,，]+");

        return regex.Split(value)
                    .Where(s => !string.IsNullOrWhiteSpace(s))
                    .Select(s => s.Trim())
                    .ToList();
    }
}
