import Pbf from 'pbf'
import getProtoStr from './protos'
import {compile} from 'pbf/compile'
import schema from 'protocol-buffers-schema'

function _getProtoVersion (taskInfo) {
  let version = '1.0'
  if (taskInfo.metadata?.environment) {
    return taskInfo.metadata.environment.protoVersion
  }
  return version
}

export function parseMVS (buffer, taskInfo) {
  if (!taskInfo.task_id || !buffer) {
    return
  }
  let protoVersion = _getProtoVersion(taskInfo)
  let protoStr = getProtoStr(protoVersion)
  if (!protoStr) {
    throw new Error('不支持的格式')
  }
  let proto = schema.parse(protoStr)
  let {MVSBlock} = compile(proto)
  return MVSBlock.read(new Pbf(buffer))
}