import { RenderDeviceError } from "./RenderDevice.js";
export class IndexBuffer {
    type;
    data;
    gpuBuffer = null;
    disposed = false;
    constructor(indices, vertexCount) {
        if (indices.length === 0) {
            throw new Error("IndexBuffer requires at least one index");
        }
        let maxIndex = 0;
        for (const index of indices) {
            if (!Number.isInteger(index) || index < 0) {
                throw new Error("IndexBuffer indices must be non-negative integers");
            }
            if (index > maxIndex)
                maxIndex = index;
        }
        if (vertexCount !== undefined && maxIndex >= vertexCount) {
            throw new RangeError(`Index ${maxIndex} is outside vertex count ${vertexCount}`);
        }
        this.type = maxIndex > 65535 ? "uint32" : "uint16";
        this.data = this.type === "uint32" ? new Uint32Array(indices) : new Uint16Array(indices);
    }
    get count() {
        return this.data.length;
    }
    get byteLength() {
        return this.data.byteLength;
    }
    get uploadedBuffer() {
        return this.gpuBuffer;
    }
    upload(device) {
        this.assertAlive();
        if (!this.gpuBuffer || this.gpuBuffer.disposed) {
            this.gpuBuffer = device.createBuffer("index", this.data.byteLength, this.data);
        }
        return this.gpuBuffer;
    }
    dispose() {
        this.gpuBuffer?.dispose();
        this.disposed = true;
    }
    assertAlive() {
        if (this.disposed) {
            throw new RenderDeviceError("IndexBuffer is disposed", "DISPOSED_RESOURCE");
        }
    }
}
//# sourceMappingURL=IndexBuffer.js.map