"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Tile
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.TileHeader = exports.TileReadError = exports.TileReadStatus = exports.TileFormat = void 0;
exports.isKnownTileFormat = isKnownTileFormat;
exports.tileFormatFromNumber = tileFormatFromNumber;
exports.nextPoint3d64FromByteStream = nextPoint3d64FromByteStream;
const core_bentley_1 = require("@itwin/core-bentley");
const core_geometry_1 = require("@itwin/core-geometry");
/** Type codes for various tile formats. Often these are embedded as 32-bit 'magic numbers' in a binary stream to indicate the format.
 * @internal
 */
var TileFormat;
(function (TileFormat) {
    TileFormat[TileFormat["Unknown"] = 0] = "Unknown";
    TileFormat[TileFormat["B3dm"] = 1835283298] = "B3dm";
    TileFormat[TileFormat["Gltf"] = 1179937895] = "Gltf";
    TileFormat[TileFormat["Pnts"] = 1937010288] = "Pnts";
    TileFormat[TileFormat["IModel"] = 1818512745] = "IModel";
    TileFormat[TileFormat["Cmpt"] = 1953525091] = "Cmpt";
    TileFormat[TileFormat["I3dm"] = 1835283305] = "I3dm";
    TileFormat[TileFormat["A3x"] = 5780289] = "A3x";
})(TileFormat || (exports.TileFormat = TileFormat = {}));
/** Given a magic number, return whether it identifies a known tile format.
 * @internal
 */
function isKnownTileFormat(format) {
    switch (format) {
        case TileFormat.Unknown:
        case TileFormat.B3dm:
        case TileFormat.Gltf:
        case TileFormat.IModel:
        case TileFormat.Pnts:
        case TileFormat.Cmpt:
        case TileFormat.I3dm:
        case TileFormat.A3x:
            return true;
        default:
            return false;
    }
}
/** Given a magic number, attempt to convert it to a known TileFormat.
 * @internal
 */
function tileFormatFromNumber(formatNumber) {
    const format = formatNumber;
    return isKnownTileFormat(format) ? format : TileFormat.Unknown;
}
/** Status codes for tile reading operations
 * @internal
 */
var TileReadStatus;
(function (TileReadStatus) {
    TileReadStatus[TileReadStatus["Success"] = 0] = "Success";
    TileReadStatus[TileReadStatus["InvalidTileData"] = 1] = "InvalidTileData";
    TileReadStatus[TileReadStatus["InvalidHeader"] = 2] = "InvalidHeader";
    TileReadStatus[TileReadStatus["InvalidBatchTable"] = 3] = "InvalidBatchTable";
    TileReadStatus[TileReadStatus["InvalidScene"] = 4] = "InvalidScene";
    TileReadStatus[TileReadStatus["InvalidFeatureTable"] = 5] = "InvalidFeatureTable";
    TileReadStatus[TileReadStatus["NewerMajorVersion"] = 6] = "NewerMajorVersion";
    TileReadStatus[TileReadStatus["Canceled"] = 7] = "Canceled";
})(TileReadStatus || (exports.TileReadStatus = TileReadStatus = {}));
const readStatusMessages = [
    "Success",
    "Invalid tile data",
    "Invalid tile header",
    "Invalid batch table",
    "Invalid scene",
    "Invalid feature table",
    "Major version too new",
    "Canceled",
];
/** Exception thrown by functions that deserialize tiles.
 * @internal
 */
class TileReadError extends core_bentley_1.BentleyError {
    constructor(status, message) {
        if (undefined === message)
            message = readStatusMessages[status];
        super(status, message);
    }
    get wasCanceled() { return TileReadStatus.Canceled === this.errorNumber; }
}
exports.TileReadError = TileReadError;
/** The base header preceding tile data of most formats, identifying the tile format and version of that format.
 * Specific tile formats may define their own headers as sub-types of this Header, appending
 * additional format-specific data.
 * @internal
 */
class TileHeader {
    _format;
    version;
    /** Construct a Header from the binary data at the supplied stream's current read position */
    constructor(stream) {
        this._format = tileFormatFromNumber(stream.readUint32());
        this.version = stream.readUint32();
    }
    get format() { return this._format; }
    /** Mark the header as representing invalid data */
    invalidate() {
        this._format = TileFormat.Unknown;
    }
}
exports.TileHeader = TileHeader;
/** Read 3 64-bit floating point numbers at the byte stream's current read position, advance by 24 bytes, and return a Point3d constructed from the 3 numbers.
 * @internal
 */
function nextPoint3d64FromByteStream(stream, result) {
    const x = stream.readFloat64(), y = stream.readFloat64(), z = stream.readFloat64();
    if (undefined === result)
        return new core_geometry_1.Point3d(x, y, z);
    result.set(x, y, z);
    return result;
}
//# sourceMappingURL=TileIO.js.map