import type { CanvasGenerationDialogState, CanvasLayer } from './types'; export const CANVAS_GENERATION_SELECTION_PREFIX = 'generation-dialog:'; export function getCanvasGenerationSelectionId(dialogId: string) { return `${CANVAS_GENERATION_SELECTION_PREFIX}${dialogId}`; } export function getCanvasGenerationDialogIdFromSelectionId(targetId: string) { return targetId.startsWith(CANVAS_GENERATION_SELECTION_PREFIX) ? targetId.slice(CANVAS_GENERATION_SELECTION_PREFIX.length) : null; } export function isCanvasGenerationSelectionId(targetId: string) { return getCanvasGenerationDialogIdFromSelectionId(targetId) !== null; } export function getSelectedLayerIds(selectionIds: string[]) { return selectionIds.filter( (selectionId) => !isCanvasGenerationSelectionId(selectionId), ); } export function getSelectedGenerationDialogIds(selectionIds: string[]) { return selectionIds.flatMap((selectionId) => { const dialogId = getCanvasGenerationDialogIdFromSelectionId(selectionId); return dialogId ? [dialogId] : []; }); } export function dedupeSelectionIds(selectionIds: string[]) { return Array.from(new Set(selectionIds)); } export function firstSelectedLayerId(selectionIds: string[]) { return getSelectedLayerIds(selectionIds)[0] ?? null; } export function normalizeCanvasSelectionIds({ selectionIds, layers, generationDialogs, }: { selectionIds: string[]; layers: CanvasLayer[]; generationDialogs: CanvasGenerationDialogState[]; }) { const layerIds = new Set(layers.map((layer) => layer.id)); const generationDialogIds = new Set( generationDialogs.map((dialog) => dialog.id), ); return dedupeSelectionIds(selectionIds).filter((selectionId) => { if (isCanvasGenerationSelectionId(selectionId)) { const dialogId = getCanvasGenerationDialogIdFromSelectionId(selectionId); return Boolean(dialogId && generationDialogIds.has(dialogId)); } return layerIds.has(selectionId); }); } export function resolvePointerSelection({ targetId, selectedIds, isMultiSelectGesture, }: { targetId: string; selectedIds: string[]; isMultiSelectGesture: boolean; }) { if (!isMultiSelectGesture) { if (selectedIds.includes(targetId)) { return selectedIds; } return [targetId]; } if (!selectedIds.includes(targetId)) { return [...selectedIds, targetId]; } if (selectedIds.length <= 1) { return [targetId]; } return selectedIds.filter((selectedId) => selectedId !== targetId); }