// automatically generated by the FlatBuffers compiler, do not modify
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any,
 * @typescript-eslint/no-non-null-assertion */
import * as flatbuffers from 'flatbuffers';

import {ABC, unionListToAbc, unionToAbc} from '../union-underlying-type/abc.js';

export class D {
  constructor() {
    this.bb = null;
    this.bb_pos = 0;
  }
  __init(i, bb) {
    this.bb_pos = i;
    this.bb = bb;
    return this;
  }
  static getRootAsD(bb, obj) {
    return (obj || new D())
        .__init(bb.readInt32(bb.position()) + bb.position(), bb);
  }
  static getSizePrefixedRootAsD(bb, obj) {
    bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
    return (obj || new D())
        .__init(bb.readInt32(bb.position()) + bb.position(), bb);
  }
  testUnionType() {
    const offset = this.bb.__offset(this.bb_pos, 4);
    return offset ? this.bb.readInt32(this.bb_pos + offset) : ABC.NONE;
  }
  testUnion(obj) {
    const offset = this.bb.__offset(this.bb_pos, 6);
    return offset ? this.bb.__union(obj, this.bb_pos + offset) : null;
  }
  testVectorOfUnionType(index) {
    const offset = this.bb.__offset(this.bb_pos, 8);
    return offset ?
        this.bb.readInt32(this.bb.__vector(this.bb_pos + offset) + index * 4) :
        null;
  }
  testVectorOfUnionTypeLength() {
    const offset = this.bb.__offset(this.bb_pos, 8);
    return offset ? this.bb.__vector_len(this.bb_pos + offset) : 0;
  }
  testVectorOfUnionTypeArray() {
    const offset = this.bb.__offset(this.bb_pos, 8);
    return offset ?
        new Int32Array(
            this.bb.bytes().buffer,
            this.bb.bytes().byteOffset + this.bb.__vector(this.bb_pos + offset),
            this.bb.__vector_len(this.bb_pos + offset)) :
        null;
  }
  testVectorOfUnion(index, obj) {
    const offset = this.bb.__offset(this.bb_pos, 10);
    return offset ?
        this.bb.__union(
            obj, this.bb.__vector(this.bb_pos + offset) + index * 4) :
        null;
  }
  testVectorOfUnionLength() {
    const offset = this.bb.__offset(this.bb_pos, 10);
    return offset ? this.bb.__vector_len(this.bb_pos + offset) : 0;
  }
  static getFullyQualifiedName() {
    return 'UnionUnderlyingType.D';
  }
  static startD(builder) {
    builder.startObject(4);
  }
  static addTestUnionType(builder, testUnionType) {
    builder.addFieldInt32(0, testUnionType, ABC.NONE);
  }
  static addTestUnion(builder, testUnionOffset) {
    builder.addFieldOffset(1, testUnionOffset, 0);
  }
  static addTestVectorOfUnionType(builder, testVectorOfUnionTypeOffset) {
    builder.addFieldOffset(2, testVectorOfUnionTypeOffset, 0);
  }
  static createTestVectorOfUnionTypeVector(builder, data) {
    builder.startVector(4, data.length, 4);
    for (let i = data.length - 1; i >= 0; i--) {
      builder.addInt32(data[i]);
    }
    return builder.endVector();
  }
  static startTestVectorOfUnionTypeVector(builder, numElems) {
    builder.startVector(4, numElems, 4);
  }
  static addTestVectorOfUnion(builder, testVectorOfUnionOffset) {
    builder.addFieldOffset(3, testVectorOfUnionOffset, 0);
  }
  static createTestVectorOfUnionVector(builder, data) {
    builder.startVector(4, data.length, 4);
    for (let i = data.length - 1; i >= 0; i--) {
      builder.addOffset(data[i]);
    }
    return builder.endVector();
  }
  static startTestVectorOfUnionVector(builder, numElems) {
    builder.startVector(4, numElems, 4);
  }
  static endD(builder) {
    const offset = builder.endObject();
    return offset;
  }
  static createD(
      builder, testUnionType, testUnionOffset, testVectorOfUnionTypeOffset,
      testVectorOfUnionOffset) {
    D.startD(builder);
    D.addTestUnionType(builder, testUnionType);
    D.addTestUnion(builder, testUnionOffset);
    D.addTestVectorOfUnionType(builder, testVectorOfUnionTypeOffset);
    D.addTestVectorOfUnion(builder, testVectorOfUnionOffset);
    return D.endD(builder);
  }
  unpack() {
    return new DT(
        this.testUnionType(), (() => {
          const temp =
              unionToAbc(this.testUnionType(), this.testUnion.bind(this));
          if (temp === null) {
            return null;
          }
          return temp.unpack();
        })(),
        this.bb.createScalarList(
            this.testVectorOfUnionType.bind(this),
            this.testVectorOfUnionTypeLength()),
        (() => {
          const ret = [];
          for (let targetEnumIndex = 0;
               targetEnumIndex < this.testVectorOfUnionTypeLength();
               ++targetEnumIndex) {
            const targetEnum = this.testVectorOfUnionType(targetEnumIndex);
            if (targetEnum === null || ABC[targetEnum] === 'NONE') {
              continue;
            }
            const temp = unionListToAbc(
                targetEnum, this.testVectorOfUnion.bind(this), targetEnumIndex);
            if (temp === null) {
              continue;
            }
            ret.push(temp.unpack());
          }
          return ret;
        })());
  }
  unpackTo(_o) {
    _o.testUnionType = this.testUnionType();
    _o.testUnion = (() => {
      const temp = unionToAbc(this.testUnionType(), this.testUnion.bind(this));
      if (temp === null) {
        return null;
      }
      return temp.unpack();
    })();
    _o.testVectorOfUnionType = this.bb.createScalarList(
        this.testVectorOfUnionType.bind(this),
        this.testVectorOfUnionTypeLength());
    _o.testVectorOfUnion = (() => {
      const ret = [];
      for (let targetEnumIndex = 0;
           targetEnumIndex < this.testVectorOfUnionTypeLength();
           ++targetEnumIndex) {
        const targetEnum = this.testVectorOfUnionType(targetEnumIndex);
        if (targetEnum === null || ABC[targetEnum] === 'NONE') {
          continue;
        }
        const temp = unionListToAbc(
            targetEnum, this.testVectorOfUnion.bind(this), targetEnumIndex);
        if (temp === null) {
          continue;
        }
        ret.push(temp.unpack());
      }
      return ret;
    })();
  }
}
export class DT {
  constructor(
      testUnionType = ABC.NONE, testUnion = null, testVectorOfUnionType = [],
      testVectorOfUnion = []) {
    this.testUnionType = testUnionType;
    this.testUnion = testUnion;
    this.testVectorOfUnionType = testVectorOfUnionType;
    this.testVectorOfUnion = testVectorOfUnion;
  }
  pack(builder) {
    const testUnion = builder.createObjectOffset(this.testUnion);
    const testVectorOfUnionType = D.createTestVectorOfUnionTypeVector(
        builder, this.testVectorOfUnionType);
    const testVectorOfUnion = D.createTestVectorOfUnionVector(
        builder, builder.createObjectOffsetList(this.testVectorOfUnion));
    return D.createD(
        builder, this.testUnionType, testUnion, testVectorOfUnionType,
        testVectorOfUnion);
  }
}
