"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const THREE = __importStar(require("three"));
const GLTFExporter_js_1 = require("three/examples/jsm/exporters/GLTFExporter.js");
const boxel_three_1 = require("@playdrop/boxel-three");
const boxel_core_1 = require("@playdrop/boxel-core");
const vox_three_1 = require("@playdrop/vox-three");
function arrayBufferToBase64(buffer) {
    const bytes = new Uint8Array(buffer);
    const chunkSize = 0x8000;
    let binary = '';
    for (let index = 0; index < bytes.length; index += chunkSize) {
        const chunk = bytes.subarray(index, index + chunkSize);
        binary += String.fromCharCode(...chunk);
    }
    return btoa(binary);
}
let cachedRenderer = null;
function getOrCreateRenderer() {
    if (cachedRenderer) {
        return cachedRenderer;
    }
    const canvas = document.getElementById('viewport');
    if (!canvas) {
        throw new Error('render_canvas_unavailable');
    }
    cachedRenderer = new THREE.WebGLRenderer({
        canvas,
        antialias: true,
        preserveDrawingBuffer: true,
        alpha: true,
    });
    return cachedRenderer;
}
function base64ToArrayBuffer(value) {
    const binary = atob(value);
    const bytes = new Uint8Array(binary.length);
    for (let index = 0; index < binary.length; index += 1) {
        bytes[index] = binary.charCodeAt(index);
    }
    return bytes.buffer;
}
async function exportBoxel(modelJson, options) {
    const model = (0, boxel_core_1.parseBoxel)(modelJson);
    const blob = await (0, boxel_three_1.exportModelToGLB)({
        THREE,
        GLTFExporterCtor: GLTFExporter_js_1.GLTFExporter,
        model,
        mode: options?.mode,
        animationIds: options?.animationIds,
    });
    const arrayBuffer = await blob.arrayBuffer();
    return arrayBufferToBase64(arrayBuffer);
}
async function renderBoxel(modelJson, options) {
    const model = (0, boxel_core_1.parseBoxel)(modelJson);
    const renderer = getOrCreateRenderer();
    renderer.setPixelRatio(1);
    return (0, boxel_three_1.renderBoxelToDataURL)({
        THREE,
        renderer,
        entity: model,
        mode: options?.mode ?? 'hierarchical',
        animationIds: options?.animationIds,
        animationId: options?.animationId ?? undefined,
        width: options?.width,
        height: options?.height,
        direction: options?.direction,
        background: options?.background ?? null,
    });
}
async function renderVox(voxBase64, options) {
    if (typeof voxBase64 !== 'string' || voxBase64.length === 0) {
        throw new Error('vox_preview_input_missing');
    }
    const renderer = getOrCreateRenderer();
    renderer.setPixelRatio(1);
    return (0, vox_three_1.renderVoxToDataURL)({
        THREE,
        renderer,
        input: base64ToArrayBuffer(voxBase64),
        width: options?.width,
        height: options?.height,
    });
}
window.playdropBoxelWorker = {
    exportBoxel,
    renderBoxel,
    renderVox,
};
