/**
 * Description: This is a SoundFont2 parser.
 *
 * @package     midi-Synth
 * @category    module js
 * @author      Mrtenz <https://github.com/Mrtenz/soundfont2>
 * @copyright   soundfont2
 * @license     GPL-2.0-or-later
 */

/**
 * Get all preset headers from a `phdr` sub-chunk.
 *
 * @param {SF2Chunk} chunk - The input chunk
 */
const getPresetHeaders = chunk => {
  if (chunk.id !== "phdr") {
    throw new ParseError("Invalid chunk ID", `'phdr'`, `'${chunk.id}'`)
  }

  if (chunk.length % SF_PRESET_HEADER_SIZE) {
    throw new ParseError(`Invalid size for the 'phdr' sub-chunk`)
  }

  return chunk.iterate(iterator => {
    return {
      name: iterator.getString(),
      preset: iterator.getInt16(),
      bank: iterator.getInt16(),
      bagIndex: iterator.getInt16(),
      library: iterator.getUInt32(),
      genre: iterator.getUInt32(),
      morphology: iterator.getUInt32()
    }
  })
}
