import { system, world } from "@minecraft/server";
import { namespace } from "../config/_config";
import ScriptingModule from "./scriptingModule";
import { registerHints } from "../config/hints";
export class Hint {
    constructor({ identifier, message, sound }) {
        this.identifier = identifier;
        this.message = message;
        this.sound = sound;
    }
    static register(hint) {
        Hint.hints.push(hint);
    }
    static async hasReceivedHint(player, identifier) {
        const key = `${namespace}:hint_${identifier}`;
        return player.getDynamicProperty(key);
    }
    static async setReceivedHint(player, identifier) {
        const key = `${namespace}:hint_${identifier}`;
        return player.setDynamicProperty(key, true);
    }
    static initialize() {
        system.runInterval(() => {
            const players = world.getPlayers();
            players.forEach(async (player) => {
                const inventory = player.getComponent("inventory");
                const container = inventory.container;
                // Track which hints have been triggered in this loop to avoid duplicates
                const triggeredHints = new Set();
                for (let i = 0; i < container.size; i++) {
                    const item = container.getItem(i);
                    if (item) {
                        const itemIdentifier = item.typeId;
                        for (const hint of Hint.hints) {
                            // If the hint is already triggered for this scan, skip it
                            if (triggeredHints.has(hint.message))
                                continue;
                            if (hint.identifier.includes(itemIdentifier)) {
                                const hasReceived = await Hint.hasReceivedHint(player, hint.identifier[0]); // Use the first identifier as the key
                                if (!hasReceived) {
                                    player.sendMessage("");
                                    player.sendMessage({ translate: hint.message }); // Send the hint message
                                    player.sendMessage("");
                                    if (hint.sound) {
                                        player.playSound(hint.sound); // Play the sound if defined
                                    }
                                    await Hint.setReceivedHint(player, hint.identifier[0]); // Set the hint as received
                                    triggeredHints.add(hint.message); // Mark the hint as triggered
                                }
                            }
                        }
                    }
                }
            });
        }, 20);
        system.afterEvents.scriptEventReceive.subscribe((event) => {
            if (event.id === `${namespace}:clear_hints`) {
                // Get all players
                const players = world.getPlayers();
                players.forEach((player) => {
                    // Iterate through all dynamic properties
                    const properties = player.getDynamicPropertyIds();
                    for (const key of properties) {
                        // Check if the property key starts with the namespace hint prefix
                        if (key.startsWith(`${namespace}:hint_`)) {
                            player.setDynamicProperty(key, false); // Set the property to false
                        }
                    }
                });
                // Optionally send a confirmation message
                world.sendMessage("All hints have been reset for all players.");
            }
        });
    }
}
Hint.hints = [];
export default class HintModule extends ScriptingModule {
    constructor() {
        super("HintModule");
    }
    onInitialize() {
        registerHints();
    }
}
