///////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2002-2026, Open Design Alliance (the "Alliance").
// All rights reserved.
//
// This software and its documentation and related materials are owned by
// the Alliance. The software may only be incorporated into application
// programs owned by members of the Alliance, subject to a signed
// Membership Agreement and Supplemental Software License Agreement with the
// Alliance. The structure and organization of this software are the valuable
// trade secrets of the Alliance and its suppliers. The software is also
// protected by copyright law and international treaty provisions. Application
// programs incorporating this software must include the following statement
// with their copyright notices:
//
//   This application incorporates Open Design Alliance software pursuant to a
//   license agreement with Open Design Alliance.
//   Open Design Alliance Copyright (C) 2002-2026 by Open Design Alliance.
//   All rights reserved.
//
// By use of this software, its documentation or related materials, you
// acknowledge and accept the above terms.
///////////////////////////////////////////////////////////////////////////////

// ===================== AI-CODE-FILE ======================
// Source: Claude Sonnet 4.5
// Date: 2025-28-10
// Reviewer: roman.mochalov@opendesign.com
// Issue: CLOUD-5933
// Notes: Originally AI-generated, modified manually
// =========================================================

const BINARY_EXTENSION_HEADER_MAGIC = "glTF";
const BINARY_EXTENSION_HEADER_LENGTH = 12;
const BINARY_EXTENSION_CHUNK_TYPES = { JSON: 0x4e4f534a, BIN: 0x004e4942 };

export interface GLTFBinaryHeader {
  magic: string;
  version: number;
  length: number;
}

export class GLTFBinaryParser {
  public header: GLTFBinaryHeader;
  public content: Uint8Array;
  public body: ArrayBuffer;

  constructor(data: ArrayBuffer) {
    const dataView = new DataView(data, 0);

    const textDecoder = new TextDecoder();
    const magic = textDecoder.decode(new Uint8Array(data, 0, Math.min(4, data.byteLength)));

    if (magic !== BINARY_EXTENSION_HEADER_MAGIC) {
      this.content = new Uint8Array(data);
      return;
    }

    this.header = {
      magic,
      version: dataView.getUint32(4, true),
      length: dataView.getUint32(8, true),
    };

    if (this.header.length !== data.byteLength) {
      throw new Error("GLTFBinaryParser: Invalid binary file header.");
    }
    if (this.header.version < 2.0) {
      throw new Error("GLTFBinaryParser: Legacy binary file detected.");
    }

    let offset = BINARY_EXTENSION_HEADER_LENGTH;

    while (offset < this.header.length) {
      const chunkLength = dataView.getUint32(offset, true);
      offset += 4;

      const chunkType = dataView.getUint32(offset, true);
      offset += 4;

      if (chunkType === BINARY_EXTENSION_CHUNK_TYPES.JSON) {
        this.content = new Uint8Array(data, offset, chunkLength);
      } else if (chunkType === BINARY_EXTENSION_CHUNK_TYPES.BIN) {
        this.body = data.slice(offset, offset + chunkLength);
      }

      offset += chunkLength;
    }

    if (this.content === undefined) {
      throw new Error("GLTFBinaryParser: JSON content not found.");
    }
    if (this.body === undefined) {
      throw new Error("GLTFBinaryParser: Binary buffer chunk not found or type not supported.");
    }
  }
}
