var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { loadSchematic } from '@enginehub/schematicjs';
import { getModelLoader } from './model/loader';
import { getResourceLoader } from '../resource/resourceLoader';
import { POSSIBLE_FACES } from './model/types';
import { faceToFacingVector, INVISIBLE_BLOCKS, NON_OCCLUDING_BLOCKS, parseNbt, } from './utils';
import { Engine, Scene, Vector3, ArcRotateCamera, HemisphericLight, Color3, Color4, Mesh, ScenePerformancePriority, } from '@babylonjs/core';
import { loadBlockStateDefinition } from './model/parser';
import { addArrowToScene, addBarsToScene } from './shapes';
const CASSETTE_DECK_URL = `https://services.enginehub.org/cassette-deck/minecraft-versions/find?dataVersion=`;
const URL_1_13 = 'https://launcher.mojang.com/v1/objects/c0b970952cdd279912da384cdbfc0c26e6c6090b/client.jar';
function getClientJarUrlDefault({ dataVersion, corsBypassUrl, }) {
    var _a, _b;
    return __awaiter(this, void 0, void 0, function* () {
        const versionManifestFile = dataVersion
            ? yield (yield fetch(`${corsBypassUrl}${CASSETTE_DECK_URL}${dataVersion}`)).json()
            : undefined;
        return `${corsBypassUrl}${(_b = (_a = versionManifestFile === null || versionManifestFile === void 0 ? void 0 : versionManifestFile[0]) === null || _a === void 0 ? void 0 : _a.clientJarUrl) !== null && _b !== void 0 ? _b : URL_1_13}`;
    });
}
export function renderSchematic(canvas, schematic, { corsBypassUrl, getClientJarUrl = getClientJarUrlDefault, resourcePacks, size, orbit = true, orbitSpeed = 0.02, renderArrow = false, renderBars = false, antialias = false, backgroundColor = 0xffffff, debug = false, disableAutoRender = false, }) {
    return __awaiter(this, void 0, void 0, function* () {
        const engine = new Engine(canvas, antialias, {
            alpha: backgroundColor !== 'transparent',
            powerPreference: 'high-performance',
        });
        if (size) {
            if (typeof size === 'number') {
                engine.setSize(size, size);
                console.warn('Usage of deprecated `size: number` property in Schematic renderer.');
            }
            else {
                engine.setSize(size.width, size.height);
            }
        }
        const scene = new Scene(engine, {
            useGeometryUniqueIdsMap: true,
            useClonedMeshMap: true,
        });
        scene.performancePriority = ScenePerformancePriority.Intermediate;
        scene.renderingManager.maintainStateBetweenFrames = true;
        scene.skipFrustumClipping = true;
        scene.ambientColor = new Color3(0.5, 0.5, 0.5);
        if (backgroundColor !== 'transparent') {
            scene.clearColor = Color4.FromHexString(`#${backgroundColor.toString(16)}FF`);
        }
        else {
            scene.clearColor = new Color4(0, 0, 0, 0);
        }
        let hasDestroyed = false;
        const camera = new ArcRotateCamera('camera', -Math.PI / 2, Math.PI / 2.5, 10, new Vector3(0, 0, 0), scene);
        camera.wheelPrecision = 50;
        const light = new HemisphericLight('light1', new Vector3(1, 1, 0), scene);
        light.specular = new Color3(0, 0, 0);
        const render = () => {
            if (hasDestroyed) {
                return;
            }
            scene.render();
        };
        if (!disableAutoRender) {
            engine.runRenderLoop(render);
        }
        const loadedSchematic = loadSchematic(parseNbt(schematic));
        const { width: worldWidth, height: worldHeight, length: worldLength, } = loadedSchematic;
        const cameraOffset = Math.max(worldWidth, worldLength, worldHeight) / 2 + 1;
        camera.radius = cameraOffset * 3;
        const resourceLoader = yield getResourceLoader([
            yield getClientJarUrl({
                dataVersion: loadedSchematic.dataVersion,
                corsBypassUrl,
            }),
            ...(resourcePacks !== null && resourcePacks !== void 0 ? resourcePacks : []),
        ]);
        const modelLoader = getModelLoader(resourceLoader);
        const blockModelLookup = new Map();
        for (const block of loadedSchematic.blockTypes) {
            if (INVISIBLE_BLOCKS.has(block.type)) {
                continue;
            }
            const blockState = yield loadBlockStateDefinition(block.type, resourceLoader);
            const blockModelData = modelLoader.getBlockModelData(block, blockState);
            if (!blockModelData.models.length) {
                console.log(blockState);
                continue;
            }
            blockModelLookup.set(block, blockModelData);
        }
        Mesh.INSTANCEDMESH_SORT_TRANSPARENT = true;
        scene.blockMaterialDirtyMechanism = true;
        for (const pos of loadedSchematic) {
            const { x, y, z } = pos;
            const block = loadedSchematic.getBlock(pos);
            if (!block || INVISIBLE_BLOCKS.has(block.type)) {
                continue;
            }
            const modelData = blockModelLookup.get(block);
            if (!modelData) {
                continue;
            }
            let anyVisible = false;
            for (const face of POSSIBLE_FACES) {
                const faceOffset = faceToFacingVector(face);
                const offBlock = loadedSchematic.getBlock({
                    x: x + faceOffset[0],
                    y: y + faceOffset[1],
                    z: z + faceOffset[2],
                });
                if (!offBlock || NON_OCCLUDING_BLOCKS.has(offBlock.type)) {
                    anyVisible = true;
                    break;
                }
            }
            if (!anyVisible) {
                continue;
            }
            const option = modelLoader.getModelOption(modelData);
            const meshes = yield modelLoader.getModel(option, block, scene);
            for (const mesh of meshes) {
                if (!mesh) {
                    continue;
                }
                mesh.position.x += -worldWidth / 2 + x + 0.5;
                mesh.position.y += -worldHeight / 2 + y + 0.5;
                mesh.position.z += -worldLength / 2 + z + 0.5;
                mesh.freezeWorldMatrix();
                scene.addMesh(mesh);
            }
        }
        scene.blockMaterialDirtyMechanism = false;
        if (renderArrow) {
            addArrowToScene(scene, cameraOffset);
        }
        if (renderBars) {
            addBarsToScene(scene, cameraOffset, worldWidth, worldHeight, worldLength);
        }
        scene.createOrUpdateSelectionOctree();
        scene.freezeMaterials();
        if (debug) {
            scene.debugLayer.show();
        }
        blockModelLookup.clear();
        resourceLoader.clearCache();
        modelLoader.clearCache();
        camera.attachControl(false, true);
        if (orbit) {
            scene.registerBeforeRender(() => {
                camera.alpha += orbitSpeed;
            });
        }
        return {
            resize(size) {
                engine.setSize(size, size);
            },
            setSize(width, height) {
                engine.setSize(width, height);
            },
            destroy() {
                engine.dispose();
                hasDestroyed = true;
            },
            render,
            getEngine() {
                return engine;
            },
        };
    });
}
//# sourceMappingURL=renderer.js.map