"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_OOT = void 0;
const smart_buffer_1 = require("smart-buffer");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const adult_map_1 = require("./adult_map");
const child_map_1 = require("./child_map");
class zzplayas_to_zzconvert_OOT {
    convert_adult(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_adult_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;
        };
        adult_map_1.TRANSLATION_MAP_ADULT.forEach((manifest, lut) => {
            let lut_offset = adult_map_1.LUT_MAP_ADULT.get(lut);
            let manifest_offset = adult_map_1.MANIFEST_MAP_ADULT.get(manifest) + start;
            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;
    }
    convert_child(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_child_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;
        };
        child_map_1.TRANSLATION_MAP_CHILD.forEach((manifest, lut) => {
            let lut_offset = child_map_1.LUT_MAP_CHILD.get(lut);
            let manifest_offset = child_map_1.MANIFEST_MAP_CHILD.get(manifest) + start;
            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(0x060053A8, 0x500C);
        return n;
    }
}
exports.zzplayas_to_zzconvert_OOT = zzplayas_to_zzconvert_OOT;
//# sourceMappingURL=zzplayas_to_zzconvert_OOT.js.map