import * as utils from "../../utils/index";
import { ComponentCompanion } from "../../npc/components/base";
var BiomeOptions;
(function (BiomeOptions) {
    BiomeOptions[BiomeOptions["basalt_deltas"] = 0] = "basalt_deltas";
    BiomeOptions[BiomeOptions["birch"] = 1] = "birch";
    BiomeOptions[BiomeOptions["cherry_grove"] = 2] = "cherry_grove";
    BiomeOptions[BiomeOptions["crimson_forest"] = 3] = "crimson_forest";
    BiomeOptions[BiomeOptions["deep_dark"] = 4] = "deep_dark";
    BiomeOptions[BiomeOptions["desert"] = 5] = "desert";
    BiomeOptions[BiomeOptions["dripstone_caves"] = 6] = "dripstone_caves";
    BiomeOptions[BiomeOptions["flower_forest"] = 7] = "flower_forest";
    BiomeOptions[BiomeOptions["ice_plains_spikes"] = 8] = "ice_plains_spikes";
    BiomeOptions[BiomeOptions["jungle"] = 9] = "jungle";
    BiomeOptions[BiomeOptions["lush_caves"] = 10] = "lush_caves";
    BiomeOptions[BiomeOptions["ocean"] = 11] = "ocean";
    BiomeOptions[BiomeOptions["plains"] = 12] = "plains";
    BiomeOptions[BiomeOptions["roofed_forest"] = 13] = "roofed_forest";
    BiomeOptions[BiomeOptions["savanna"] = 14] = "savanna";
    BiomeOptions[BiomeOptions["snowy_slopes"] = 15] = "snowy_slopes";
    BiomeOptions[BiomeOptions["soul_sand_valley"] = 16] = "soul_sand_valley";
    BiomeOptions[BiomeOptions["swamp"] = 17] = "swamp";
    BiomeOptions[BiomeOptions["the_end"] = 18] = "the_end";
    BiomeOptions[BiomeOptions["warped_forest"] = 19] = "warped_forest";
    BiomeOptions[BiomeOptions["any"] = 20] = "any";
})(BiomeOptions || (BiomeOptions = {}));
const HATS = [
    {
        itemId: "ldz_crcomp:hat_arrow_head",
        hatIndex: 0,
        biomeOptions: [BiomeOptions.any],
        weight: 3,
        heightOffset: 25,
    },
    {
        itemId: "ldz_crcomp:hat_astronaut",
        hatIndex: 1,
        biomeOptions: [BiomeOptions.any],
        weight: 3,
        heightOffset: 23,
    },
    {
        itemId: "ldz_crcomp:hat_clown",
        hatIndex: 2,
        biomeOptions: [BiomeOptions.any],
        weight: 3,
        heightOffset: 23,
    },
    {
        itemId: "ldz_crcomp:hat_conductor",
        hatIndex: 3,
        biomeOptions: [BiomeOptions.any],
        weight: 3,
        heightOffset: 30,
    },
    {
        itemId: "ldz_crcomp:hat_cowboy",
        hatIndex: 4,
        biomeOptions: [BiomeOptions.any],
        weight: 2,
        heightOffset: 30,
    },
    {
        itemId: "ldz_crcomp:hat_crown1",
        hatIndex: 5,
        biomeOptions: [BiomeOptions.any],
        weight: 1,
        heightOffset: 30,
    },
    {
        itemId: "ldz_crcomp:hat_diver",
        hatIndex: 6,
        biomeOptions: [BiomeOptions.any],
        weight: 3,
        heightOffset: 24,
    },
    {
        itemId: "ldz_crcomp:hat_super_anime_hair",
        hatIndex: 7,
        biomeOptions: [BiomeOptions.any],
        weight: 3,
        heightOffset: 30,
    },
    {
        itemId: "ldz_crcomp:hat_posh",
        hatIndex: 8,
        biomeOptions: [BiomeOptions.any],
        weight: 3,
        heightOffset: 30,
    },
    {
        itemId: "ldz_crcomp:hat_headlamp",
        hatIndex: 9,
        biomeOptions: [BiomeOptions.any],
        weight: 3,
        heightOffset: 30,
    },
    {
        itemId: "ldz_crcomp:hat_helicopter_beanie",
        hatIndex: 10,
        biomeOptions: [BiomeOptions.any],
        weight: 3,
        heightOffset: 30,
    },
    {
        itemId: "ldz_crcomp:hat_homburg",
        hatIndex: 11,
        biomeOptions: [BiomeOptions.any],
        weight: 3,
        heightOffset: 30,
    },
    {
        itemId: "ldz_crcomp:hat_mohawk",
        hatIndex: 12,
        biomeOptions: [BiomeOptions.any],
        weight: 2,
        heightOffset: 25,
    },
    {
        itemId: "ldz_crcomp:hat_motorbike",
        hatIndex: 13,
        biomeOptions: [BiomeOptions.any],
        weight: 3,
        heightOffset: 23,
    },
    {
        itemId: "ldz_crcomp:hat_musketeer",
        hatIndex: 14,
        biomeOptions: [BiomeOptions.any],
        weight: 3,
        heightOffset: 30,
    },
    {
        itemId: "ldz_crcomp:hat_nastee",
        hatIndex: 15,
        biomeOptions: [BiomeOptions.any],
        weight: 2,
        heightOffset: 30,
    },
    {
        itemId: "ldz_crcomp:hat_party",
        hatIndex: 16,
        biomeOptions: [BiomeOptions.any],
        weight: 3,
        heightOffset: 30,
    },
    {
        itemId: "ldz_crcomp:hat_pirate",
        hatIndex: 17,
        biomeOptions: [BiomeOptions.any],
        weight: 2,
        heightOffset: 30,
    },
    {
        itemId: "ldz_crcomp:hat_pizza",
        hatIndex: 18,
        biomeOptions: [BiomeOptions.any],
        weight: 3,
        heightOffset: 31,
    },
    {
        itemId: "ldz_crcomp:hat_plunger",
        hatIndex: 19,
        biomeOptions: [BiomeOptions.any],
        weight: 2,
        heightOffset: 27.5,
    },
    {
        itemId: "ldz_crcomp:hat_rabbit",
        hatIndex: 20,
        biomeOptions: [BiomeOptions.any],
        weight: 3,
        heightOffset: 25,
    },
    {
        itemId: "ldz_crcomp:hat_roman",
        hatIndex: 21,
        biomeOptions: [BiomeOptions.any],
        weight: 3,
        heightOffset: 24,
    },
    {
        itemId: "ldz_crcomp:hat_rubber_boot",
        hatIndex: 22,
        biomeOptions: [BiomeOptions.any],
        weight: 2,
        heightOffset: 30.5,
    },
    {
        itemId: "ldz_crcomp:hat_sakura_parkette",
        hatIndex: 23,
        biomeOptions: [BiomeOptions.any],
        weight: 3,
        heightOffset: 29.5,
    },
    {
        itemId: "ldz_crcomp:hat_santa",
        hatIndex: 24,
        biomeOptions: [BiomeOptions.any],
        weight: 2,
        heightOffset: 26,
    },
    {
        itemId: "ldz_crcomp:hat_sherlock",
        hatIndex: 25,
        biomeOptions: [BiomeOptions.any],
        weight: 3,
        heightOffset: 29,
    },
    {
        itemId: "ldz_crcomp:hat_sombrero",
        hatIndex: 26,
        biomeOptions: [BiomeOptions.any],
        weight: 3,
        heightOffset: 24,
    },
    {
        itemId: "ldz_crcomp:hat_top",
        hatIndex: 27,
        biomeOptions: [BiomeOptions.any],
        weight: 2,
        heightOffset: 24,
    },
    {
        itemId: "ldz_crcomp:hat_lampshade",
        hatIndex: 28,
        biomeOptions: [BiomeOptions.any],
        weight: 3,
        heightOffset: 29,
    },
    {
        itemId: "ldz_crcomp:hat_viking",
        hatIndex: 29,
        biomeOptions: [BiomeOptions.any],
        weight: 3,
        heightOffset: 23,
    },
];
export class HatOptionsComponent extends ComponentCompanion {
    async onComponentAdded(npc) {
        super.onComponentAdded(npc);
        if (npc.Hat)
            return;
        npc.Hat = this.getAvailableHat(npc);
    }
    async onComponentRemoved(npc) {
        var _a;
        utils.abortEvents(((_a = this.npc) === null || _a === void 0 ? void 0 : _a.ID) + this.id);
    }
    getAvailableHat(npc) {
        var _a;
        if (npc.HatIndex > -1) {
            return HATS[npc.HatIndex];
        }
        const obtainedHats = (_a = utils.readDynamicProperty(npc.Owner, "obtainedHats")) !== null && _a !== void 0 ? _a : [];
        const biome = npc.Entity.getProperty("biome_index");
        const availableHats = HATS.filter(hat => {
            const validBiome = hat.biomeOptions.includes(BiomeOptions.any) || hat.biomeOptions.includes(biome);
            const unobtained = (!obtainedHats.includes(hat.itemId) && utils.getRandomInteger(0, 9) > 0) || obtainedHats.length >= HATS.length;
            return validBiome && unobtained;
        });
        const totalWeight = availableHats.map(hat => hat.weight).reduce((a, b) => a + b);
        const randomValue = utils.getRandomInteger(0, totalWeight);
        let weightSum = 0;
        for (const hat of availableHats) {
            weightSum += hat.weight;
            if (randomValue <= weightSum) {
                return hat;
            }
        }
        return utils.getRandomArrayEntry(HATS);
    }
}
