package auxilary

import (
	"bytes"
	"encoding/binary"
)

// int32ToBigEndian converts an int32 to a big-endian byte array.
func int32ToBigEndian(num int32) ([]byte, error) {
	buf := new(bytes.Buffer)
	err := binary.Write(buf, binary.BigEndian, num)
	if err != nil {
		return nil, err
	}
	return buf.Bytes(), nil
}

// bigEndianToInt32 converts a big-endian byte array to an int32.
func bigEndianToInt32(data []byte) int32 {
	var num int32
	buf := bytes.NewReader(data)
	err := binary.Read(buf, binary.BigEndian, &num)
	if err != nil {
		return 0
	}
	return num
}

// bigEndianToInt24 converts a big-endian 3-byte array to an int32 (simulating int24).
func bigEndianToInt24(data []byte) int32 {
	if len(data) != 3 {
		return 0
	}
	num := int32(data[0])<<16 | int32(data[1])<<8 | int32(data[2])
	// Adjust for negative values
	if num&0x800000 != 0 {
		num |= ^0xFFFFFF
	}
	return num
}

// bigEndianToInt16 converts a big-endian byte array to an int16.
func bigEndianToInt16(data []byte) int16 {
	var num int16
	buf := bytes.NewReader(data)
	err := binary.Read(buf, binary.BigEndian, &num)
	if err != nil {
		return 0
	}
	return num
}
