var _a;
import * as utils from "../../utils/index";
import { ComponentCompanion } from "../../npc/components/base";
const requestList = [
    {
        index: 0,
        itemName: "minecraft:pink_petals",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "tile.pink_petals",
        suffix(count) {
            return { text: "§r" };
        },
    },
    {
        index: 1,
        itemName: "minecraft:sunflower",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "tile.double_plant.sunflower",
        suffix(count) {
            return { text: "s§r" };
        },
    },
    {
        index: 2,
        itemName: "minecraft:rose_bush",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "tile.double_plant.rose",
        suffix(count) {
            return { text: "es§r" };
        },
    },
    {
        index: 3,
        itemName: "minecraft:birch_sapling",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "tile.sapling.birch",
        suffix(count) {
            return { text: "s§r" };
        },
    },
    {
        index: 4,
        itemName: "minecraft:light_blue_candle",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "tile.light_blue_candle",
        suffix(count) {
            return { text: "s§r" };
        },
    },
    {
        index: 5,
        itemName: "minecraft:red_mushroom",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "tile.red_mushroom",
        suffix(count) {
            return { text: "s§r" };
        },
    },
    {
        index: 6,
        itemName: "minecraft:deadbush",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "tile.deadbush",
        suffix(count) {
            return { text: "s§r" };
        },
    },
    {
        index: 7,
        itemName: "minecraft:cocoa_beans",
        itemData: 0,
        itemMin: 1,
        itemMax: 5,
        itemLang: "item.dye.brown",
        suffix(count) {
            return { text: "§r" };
        },
    },
    {
        index: 8,
        itemName: "minecraft:bed",
        itemData: 3,
        itemMin: 1,
        itemMax: 1,
        itemLang: "item.bed.lightBlue",
        suffix(count) {
            return { text: "§r" };
        },
    },
    {
        index: 9,
        itemName: "minecraft:clock",
        itemData: 0,
        itemMin: 1,
        itemMax: 3,
        itemLang: "item.clock",
        suffix(count) {
            return count > 1 ? { text: "s§r" } : { text: "§r" };
        },
    },
    {
        index: 10,
        itemName: "minecraft:acacia_sapling",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "tile.sapling.acacia",
        suffix(count) {
            return { text: "s§r" };
        },
    },
    {
        index: 11,
        itemName: "minecraft:egg",
        itemData: 0,
        itemMin: 1,
        itemMax: 12,
        itemLang: "item.egg",
        suffix(count) {
            return count > 1 ? { text: "s§r" } : { text: "§r" };
        },
    },
    {
        index: 12,
        itemName: "minecraft:powder_snow_bucket",
        itemData: 0,
        itemMin: 1,
        itemMax: 3,
        itemLang: "item.bucketPowderSnow",
        suffix(count) {
            return count > 1 ? { text: "s§r" } : { text: "§r" };
        },
    },
    {
        index: 13,
        itemName: "minecraft:snowball",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "item.snowball",
        suffix(count) {
            return { text: "s§r" };
        },
    },
    {
        index: 14,
        itemName: "minecraft:armor_stand",
        itemData: 0,
        itemMin: 3,
        itemMax: 5,
        itemLang: "entity.armor_stand",
        suffix(count) {
            return { text: "s§r" };
        },
    },
    {
        index: 15,
        itemName: "minecraft:kelp",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "item.kelp",
        suffix(count) {
            return { text: "§r" };
        },
    },
    {
        index: 16,
        itemName: "minecraft:firework_rocket",
        itemData: 0,
        itemMin: 1,
        itemMax: 5,
        itemLang: "entity.fireworks_rocket",
        suffix(count) {
            return count > 1 ? { text: "s§r" } : { text: "§r" };
        },
    },
    {
        index: 17,
        itemName: "minecraft:waterlily",
        itemData: 0,
        itemMin: 1,
        itemMax: 5,
        itemLang: "tile.waterlily",
        suffix(count) {
            return count > 1 ? { text: "s§r" } : { text: "§r" };
        },
    },
    {
        index: 18,
        itemName: "minecraft:mangrove_propagule",
        itemData: 0,
        itemMin: 1,
        itemMax: 5,
        itemLang: "tile.mangrove_propagule",
        suffix(count) {
            return count > 1 ? { text: "s§r" } : { text: "§r" };
        },
    },
    {
        index: 19,
        itemName: "minecraft:clay_ball",
        itemData: 0,
        itemMin: 12,
        itemMax: 16,
        itemLang: "item.clay_ball",
        suffix(count) {
            return { text: "s§r" };
        },
    },
    {
        index: 20,
        itemName: "minecraft:soul_lantern",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "tile.soul_lantern",
        suffix(count) {
            return { text: "s§r" };
        },
    },
    {
        index: 21,
        itemName: "minecraft:pointed_dripstone",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "tile.pointed_dripstone",
        suffix(count) {
            return { text: "s§r" };
        },
    },
    {
        index: 22,
        itemName: "minecraft:glow_berries",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "item.glow_berries",
        suffix(count) {
            return { text: "§r" };
        },
    },
    {
        index: 23,
        itemName: "minecraft:quartz",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "item.quartz",
        suffix(count) {
            return { text: "§r" };
        },
    },
    {
        index: 24,
        itemName: "minecraft:sea_pickle",
        itemData: 0,
        itemMin: 1,
        itemMax: 5,
        itemLang: "tile.sea_pickle",
        suffix(count) {
            return count > 1 ? { text: "s§r" } : { text: "§r" };
        },
    },
    {
        index: 25,
        itemName: "minecraft:campfire",
        itemData: 0,
        itemMin: 1,
        itemMax: 16,
        itemLang: "tile.campfire",
        suffix(count) {
            return count > 1 ? { text: "s§r" } : { text: "§r" };
        },
    },
    {
        index: 26,
        itemName: "minecraft:crimson_fungus",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "tile.crimson_fungus",
        suffix(count) {
            return { text: "§r" };
        },
    },
    {
        index: 27,
        itemName: "minecraft:warped_fungus_on_a_stick",
        itemData: 0,
        itemMin: 1,
        itemMax: 16,
        itemLang: "item.warped_fungus_on_a_stick",
        suffix(count) {
            return { text: "§r" };
        },
    },
    {
        index: 28,
        itemName: "minecraft:sweet_berries",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "item.sweet_berries",
        suffix(count) {
            return { text: "§r" };
        },
    },
    {
        index: 29,
        itemName: "minecraft:ender_pearl",
        itemData: 0,
        itemMin: 1,
        itemMax: 5,
        itemLang: "item.ender_pearl",
        suffix(count) {
            return count > 1 ? { text: "s§r" } : { text: "§r" };
        },
    },
    {
        index: 30,
        itemName: "minecraft:blaze_rod",
        itemData: 0,
        itemMin: 1,
        itemMax: 5,
        itemLang: "item.blaze_rod",
        suffix(count) {
            return count > 1 ? { text: "s§r" } : { text: "§r" };
        },
    },
    {
        index: 31,
        itemName: "minecraft:blue_orchid",
        itemData: 0,
        itemMin: 1,
        itemMax: 16,
        itemLang: "tile.red_flower.blueOrchid",
        suffix(count) {
            return count > 1 ? { text: "s§r" } : { text: "§r" };
        },
    },
    {
        index: 32,
        itemName: "minecraft:bamboo",
        itemData: 1,
        itemMin: 1,
        itemMax: 16,
        itemLang: "tile.bamboo",
        suffix(count) {
            return { text: "§r" };
        },
    },
    {
        index: 33,
        itemName: "minecraft:cauldron",
        itemData: 0,
        itemMin: 1,
        itemMax: 5,
        itemLang: "tile.cauldron",
        suffix(count) {
            return count > 1 ? { text: "s§r" } : { text: "§r" };
        },
    },
    {
        index: 34,
        itemName: "minecraft:gunpowder",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "item.gunpowder",
        suffix(count) {
            return { text: "s§r" };
        },
    },
    {
        index: 35,
        itemName: "minecraft:rotten_flesh",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "item.rotten_flesh",
        suffix(count) {
            return { text: "s§r" };
        },
    },
    {
        index: 36,
        itemName: "minecraft:redstone",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "item.redstone",
        suffix(count) {
            return { text: "§r" };
        },
    },
    {
        index: 37,
        itemName: "minecraft:spider_eye",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "item.spider_eye",
        suffix(count) {
            return { text: "s§r" };
        },
    },
    {
        index: 38,
        itemName: "minecraft:bone",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "item.bone",
        suffix(count) {
            return { text: "s§r" };
        },
    },
    {
        index: 39,
        itemName: "minecraft:spyglass",
        itemData: 0,
        itemMin: 1,
        itemMax: 5,
        itemLang: "item.spyglass",
        suffix(count) {
            return count > 1 ? { text: "es§r" } : { text: "§r" };
        },
    },
    {
        index: 40,
        itemName: "minecraft:bell",
        itemData: 0,
        itemMin: 1,
        itemMax: 3,
        itemLang: "item.bell",
        suffix(count) {
            return count > 1 ? { text: "s§r" } : { text: "§r" };
        },
    },
    {
        index: 41,
        itemName: "minecraft:prismarine_shard",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "item.prismarine_shard",
        suffix(count) {
            return { text: "s§r" };
        },
    },
    {
        index: 42,
        itemName: "minecraft:rail",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "tile.rail",
        suffix(count) {
            return { text: "s§r" };
        },
    },
    {
        index: 43,
        itemName: "minecraft:ghast_tear",
        itemData: 0,
        itemMin: 3,
        itemMax: 5,
        itemLang: "item.ghast_tear",
        suffix(count) {
            return { text: "s§r" };
        },
    },
    {
        index: 44,
        itemName: "minecraft:apple",
        itemData: 0,
        itemMin: 1,
        itemMax: 16,
        itemLang: "item.apple",
        suffix(count) {
            return count > 1 ? { text: "s§r" } : { text: "§r" };
        },
    },
    {
        index: 45,
        itemName: "minecraft:slime_ball",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "item.slime_ball",
        suffix(count) {
            return { text: "s§r" };
        },
    },
    {
        index: 46,
        itemName: "minecraft:flint",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "item.flint",
        suffix(count) {
            return { text: "§r" };
        },
    },
    {
        index: 47,
        itemName: "minecraft:glowstone_dust",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "item.glowstone_dust",
        suffix(count) {
            return { text: "§r" };
        },
    },
    {
        index: 48,
        itemName: "minecraft:lapis_lazuli",
        itemData: 0,
        itemMin: 8,
        itemMax: 16,
        itemLang: "item.dye.blue",
        suffix(count) {
            return { text: "§r" };
        },
    },
];
const rewardList = [
    { index: 0, lootTable: 'ldz/crcomp/reward_0' },
    { index: 1, lootTable: 'ldz/crcomp/reward_1' },
    { index: 2, lootTable: 'ldz/crcomp/reward_2' },
    { index: 3, lootTable: 'ldz/crcomp/reward_3' },
    { index: 4, lootTable: 'ldz/crcomp/reward_4' },
    { index: 5, lootTable: 'ldz/crcomp/reward_5' },
    { index: 6, lootTable: 'ldz/crcomp/reward_6' },
    { index: 7, lootTable: 'ldz/crcomp/reward_7' },
    { index: 8, lootTable: 'ldz/crcomp/reward_8' },
    { index: 9, lootTable: 'ldz/crcomp/reward_9' },
    { index: 10, lootTable: 'ldz/crcomp/reward_10' },
    { index: 11, lootTable: 'ldz/crcomp/reward_11' },
    { index: 12, lootTable: 'ldz/crcomp/reward_12' },
    { index: 13, lootTable: 'ldz/crcomp/reward_13' },
    { index: 14, lootTable: 'ldz/crcomp/reward_14' },
    { index: 15, lootTable: 'ldz/crcomp/reward_15' },
    { index: 16, lootTable: 'ldz/crcomp/reward_16' },
    { index: 17, lootTable: 'ldz/crcomp/reward_17' },
    { index: 18, lootTable: 'ldz/crcomp/reward_18' },
    { index: 19, lootTable: 'ldz/crcomp/reward_19' },
    { index: 20, lootTable: 'ldz/crcomp/reward_20' },
    { index: 21, lootTable: 'ldz/crcomp/reward_21' },
    { index: 22, lootTable: 'ldz/crcomp/reward_22' },
    { index: 23, lootTable: 'ldz/crcomp/reward_23' },
    { index: 24, lootTable: 'ldz/crcomp/reward_24' },
    { index: 25, lootTable: 'ldz/crcomp/reward_25' },
    { index: 26, lootTable: 'ldz/crcomp/reward_26' },
    { index: 27, lootTable: 'ldz/crcomp/reward_27' },
    { index: 28, lootTable: 'ldz/crcomp/reward_28' },
    { index: 29, lootTable: 'ldz/crcomp/reward_29' },
];
export class QuestComponent extends ComponentCompanion {
    constructor() {
        super(...arguments);
        this.questData = { requestQuantity: 0, requestIndices: [], rewardIndices: [], hasQuest: false };
    }
    get QuestData() {
        return this.questData;
    }
    set QuestData(v) {
        this.questData = v;
        utils.writeDynamicProperty(this.npc.Entity, "questData", v);
    }
    get QuestStatus() {
        const questData = this.QuestData;
        if (!questData.hasQuest) {
            return "none";
        }
        const requestItem = requestList[questData.activeRequestIndex];
        const result = this.npc.Owner.runCommand(`testfor @s[hasitem=[{item=${requestItem.itemName},data=${requestItem.itemData},quantity=${questData.requestQuantity}..}]]`);
        return result.successCount ? "completed" : "pending";
    }
    get QuestDetails() {
        var _b, _c;
        const messageIndex = utils.getRandomInteger(0, 9);
        const requestItem = requestList[(_b = this.questData.activeRequestIndex) !== null && _b !== void 0 ? _b : 0];
        (_c = this.npc) === null || _c === void 0 ? void 0 : _c.Owner.playSound(`ldz_crcomp:craftee.quest_check.${messageIndex}`, { volume: this.npc.Volume });
        return { translate: `ldz_crcomp:craftee.quest_check.${messageIndex}`, with: { rawtext: [{ text: `§b${this.QuestData.requestQuantity} ` }, { translate: `${requestItem.itemLang}.name` }, requestItem.suffix(this.QuestData.requestQuantity), { translate: "\n" }] } };
    }
    async onComponentAdded(npc) {
        super.onComponentAdded(npc);
        this.questData = utils.readDynamicProperty(this.npc.Entity, "questData") || this.questData;
        if (this.QuestStatus !== "none") {
            this.showQuestParticle();
        }
    }
    showQuestParticle() {
        var _b, _c;
        (_b = this.npc) === null || _b === void 0 ? void 0 : _b.Entity.setProperty('ldz_crcomp:request_index', this.QuestData.activeRequestIndex);
        (_c = this.npc) === null || _c === void 0 ? void 0 : _c.Entity.setProperty('ldz_crcomp:request_quantity', this.QuestData.requestQuantity);
    }
    setRequestItem() {
        // Pick desired items from pool
        const questData = this.QuestData;
        let filteredRequestList = requestList;
        filteredRequestList = filteredRequestList.filter(item => !questData.requestIndices.includes(item.index));
        // If pool is empty, randomize and reset it
        if (filteredRequestList.length <= 0) {
            filteredRequestList = requestList;
            questData.requestIndices = [];
        }
        const request = filteredRequestList[utils.getRandomInteger(0, filteredRequestList.length - 1)];
        questData.requestIndices.push(request.index);
        questData.activeRequestIndex = request.index;
        questData.requestQuantity = utils.getRandomInteger(request.itemMin, request.itemMax);
        this.QuestData = questData;
    }
    setRewardItem() {
        // Pick desired items from pool
        const questData = this.QuestData;
        let filteredRewardList = rewardList;
        filteredRewardList = filteredRewardList.filter(item => !questData.rewardIndices.includes(item.index));
        // If pool is empty, randomize and reset it
        if (filteredRewardList.length <= 0) {
            filteredRewardList = rewardList;
            questData.rewardIndices = [];
        }
        const reward = filteredRewardList[utils.getRandomInteger(0, filteredRewardList.length - 1)];
        questData.rewardIndices.push(reward.index);
        questData.activeRewardIndex = reward.index;
        this.QuestData = questData;
    }
    setNewQuest() {
        var _b;
        this.setRequestItem();
        this.setRewardItem();
        const questData = this.QuestData;
        questData.hasQuest = true;
        this.QuestData = questData;
        this.showQuestParticle();
        (_b = this.npc) === null || _b === void 0 ? void 0 : _b.broadcastEvent("quest_give");
    }
    clearQuest() {
        var _b, _c, _d;
        (_b = this.npc) === null || _b === void 0 ? void 0 : _b.Entity.setProperty('ldz_crcomp:request_index', 0);
        (_c = this.npc) === null || _c === void 0 ? void 0 : _c.Entity.setProperty('ldz_crcomp:request_quantity', 0);
        (_d = this.npc) === null || _d === void 0 ? void 0 : _d.globalStopSound();
        const questData = this.QuestData;
        questData.hasQuest = false;
        this.QuestData = questData;
    }
    completeQuest() {
        var _b, _c, _d, _e, _f, _g;
        const request = requestList[(_b = this.QuestData.activeRequestIndex) !== null && _b !== void 0 ? _b : 0];
        const reward = rewardList[(_c = this.QuestData.activeRewardIndex) !== null && _c !== void 0 ? _c : 0];
        this.clearQuest();
        (_d = this.npc) === null || _d === void 0 ? void 0 : _d.Owner.playSound('random.pop');
        (_e = this.npc) === null || _e === void 0 ? void 0 : _e.Owner.runCommand(`clear @s ${request.itemName} ${request.itemData} ${this.QuestData.requestQuantity}`);
        (_f = this.npc) === null || _f === void 0 ? void 0 : _f.Owner.runCommand(`loot give @s loot "${reward.lootTable}"`);
        (_g = this.npc) === null || _g === void 0 ? void 0 : _g.broadcastEvent("quest_reward");
    }
}
_a = QuestComponent;
QuestComponent.ComponentId = _a.name;
