import type { DriveFileInfo } from "@/lib/api/types" import { normalizeDriveFolderPath } from "@/lib/drive/drive-sidebar-tree" export type DriveMoveSource = Pick export function parentFolderPath(itemPath: string): string { const normalized = itemPath.replace(/\/+$/, "") const idx = normalized.lastIndexOf("/") if (idx <= 0) return "/" return normalized.slice(0, idx) || "/" } export function buildMoveDestination(destFolderPath: string, itemName: string): string { const dest = normalizeDriveFolderPath(destFolderPath) return dest === "/" ? `/${itemName}` : `${dest}/${itemName}`.replace(/\/+/g, "/") } export function isMoveDestinationBlocked( sources: Pick[], destFolderPath: string ): boolean { const dest = normalizeDriveFolderPath(destFolderPath) return sources.some((source) => { if (source.type !== "directory") return false const src = normalizeDriveFolderPath(source.path) return dest === src || dest.startsWith(`${src}/`) }) } export function isMoveToSameFolder(sources: DriveMoveSource[], destFolderPath: string): boolean { const dest = normalizeDriveFolderPath(destFolderPath) return sources.every((source) => parentFolderPath(source.path) === dest) } export async function copyDriveItemsToFolder( sources: DriveMoveSource[], destFolderPath: string, copy: (body: { source: string; destination: string }) => Promise ): Promise { for (const item of sources) { const destination = buildMoveDestination(destFolderPath, item.name) await copy({ source: item.path, destination }) } } export async function moveDriveItemsToFolder( sources: DriveMoveSource[], destFolderPath: string, move: (body: { source: string; destination: string }) => Promise ): Promise { for (const item of sources) { const destination = buildMoveDestination(destFolderPath, item.name) await move({ source: item.path, destination }) } }