import { clsx, type ClassValue } from 'clsx'; import type { BoundingBox } from 'libmojangles'; import { twMerge } from 'tailwind-merge'; export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)); } // eslint-disable-next-line @typescript-eslint/no-explicit-any export type WithoutChild = T extends { child?: any } ? Omit : T; // eslint-disable-next-line @typescript-eslint/no-explicit-any export type WithoutChildren = T extends { children?: any } ? Omit : T; export type WithoutChildrenOrChild = WithoutChildren>; export type WithElementRef = T & { ref?: U | null }; export function boundingBoxOfBoxes(bboxes: BoundingBox[]): BoundingBox { if (bboxes.length === 0) { throw new Error('Cannot compute bounding box of empty list'); } return { min: [Math.min(...bboxes.map((b) => b.min[0])), Math.min(...bboxes.map((b) => b.min[1]))], max: [Math.max(...bboxes.map((b) => b.max[0])), Math.max(...bboxes.map((b) => b.max[1]))] }; } export type AnchorPosition = | 'top-left' | 'top-center' | 'top-right' | 'middle-left' | 'center' | 'middle-right' | 'bottom-left' | 'bottom-center' | 'bottom-right'; export const ANCHOR_POSITIONS: AnchorPosition[] = [ 'top-left', 'top-center', 'top-right', 'middle-left', 'center', 'middle-right', 'bottom-left', 'bottom-center', 'bottom-right' ]; export function alignForAnchor(anchor: string): 'left' | 'right' | 'center' { if (anchor.endsWith('-left')) return 'left'; if (anchor.endsWith('-right')) return 'right'; return 'center'; } export function applyAnchorToElement(element: Element, anchor: string) { element.setAttribute('anchor', anchor); element.setAttribute('x', '0'); element.setAttribute('y', anchor.startsWith('bottom-') ? '-18' : '0'); element.setAttribute('align', alignForAnchor(anchor)); } export function getElementPath(element: Element): string { const indices: number[] = []; let current: Element | null = element; while (current && current.parentElement) { const parent: Element = current.parentElement; if (parent.tagName === 'hud') break; const index = Array.from(parent.children).indexOf(current); indices.unshift(index); current = parent; } return indices.join('/'); } export function resolveElementPath(doc: Document, path: string): Element | null { const hud = doc.querySelector('hud'); if (!hud) return null; const indices = path.split('/').map(Number); let current: Element = hud; for (const idx of indices) { const child = current.children[idx]; if (!child) return null; current = child; } return current; }