package com.correx.apps.server

import java.nio.file.Path

internal data class BootWorkspace(
    val workspaceRoot: Path,
    val workingDir: Path,
    val workingDirWasClamped: Boolean,
)

internal fun resolveBootWorkspace(
    explicitWorkspaceRoot: Path?,
    explicitWorkingDir: Path?,
    processWorkingDir: Path,
): BootWorkspace {
    val workspaceRoot = (explicitWorkspaceRoot ?: explicitWorkingDir ?: processWorkingDir)
        .toAbsolutePath()
        .normalize()
    val requestedWorkingDir = (explicitWorkingDir ?: workspaceRoot)
        .toAbsolutePath()
        .normalize()
    val workingDirIsContained = requestedWorkingDir.startsWith(workspaceRoot)

    return BootWorkspace(
        workspaceRoot = workspaceRoot,
        workingDir = requestedWorkingDir.takeIf { workingDirIsContained } ?: workspaceRoot,
        workingDirWasClamped = !workingDirIsContained,
    )
}
