"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createFaceArtifacts = createFaceArtifacts;
const face_map_1 = require("./face-map");
function createFaceArtifacts(params) {
    const { baseId, face, source, transform, expectedSize, textureWidth, textureHeight, texturePixels, ensureMaterial, variant = 'base', } = params;
    const transformed = (0, face_map_1.transformFacePixels)(source, transform, textureWidth, textureHeight, texturePixels);
    const width = transformed.width;
    const height = transformed.height;
    if (width !== expectedSize[0] || height !== expectedSize[1]) {
        throw new Error(`Transformed face for "${baseId}" (${face}) has size ${width}x${height} but expected ${expectedSize[0]}x${expectedSize[1]}`);
    }
    const textureMaterials = [];
    const localMaterialIndex = new Map();
    const patternData = [];
    let hasTransparency = false;
    let hasVisiblePixels = false;
    for (let row = 0; row < height; row += 1) {
        for (let col = 0; col < width; col += 1) {
            const idx = (row * width + col) * 4;
            const color = {
                r: transformed.data[idx],
                g: transformed.data[idx + 1],
                b: transformed.data[idx + 2],
                a: transformed.data[idx + 3],
            };
            if (color.a < 255) {
                hasTransparency = true;
            }
            if (color.a > 0) {
                hasVisiblePixels = true;
            }
            const materialId = ensureMaterial(color);
            let localIndex = localMaterialIndex.get(materialId);
            if (localIndex === undefined) {
                localIndex = textureMaterials.length;
                textureMaterials.push(materialId);
                localMaterialIndex.set(materialId, localIndex);
            }
            patternData.push(localIndex);
        }
    }
    if (variant === 'overlay' && !hasVisiblePixels) {
        return { pattern: null, texture: null, hasTransparency, hasVisiblePixels: false };
    }
    const suffix = variant === 'overlay' ? '_overlay' : '';
    const patternId = `${baseId}${suffix}`;
    const pattern = {
        id: patternId,
        size: [width, height],
        data: patternData,
    };
    const texture = {
        id: patternId,
        pattern: patternId,
        materials: textureMaterials,
    };
    return {
        pattern,
        texture,
        hasTransparency,
        hasVisiblePixels: true,
    };
}
//# sourceMappingURL=artifacts.js.map