import { system } from "@minecraft/server";

/** @enum {number} */
export const PetEquipmentColor = {
    white: 0,
    orange: 1,
    magenta: 2,
    light_blue: 3,
    yellow: 4,
    lime: 5,
    pink: 6,
    gray: 7,
    light_gray: 8,
    cyan: 9,
    purple: 10,
    blue: 11,
    brown: 12,
    green: 13,
    red: 14,
    black: 15
};

system.afterEvents.scriptEventReceive.subscribe(({ id, message, sourceEntity }) => {
    if (id != "billey:head_equipment_changed_to" || !sourceEntity?.isValid()) {
        return;
    }
    sourceEntity.setDynamicProperty("equipmentHead", message || undefined);
});

/**
 * @param {Entity} pet
 * @returns {string|undefined} The typeId of the item the pet is wearing on its head.
 * Undefined if it's not wearing anything on its head
 */
export function getPetHeadEquipmentId(pet) {
    return pet.getDynamicProperty("equipmentHead");
}

/**
 * Makes an item be considered a pet hat by billey's mobs.
 * @param {string} id The id of the item you want to be equippable to pets as a hat
 * @param {boolean} isBrave Whether this hat should make pets that don't usually defend their owner
    defend him anyway.
    This should be true for hats that make the pet stronger in combat,
    like billey's mobs's rat crown and rubber ducky
 * @param {boolean} isDyeable True for hats that should be dyeable.
    Due to limitations, dyeable pet equipment needs to have 16 textures,
    one for each main vanilla color, and be indexed by q.property('billey:head_equipment_color').
 * @param {PetEquipmentColor|undefined} defaultColor The default billeysMobsHats.PetEquipmentColor of the hat if it's dyeable. Set it to undefined if the piece is not dyeable.
 */
export function registerPetHat(id, isBrave, isDyeable, defaultColor) {
    system.sendScriptEvent("billey:register_pet_hat", JSON.stringify({
        id,
        isBrave,
        isDyeable,
        defaultColor
    }));
}