namespace SirayaPrinter.Sync;

public static class PathMapper
{
    public static string InstallRoot { get; } =
        Path.Combine(
            Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
            "Siraya",
            "Printer");

    public static string VersionsDir => Path.Combine(InstallRoot, "versions");

    public static string StageDir =>
        Path.Combine(Path.GetTempPath(), "SirayaPrinter", "stage");

    /// <summary>Maps manifest path (e.g. app/index.js) to install dir path.</summary>
    public static string ToInstallPath(string manifestPath)
    {
        var normalized = manifestPath.Replace('/', Path.DirectorySeparatorChar);
        if (normalized.StartsWith("app" + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase))
        {
            var rest = normalized["app".Length..].TrimStart(Path.DirectorySeparatorChar);
            return Path.Combine(InstallRoot, rest);
        }
        if (normalized.StartsWith("runtime" + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase))
        {
            return Path.Combine(InstallRoot, normalized);
        }
        return Path.Combine(InstallRoot, normalized);
    }

    public static string ToStagePath(string manifestPath)
    {
        var normalized = manifestPath.Replace('/', Path.DirectorySeparatorChar);
        return Path.Combine(StageDir, normalized);
    }
}
