using System;

namespace MainShell.Common
{
    /// <summary>
    /// 用于流程内部以 MessageKey 形式表达受控失败，并保留格式化参数。
    /// </summary>
    public class LocalizedProcessException : Exception
    {
        public MessageKey FailureMessageKey { get; }

        public string[] FailureMessageArguments { get; }

        public LocalizedProcessException(MessageKey failureMessageKey, params string[] failureMessageArguments)
            : base(LanguageResourceHelper.Format(failureMessageKey, ConvertArguments(failureMessageArguments)))
        {
            FailureMessageKey = failureMessageKey;
            FailureMessageArguments = failureMessageArguments ?? Array.Empty<string>();
        }

        public static object[] ConvertArguments(string[] failureMessageArguments)
        {
            if (failureMessageArguments == null || failureMessageArguments.Length == 0)
            {
                return Array.Empty<object>();
            }

            object[] convertedArguments = new object[failureMessageArguments.Length];
            for (int i = 0; i < failureMessageArguments.Length; i++)
            {
                convertedArguments[i] = failureMessageArguments[i];
            }

            return convertedArguments;
        }
    }
}