export type Node = {
	id: string;
	user_id: string;
	x: number;
	y: number;
	width: number;
	height: number;
	name: string;
	icon: string;
	// 1 disables editing
	permission: number;
	visible: boolean;
	endable: boolean;
};

export type LinkNode = {
	node: Node;
	x: number;
	y: number;
};

export type FullLink = {
	sourceNode: LinkNode;
	targetNode: LinkNode;
	bi?: boolean;
};

export type FullLinkApi = {
	id: string;
	user_id: string;
	target_node: string | null;
	source_node: string | null;
	bi: boolean;
	source_x: number;
	source_y: number;

	target_x: number;
	target_y: number;
};

export type ActionType = undefined | 'drag' | 'create' | 'move' | 'link';

export function extractLinkNodePosX(
	canvasX: (x: number) => number,
	node: LinkNode,
	grid_size: number
) {
	if (node.x === -1) {
		return canvasX(node.node.x);
	}
	if (node.x === node.node.width) {
		return canvasX(node.node.x) + node.node.width * grid_size;
	}
	return canvasX(node.node.x) + node.x * grid_size + grid_size / 2;
}

export function extractLinkNodePosY(
	canvasY: (x: number) => number,
	node: LinkNode,
	grid_size: number
) {
	if (node.y === -1) {
		return canvasY(node.node.y);
	}
	if (node.y === node.node.height) {
		return canvasY(node.node.y) + node.node.height * grid_size;
	}
	return canvasY(node.node.y) + node.y * grid_size + grid_size / 2;
}
