//! A `#[doc(hidden)]` module used by the `afia-component-macro` crate.

/// Concatenate byte slices into a byte array.
///
/// ```
/// # use afia_component::_macro::concat_byte_slices;
///
/// #[cfg_attr(target_arch = "wasm32", link_section = "__afia__$output_descriptor")]
/// pub static _OUTPUT_DESCRIPTORS: [u8; 68] = concat_byte_slices(&[
///     &[0],
///     &[222,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
///     &[12], b"My Component",
///     &[36, 0],
///     b"This is the component's description."
/// ]);
/// ```
///
/// TODO: We can delete this when [`std::concat_bytes`] becomes stable.
pub const fn concat_byte_slices<const N: usize>(slices: &[&[u8]]) -> [u8; N] {
    let mut concatenated = [0; N];
    let mut cur_slice_idx = 0;
    let mut concat_idx = 0;

    // `for` loops cannot be used in `const` functions, so we use a `while` loop.
    while cur_slice_idx < slices.len() {
        let mut idx_within_slice = 0;

        let slice = slices[cur_slice_idx];

        while idx_within_slice < slice.len() {
            concatenated[concat_idx] = slice[idx_within_slice];
            idx_within_slice += 1;
            concat_idx += 1;
        }

        cur_slice_idx += 1;
    }

    assert!(concat_idx == N);

    concatenated
}
