"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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SwordsEquipment = void 0;
const JSONTemplate_1 = require("modloader64_api/JSONTemplate");
const Z64API = __importStar(require("../../API/imports"));
const Z64CORE = __importStar(require("../importsZ64"));
class SwordsEquipment extends JSONTemplate_1.JSONTemplate {
    emulator;
    biggoron_flag_addr = Z64CORE.Z64.Z64_SAVE + 0x003e;
    biggoron_dmg_addr = Z64CORE.Z64.Z64_SAVE + 0x0036;
    core;
    jsonFields = [
        'kokiriSword',
        'masterSword',
        'giantKnife',
        'biggoronSword',
        'swordLevel',
    ];
    constructor(emulator, core) {
        super();
        this.emulator = emulator;
        this.core = core;
    }
    get kokiriSword() {
        return this.emulator.rdramReadBit8(Z64CORE.Z64.Z64_EQUIP_ADDR + 1, 7 /* SwordBitMap.KOKIRI_OOT */);
    }
    set kokiriSword(bool) {
        this.emulator.rdramWriteBit8(Z64CORE.Z64.Z64_EQUIP_ADDR + 1, 7 /* SwordBitMap.KOKIRI_OOT */, bool);
    }
    get masterSword() {
        return this.emulator.rdramReadBit8(Z64CORE.Z64.Z64_EQUIP_ADDR + 1, 6 /* SwordBitMap.MASTER */);
    }
    set masterSword(bool) {
        this.emulator.rdramWriteBit8(Z64CORE.Z64.Z64_EQUIP_ADDR + 1, 6 /* SwordBitMap.MASTER */, bool);
    }
    get giantKnife() {
        return this.emulator.rdramReadBit8(Z64CORE.Z64.Z64_EQUIP_ADDR + 1, 5 /* SwordBitMap.GIANT */) && this.emulator.rdramRead8(this.biggoron_flag_addr) === 0;
    }
    set giantKnife(bool) {
        this.emulator.rdramWriteBit8(Z64CORE.Z64.Z64_EQUIP_ADDR + 1, 5 /* SwordBitMap.GIANT */, bool);
        this.emulator.rdramWrite8(this.biggoron_flag_addr, 0);
        this.emulator.rdramWrite16(this.biggoron_dmg_addr, 8);
    }
    get biggoronSword() {
        return this.emulator.rdramReadBit8(Z64CORE.Z64.Z64_EQUIP_ADDR + 1, 5 /* SwordBitMap.BIGGORON */) && this.emulator.rdramRead8(this.biggoron_flag_addr) === 1;
    }
    set biggoronSword(bool) {
        this.emulator.rdramWriteBit8(Z64CORE.Z64.Z64_SAVE, 5 /* SwordBitMap.BIGGORON */, bool);
        this.emulator.rdramWrite8(this.biggoron_flag_addr, bool ? 1 : 0);
    }
    get swordLevel() {
        let bits = this.emulator.rdramReadBits8(Z64CORE.Z64.Z64_EQUIP_ADDR);
        if (bits[7 /* SwordBitMap.KOKIRI_MM */] === 1 && bits[6 /* SwordBitMap.GILDED */] === 0) {
            return Z64API.Z64.Sword.KOKIRI_MM;
        }
        else if (bits[7 /* SwordBitMap.KOKIRI_MM */] === 0 && bits[6 /* SwordBitMap.GILDED */] === 1) {
            return Z64API.Z64.Sword.RAZOR;
        }
        else if (bits[7 /* SwordBitMap.KOKIRI_MM */] === 1 && bits[6 /* SwordBitMap.GILDED */] === 1) {
            return Z64API.Z64.Sword.GILDED;
        }
        else {
            return Z64API.Z64.Sword.NONE;
        }
    }
    set swordLevel(level) {
        let bits = this.emulator.rdramReadBits8(Z64CORE.Z64.Z64_EQUIP_ADDR);
        switch (level) {
            case Z64API.Z64.Sword.NONE:
                bits[7 /* SwordBitMap.KOKIRI_MM */] = 0;
                bits[6 /* SwordBitMap.GILDED */] = 0;
                break;
            case Z64API.Z64.Sword.KOKIRI_MM:
                bits[7 /* SwordBitMap.KOKIRI_MM */] = 1;
                bits[6 /* SwordBitMap.GILDED */] = 0;
                break;
            case Z64API.Z64.Sword.RAZOR:
                bits[7 /* SwordBitMap.KOKIRI_MM */] = 0;
                bits[6 /* SwordBitMap.GILDED */] = 1;
                break;
            case Z64API.Z64.Sword.GILDED:
                bits[7 /* SwordBitMap.KOKIRI_MM */] = 1;
                bits[6 /* SwordBitMap.GILDED */] = 1;
                break;
        }
        this.emulator.rdramWriteBits8(Z64CORE.Z64.Z64_EQUIP_ADDR, bits);
    }
    updateSwordonB() {
        let level = this.swordLevel;
        switch (level) {
            case Z64API.Z64.Sword.NONE:
                if (this.emulator.rdramRead8(0x1EF6BC) !== 0x50) {
                    this.emulator.rdramWrite8(0x1EF6BC, 0xFF);
                }
                break;
            case Z64API.Z64.Sword.KOKIRI_MM:
                if (this.emulator.rdramRead8(0x1EF6BC) !== 0x50) {
                    this.emulator.rdramWrite8(0x1EF6BC, 0x4D);
                }
                break;
            case Z64API.Z64.Sword.RAZOR:
                if (this.emulator.rdramRead8(0x1EF6BC) !== 0x50) {
                    this.emulator.rdramWrite8(0x1EF6BC, 0x4E);
                }
                break;
            case Z64API.Z64.Sword.GILDED:
                if (this.emulator.rdramRead8(0x1EF6BC) !== 0x50) {
                    this.emulator.rdramWrite8(0x1EF6BC, 0x4F);
                }
                break;
        }
        this.core.commandBuffer.updateButton(0);
    }
}
exports.SwordsEquipment = SwordsEquipment;
//# sourceMappingURL=SwordsEquipment.js.map