import { Tool } from "./base_tool.js";
import { Tools } from "./tool_manager.js";
import { PlayerUtil } from "./../modules/player_util.js";
import { Selection } from "./../modules/selection.js";
class BrushTool extends Tool {
    constructor(brush, mask) {
        super();
        this.range = null;
        this.mask = null;
        this.traceMask = null;
        this.permission = "worldedit.brush";
        this.outlines = new Map();
        this.prevTick = 0;
        this.ticksToUpdate = 0;
        this.use = function* (self, player, session) {
            const hit = PlayerUtil.traceForBlock(player, self.range, self.traceMask);
            if (!hit) {
                throw "commands.wedit:jumpto.none";
            }
            yield* self.brush.apply(hit, session, self.mask);
        };
        this.tick = function* (self, player, session, tick) {
            this.ticksToUpdate -= tick - this.prevTick;
            this.prevTick = tick;
            if (this.ticksToUpdate > 0 || !session.drawOutlines)
                return;
            this.ticksToUpdate = 3;
            const hit = PlayerUtil.traceForBlock(player, self.range, self.traceMask);
            yield;
            if (hit) {
                if (!self.outlines.has(session)) {
                    const selection = new Selection(player);
                    self.outlines.set(session, { selection, lastHit: hit });
                }
                const { selection, lastHit } = self.outlines.get(session);
                self.brush.updateOutline(selection, hit);
                if (lastHit && !lastHit.equals(hit)) {
                    selection.forceDraw();
                }
                else {
                    selection.draw();
                }
                self.outlines.get(session).lastHit = hit;
                yield;
            }
        };
        this.brush = brush;
        this.mask = mask;
    }
    set size(value) {
        this.brush.resize(value);
    }
    set material(value) {
        this.brush.paintWith(value);
    }
}
Tools.register(BrushTool, "brush");
