import {} from '@blockeditor/store';
import {} from '../../../_common/utils/index.js';
import { Bound, clamp, deserializeXYWH, getQuadBoundsWithRotation, GRID_GAP_MAX, GRID_GAP_MIN, ShapeElement, TextElement, } from '../../../surface-block/index.js';
import { getElementsWithoutGroup } from '../../../surface-block/managers/group-manager.js';
export function isTopLevelBlock(selectable) {
    return !!selectable && 'flavour' in selectable;
}
export function isNoteBlock(element) {
    return !!element && 'flavour' in element && element.flavour === 'affine:note';
}
export function isFrameBlock(element) {
    return (!!element && 'flavour' in element && element.flavour === 'affine:frame');
}
export function isImageBlock(element) {
    return (!!element && 'flavour' in element && element.flavour === 'affine:image');
}
export function isBookmarkBlock(element) {
    return (!!element && 'flavour' in element && element.flavour === 'affine:bookmark');
}
export function isEmbeddedBlock(element) {
    return (!!element && 'flavour' in element && /affine:embed-*/.test(element.flavour));
}
export function isEmbedGithubBlock(element) {
    return (!!element &&
        'flavour' in element &&
        element.flavour === 'affine:embed-github');
}
export function isEmbedYoutubeBlock(element) {
    return (!!element &&
        'flavour' in element &&
        element.flavour === 'affine:embed-youtube');
}
export function isEmbedFigmaBlock(element) {
    return (!!element &&
        'flavour' in element &&
        element.flavour === 'affine:embed-figma');
}
export function isEmbedLinkedDocBlock(element) {
    return (!!element &&
        'flavour' in element &&
        element.flavour === 'affine:embed-linked-doc');
}
export function isCanvasElement(selectable) {
    return !isTopLevelBlock(selectable);
}
export function isCanvasElementWithText(element) {
    return element instanceof TextElement || element instanceof ShapeElement;
}
export function isConnectable(element) {
    return !!element && element.connectable;
}
export function getSelectionBoxBound(viewport, bound) {
    const { w, h } = bound;
    const [x, y] = viewport.toViewCoord(bound.x, bound.y);
    return new DOMRect(x, y, w * viewport.zoom, h * viewport.zoom);
}
// https://developer.mozilla.org/en-US/docs/Web/CSS/cursor
export function getCursorMode(edgelessTool) {
    switch (edgelessTool.type) {
        case 'default':
            return 'default';
        case 'pan':
            return edgelessTool.panning ? 'grabbing' : 'grab';
        case 'brush':
        case 'eraser':
        case 'shape':
        case 'connector':
        case 'frame':
            return 'crosshair';
        case 'text':
            return 'text';
        default:
            return 'default';
    }
}
export function pickSurfaceElementById(surface, page, id) {
    const blocks = page.root?.children.filter(child => child.flavour === 'affine:note') ?? [];
    const element = surface.pickById(id) || blocks.find(b => b.id === id);
    return element;
}
export function getBackgroundGrid(zoom, showGrid) {
    const step = zoom < 0.5 ? 2 : 1 / (Math.floor(zoom) || 1);
    const gap = clamp(20 * step * zoom, GRID_GAP_MIN, GRID_GAP_MAX);
    return {
        gap,
        grid: showGrid
            ? 'radial-gradient(var(--affine-edgeless-grid-color) 1px, var(--affine-background-primary-color) 1px)'
            : 'unset',
    };
}
export function getSelectedRect(selected) {
    if (selected.length === 0) {
        return new DOMRect();
    }
    if (selected.length === 1) {
        const [x, y, w, h] = deserializeXYWH(selected[0].xywh);
        return new DOMRect(x, y, w, h);
    }
    return getElementsWithoutGroup(selected).reduce((bounds, selectable, index) => {
        const rotate = isTopLevelBlock(selectable) ? 0 : selectable.rotate;
        const [x, y, w, h] = deserializeXYWH(selectable.xywh);
        let { left, top, right, bottom } = getQuadBoundsWithRotation({
            x,
            y,
            w,
            h,
            rotate,
        });
        if (index !== 0) {
            left = Math.min(left, bounds.left);
            top = Math.min(top, bounds.top);
            right = Math.max(right, bounds.right);
            bottom = Math.max(bottom, bounds.bottom);
        }
        bounds.x = left;
        bounds.y = top;
        bounds.width = right - left;
        bounds.height = bottom - top;
        return bounds;
    }, new DOMRect());
}
export function getSelectableBounds(selected) {
    const bounds = new Map();
    getElementsWithoutGroup(selected).forEach(ele => {
        const bound = Bound.deserialize(ele.xywh);
        bounds.set(ele.id, {
            bound,
            rotate: ele.rotate,
        });
    });
    return bounds;
}
//# sourceMappingURL=query.js.map