(function () {
    const MB = window.MagicBot;

    // --- LOGIC: Commands ---
    MB.sendMsg = function (msg) {
        if (!MB.socket) {
            alert("Socket not connected! Wait for game to load.");
            return;
        }
        MB.socket.send(JSON.stringify(msg));
    };

    MB.harvestLoop = async function (start, end, count, delay) {
        let sent = 0;
        for (let slot = start; slot <= end; slot++) {
            for (let i = 0; i < count; i++) {
                MB.sendMsg({
                    type: 'HarvestCrop',
                    slot: slot,
                    slotsIndex: i,
                    scopePath: ["Room", "Quinoa"]
                });
                sent++;
                await new Promise(r => setTimeout(r, delay));
            }
        }
        console.log(`[MagicBot] Harvested ${sent} items.`);
    };

    MB.teleport = function (x, y) {
        MB.sendMsg({
            type: 'Teleport',
            position: { x, y },
            scopePath: ["Room", "Quinoa"]
        });
    }

    MB.sellAll = function () {
        MB.sendMsg({
            type: 'SellAllCrops',
            scopePath: ["Room", "Quinoa"]
        });
    }

    console.log('[MagicBot] Commands module loaded.');
})();
