/**
 * Encoding Helpers
 * @countermeasure/web-components
 *
 * Shared encode/decode helpers for symmetric and hybrid cipher classes.
 * Converts between Uint8Array and string using the configured encoding format.
 */

import { toBase64, fromBase64, toHex, fromHex, bytesToString, stringToBytes } from './encoding'
import type { EncodingFormat } from './types'

export type { EncodingFormat }

/**
 * Encode a Uint8Array to a string using the specified format.
 */
export function encodeOutput(data: Uint8Array, encoding: EncodingFormat): string {
  switch (encoding) {
    case 'base64':
      return toBase64(data)
    case 'hex':
      return toHex(data)
    case 'raw':
      return bytesToString(data)
    default:
      return toBase64(data)
  }
}

/**
 * Decode a string to a Uint8Array using the specified format.
 */
export function decodeInput(data: string, encoding: EncodingFormat): Uint8Array {
  switch (encoding) {
    case 'base64':
      return fromBase64(data)
    case 'hex':
      return fromHex(data)
    case 'raw':
      return stringToBytes(data)
    default:
      return fromBase64(data)
  }
}
