///////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2002-2025, 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-2025 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: 0x004e4042 };

export class GLTFBinaryExtension {
  public content: string;
  public body: ArrayBuffer;

  constructor(data: ArrayBuffer) {
    const headerView = new DataView(data, 0, BINARY_EXTENSION_HEADER_LENGTH);
    const textDecoder = new TextDecoder();

    const magic = textDecoder.decode(new Uint8Array(data.slice(0, 4)));

    if (magic !== BINARY_EXTENSION_HEADER_MAGIC) {
      this.content = textDecoder.decode(data);
      return;
    }

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

    if (header.magic !== BINARY_EXTENSION_HEADER_MAGIC) {
      throw new Error("Unsupported glTF-Binary header.");
    }

    if (header.version < 2.0) {
      throw new Error("Legacy binary file detected.");
    }

    const chunkContentsLength = header.length - BINARY_EXTENSION_HEADER_LENGTH;
    const chunkView = new DataView(data, BINARY_EXTENSION_HEADER_LENGTH);
    let chunkIndex = 0;

    while (chunkIndex < chunkContentsLength) {
      const chunkLength = chunkView.getUint32(chunkIndex, true);
      chunkIndex += 4;

      const chunkType = chunkView.getUint32(chunkIndex, true);
      chunkIndex += 4;

      if (chunkType === BINARY_EXTENSION_CHUNK_TYPES.JSON) {
        const contentArray = new Uint8Array(data, BINARY_EXTENSION_HEADER_LENGTH + chunkIndex, chunkLength);
        this.content = textDecoder.decode(contentArray);
      } else if (chunkType === BINARY_EXTENSION_CHUNK_TYPES.BIN) {
        const byteOffset = BINARY_EXTENSION_HEADER_LENGTH + chunkIndex;
        this.body = data.slice(byteOffset, byteOffset + chunkLength);
      }

      chunkIndex += chunkLength;
    }

    if (typeof this.content === "undefined") {
      throw new Error("JSON content not found.");
    }
  }
}
