using XF.Domain.Exceptions;

namespace XF.Domain.Extensions
{
    public static class GuidExtensions
    {
        /// <summary>
        /// string类型的Guid校验
        /// </summary>
        /// <param name="id"></param>
        public static void ValidGuidString(this string id)
        {
            if (string.IsNullOrEmpty(id))
                throw new UserFriendlyException("无效的id");
            if (!Guid.TryParse(id, out _))
                throw new UserFriendlyException("无效的id");
        }

        /// <summary>
        /// string类型的Guid校验
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool IsGuidString(this string id)
        {
            if (string.IsNullOrEmpty(id))
                return false;
            return Guid.TryParse(id, out _);
        }

        /// <summary>
        /// id转为字符格式字符串
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string ToDigitsGuidString(this string id)
        {
            id.ValidGuidString();
            return Guid.Parse(id).ToString("N");
        }

        /// <summary>
        /// id转为连字符格式字符串
        /// </summary>
        /// <returns></returns>
        public static string ToHyphensGuidString(this string id)
        {
            return id.Length == 32 ? Guid.Parse(id).ToString("D") : id;
        }
    }
}
