"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.zzplayas_to_zzconvert_MM = void 0;
const smart_buffer_1 = require("smart-buffer");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const human_map_1 = require("./human_map");
class zzplayas_to_zzconvert_MM {
    convert_human(buf) {
        let out = new smart_buffer_1.SmartBuffer();
        out.writeBuffer(buf);
        if (buf.indexOf("!PlayAsManifest0") > -1) {
            let start = buf.indexOf("!PlayAsManifest0");
            out.writeOffset = start;
            out.readOffset = start;
            while (out.remaining() > 0) {
                out.readUInt8();
                out.writeUInt8(0);
            }
            out.writeOffset = start;
        }
        let template = fs_1.default.readFileSync(path_1.default.resolve(__dirname, "zobjs/zzconvert_human_template.zobj"));
        out.writeBuffer(template);
        let n = out.toBuffer();
        let start = n.indexOf("!PlayAsManifest0");
        let unwrap = (buf, start) => {
            let cur = buf.readUInt32BE(start + 0x4) - 0x06000000;
            while (cur >= 0x5000 && cur <= 0x5800) {
                cur = buf.readUInt32BE(cur + 0x4) - 0x06000000;
            }
            return cur;
        };
        let findDF = () => {
            for (let i = 0; i < buf.byteLength; i += 8) {
                let p1 = buf.readUInt32BE(i);
                let p2 = buf.readUInt32BE(i + 0x4);
                if (p1 === 0xDF000000 && p2 === 0) {
                    return i;
                }
            }
            return -1;
        };
        human_map_1.TRANSLATION_MAP_HUMAN.forEach((manifest, lut) => {
            let lut_offset = human_map_1.LUT_MAP_HUMAN.get(lut);
            let manifest_offset = human_map_1.MANIFEST_MAP_HUMAN.get(manifest) + start;
            if (lut_offset === undefined) {
                n.writeUInt32BE(findDF(), manifest_offset);
                return;
            }
            if (Number.isNaN(manifest_offset)) {
                return;
            }
            if (buf.readUInt8(lut_offset) === 0xDE) {
                n.writeUInt32BE(unwrap(buf, lut_offset), manifest_offset);
                //console.log(unwrap(buf, lut_offset).toString(16) + " to " + manifest);
            }
            else {
                let cur = buf.readUInt8(lut_offset);
                let advance = 0x0;
                while (cur !== 0xDE) {
                    advance += 0x8;
                    cur = buf.readUInt8(lut_offset + advance);
                }
                n.writeUInt32BE(unwrap(buf, (lut_offset + advance)), manifest_offset);
                //console.log(manifest);
                //console.log(unwrap(buf, lut_offset + advance).toString(16) + " to " + manifest);
            }
        });
        n.writeUInt32BE(0x06005380, 0x500C);
        return n;
    }
}
exports.zzplayas_to_zzconvert_MM = zzplayas_to_zzconvert_MM;
//# sourceMappingURL=zzplayas_to_zzconvert_MM.js.map