import { existsSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { APP_NAME } from "../config.js";
export function getManifestFromPackageJson(pkg) {
    const appManifest = pkg[APP_NAME];
    if (appManifest && typeof appManifest === "object" && !Array.isArray(appManifest)) {
        return appManifest;
    }
    const legacyManifest = pkg.pi;
    if (legacyManifest && typeof legacyManifest === "object" && !Array.isArray(legacyManifest)) {
        return legacyManifest;
    }
    return null;
}
export function readPiManifestFile(packageJsonPath) {
    try {
        const content = readFileSync(packageJsonPath, "utf-8");
        const pkg = JSON.parse(content);
        return getManifestFromPackageJson(pkg);
    }
    catch {
        return null;
    }
}
export function readPiManifest(packageRoot) {
    const packageJsonPath = join(packageRoot, "package.json");
    if (!existsSync(packageJsonPath)) {
        return null;
    }
    return readPiManifestFile(packageJsonPath);
}
export function conventionDirsForResource(packageRoot, resourceType) {
    if (resourceType === "workflows") {
        return [join(packageRoot, "workflows"), join(packageRoot, "workflow")];
    }
    return [join(packageRoot, resourceType)];
}
export function manifestEntriesForResource(manifest, resourceType) {
    if (!manifest)
        return undefined;
    if (resourceType === "workflows")
        return manifest.workflows ?? manifest.workflow;
    return manifest[resourceType];
}
//# sourceMappingURL=package-manager-manifest.js.map