namespace Prexonite.Compiler.Cil;

/// <summary>
///     A cil hint. Can be serialized to a meta entry for storage.
/// </summary>
[SuppressMessage(
    "Microsoft.Naming",
    "CA1704:IdentifiersShouldBeSpelledCorrectly",
    MessageId = nameof(Cil)
)]
public interface ICilHint
{
    /// <summary>
    ///     The key under which this CIL hint is stored. This key is used to deserialize the hint into the correct format.
    /// </summary>
    [SuppressMessage(
        "Microsoft.Naming",
        "CA1704:IdentifiersShouldBeSpelledCorrectly",
        MessageId = nameof(Cil)
    )]
    string CilKey { get; }

    /// <summary>
    ///     Get the list of fields to be serialized. Does not include the key.
    /// </summary>
    /// <returns>The list of fields to be serialized.</returns>
    MetaEntry[] GetFields();
}

[SuppressMessage(
    "Microsoft.Naming",
    "CA1704:IdentifiersShouldBeSpelledCorrectly",
    MessageId = nameof(Cil)
)]
public static class CilHintExtensions
{
    extension(ICilHint hint)
    {
        /// <summary>
        ///     Converts the supplied CIL hint to a meta entry (including the CIL hint key).
        /// </summary>
        /// <returns>The serialized cil hint.</returns>
        public MetaEntry ToMetaEntry()
        {
            var key = hint.CilKey;
            var fields = hint.GetFields();

            var entry = new MetaEntry[fields.Length + 1];
            entry[0] = key;
            Array.Copy(fields, 0, entry, 1, fields.Length);

            return (MetaEntry)entry;
        }
    }
}
