import { Workspace } from '@blockeditor/store';
import { Bound, CanvasElementType, ConnectorEndpointStyle, ConnectorMode, getLineWidth, normalizeShapeBound, SHAPE_TEXT_PADDING, ShapeStyle, StrokeStyle, } from '../../surface-block/index.js';
import { layout } from './layout.js';
export const DEFAULT_SHAPE_PROPS = {
    shapeType: 'rect',
    strokeColor: '--affine-palette-line-black',
    filled: false,
    radius: 0.1,
    strokeWidth: 2,
    strokeStyle: StrokeStyle.Solid,
    shapeStyle: ShapeStyle.General,
};
export const DEFAULT_CONNECTOR_PROPS = {
    stroke: '--affine-palette-line-black',
    mode: ConnectorMode.Orthogonal,
    controllers: [],
    strokeWidth: 2,
    strokeStyle: StrokeStyle.Solid,
    frontEndpointStyle: ConnectorEndpointStyle.None,
    rearEndpointStyle: ConnectorEndpointStyle.None,
};
const directionMap = {
    left: {
        from: [0, 0.5],
        to: [1, 0.5],
    },
    right: {
        from: [1, 0.5],
        to: [0, 0.5],
    },
    top: {
        from: [0.5, 0],
        to: [0.5, 1],
    },
    bottom: {
        from: [0.5, 1],
        to: [0.5, 0],
    },
};
const drawAllNode = (node, surface) => {
    const shapeIds = [];
    const connectorIds = [];
    const connectors = [];
    const drawNode = (node) => {
        const { text, children } = node;
        const id = surface.addElement(CanvasElementType.SHAPE, {
            ...DEFAULT_SHAPE_PROPS,
            xywh: `[${0},${0},${0},${0}]`,
            text: new Workspace.Y.Text(text),
        });
        shapeIds.push(id);
        const ele = surface.pickById(id);
        const maxWidth = Math.max(...text.split('\n').map(line => getLineWidth(line, ele.font))) +
            SHAPE_TEXT_PADDING * 2;
        const bound = normalizeShapeBound(ele, new Bound(0, 0, Math.min(600, maxWidth), 0));
        return {
            id,
            width: bound.w,
            height: bound.h,
            children: children.map(child => {
                const layoutNode = drawNode(child);
                connectors.push({ from: id, to: layoutNode.id });
                return layoutNode;
            }),
        };
    };
    const layoutNode = drawNode(node);
    const result = layout.leftRight(layoutNode, {
        gapHorizontal: 130,
        gapVertical: 10,
    });
    const updatePosition = (node, result) => {
        const { id, width, height } = node;
        const { x, y } = result.self;
        surface.updateElement(id, {
            xywh: `[${x},${y},${width},${height}]`,
        });
        node.children.forEach((child, index) => {
            const layoutNodeResult = result.children[index];
            updatePosition(child, layoutNodeResult);
            if (layoutNodeResult.connector) {
                const direction = directionMap[layoutNodeResult.connector.direction];
                const connectorId = surface.addElement(CanvasElementType.CONNECTOR, {
                    ...DEFAULT_CONNECTOR_PROPS,
                    source: {
                        id: id,
                        position: direction.from,
                    },
                    target: {
                        id: child.id,
                        position: direction.to,
                    },
                });
                connectorIds.push(connectorId);
            }
        });
    };
    updatePosition(layoutNode, result);
    return { shapeIds, connectorIds };
};
export function drawMindMap(surfaceElement, mindMap) {
    const { shapeIds, connectorIds } = drawAllNode(mindMap, surfaceElement);
    const { edgeless } = surfaceElement;
    edgeless.selectionManager.set({
        elements: [...shapeIds, ...connectorIds],
        editing: false,
    });
    surfaceElement.group.createGroupOnSelected();
}
//# sourceMappingURL=draw.js.map