import { assertExists, noop } from '@blockeditor/global/utils';
import { Workspace } from '@blockeditor/store';
import {} from '../../../../_common/utils/index.js';
import { Bound, Vec } from '../../../../surface-block/index.js';
import { EdgelessToolController } from './index.js';
export class FrameToolController extends EdgelessToolController {
    constructor() {
        super(...arguments);
        this.tool = {
            type: 'frame',
        };
        this._startPoint = null;
        this._frame = null;
    }
    _toModelCoord(p) {
        return this._surface.viewport.toModelCoord(p.x, p.y);
    }
    onContainerPointerDown() {
        noop();
    }
    onContainerDragStart(e) {
        this._page.captureSync();
        const { point } = e;
        this._startPoint = this._toModelCoord(point);
    }
    onContainerDragMove(e) {
        const currentPoint = this._toModelCoord(e.point);
        const surface = this._surface;
        assertExists(this._startPoint);
        if (Vec.dist(this._startPoint, currentPoint) < 8 && !this._frame)
            return;
        if (!this._frame) {
            const frames = surface.frame.frames;
            const id = surface.addElement('affine:frame', {
                title: new Workspace.Y.Text(`Frame ${frames.length + 1}`),
                xywh: Bound.fromPoints([this._startPoint, currentPoint]).serialize(),
            }, surface.model);
            this._frame = surface.pickById(id);
            this._frame.stash('xywh');
            return;
        }
        assertExists(this._frame);
        this._surface.updateElement(this._frame.id, {
            xywh: Bound.fromPoints([this._startPoint, currentPoint]).serialize(),
        });
    }
    onContainerDragEnd() {
        if (this._frame) {
            const frame = this._frame;
            this._page.transact(() => {
                frame.pop('xywh');
            });
            this._edgeless.tools.setEdgelessTool({ type: 'default' });
            this._edgeless.selectionManager.set({
                elements: [this._frame.id],
                editing: false,
            });
            this._page.captureSync();
        }
        this._frame = null;
        this._startPoint = null;
    }
    onContainerClick() {
        noop();
    }
    onContainerDblClick() {
        noop();
    }
    onContainerTripleClick() {
        noop();
    }
    onContainerMouseMove() {
        noop();
    }
    onContainerMouseOut() {
        noop();
    }
    onContainerContextMenu() {
        noop();
    }
    onPressShiftKey() {
        noop();
    }
    beforeModeSwitch() {
        noop();
    }
    afterModeSwitch() {
        noop();
    }
}
//# sourceMappingURL=frame-tool.js.map