import { isWindows } from './std/_util/os.ts'
const UPPERCASE_ALPHABET = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'P', 'Y', 'Z',] as const
export type UppercaseAlphabet = typeof UPPERCASE_ALPHABET[number]
export interface WindowsDeviceMapper<WasiPathPrefix extends string> { (device: UppercaseAlphabet): WasiPathPrefix}
export const DEFAULT_WINDOWS_DEVICE_MAPPER: WindowsDeviceMapper<`/mnt/${UppercaseAlphabet}`> = device => `/mnt/${device}` as const
export function fromWindowsPath<WasiAbsolutePathPrefix extends string>( path: string, getAbsolutePrefix: WindowsDeviceMapper<WasiAbsolutePathPrefix>,): string { const [letter, colon, backSlash] = path if (colon === ':' && backSlash === '\\' && UPPERCASE_ALPHABET.includes(letter as UppercaseAlphabet)) { const prefix = getAbsolutePrefix(letter as UppercaseAlphabet) const suffix = path.slice(3).replaceAll('\\', '/') return `${prefix}/${suffix}` as const } else { return path.replaceAll('\\', '/') }}
export const fromPosixPath = <Path extends string>(path: Path) => path
export const fromWindowsPosixPath = isWindows ? fromWindowsPath : fromPosixPath
interface PathConverter { (path: string, getAbsolutePrefixForWindows?: WindowsDeviceMapper<string>): string}
export const getWasiPath: PathConverter = isWindows ? (path, prefix = DEFAULT_WINDOWS_DEVICE_MAPPER) => fromWindowsPath(path, prefix) : fromPosixPath
export default getWasiPath