// Copyright (C) 2015-2026 The Neo Project.
//
// Node.Branch.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.IO;

namespace Neo.Cryptography.MPTTrie;

partial class Node
{
    public const int BranchChildCount = 17;
    public Node[] Children { get; internal set; } = [];

    public static Node NewBranch()
    {
        var n = new Node
        {
            Type = NodeType.BranchNode,
            Reference = 1,
            Children = new Node[BranchChildCount],
        };
        for (var i = 0; i < BranchChildCount; i++)
        {
            n.Children[i] = new Node();
        }
        return n;
    }

    protected int BranchSize
    {
        get
        {
            var size = 0;
            for (var i = 0; i < BranchChildCount; i++)
            {
                size += Children[i].SizeAsChild;
            }
            return size;
        }
    }

    private void SerializeBranch(BinaryWriter writer)
    {
        for (var i = 0; i < BranchChildCount; i++)
        {
            Children[i].SerializeAsChild(writer);
        }
    }

    private void DeserializeBranch(ref MemoryReader reader)
    {
        Children = new Node[BranchChildCount];
        for (var i = 0; i < BranchChildCount; i++)
        {
            var n = new Node();
            n.Deserialize(ref reader);
            Children[i] = n;
        }
    }
}
