import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
import { useState } from "react";
import { object, record, string } from "zod";
import { Disclosure } from "../../../disclosure/disclosure.js";
const PH_DEPENDENCIES = [
    /^@powerhousedao\/.+$/,
    "document-drive",
    "document-model",
];
const PackageJsonSchema = object({
    version: string({ message: "Missing version field in package.json" }),
    dependencies: record(string(), string()).nullable(),
    devDependencies: record(string(), string()).nullable(),
})
    .refine((data) => data.dependencies != null || data.devDependencies != null, "package.json must have either dependencies or devDependencies")
    .transform((data) => {
    const allDependencies = {
        ...data.dependencies,
        ...data.devDependencies,
    };
    return {
        version: data.version,
        dependencies: Object.fromEntries(Object.entries(allDependencies).filter(([key]) => PH_DEPENDENCIES.some((regexOrName) => typeof regexOrName === "string"
            ? regexOrName === key
            : regexOrName.test(key)))),
    };
});
export function verifyPackageJsonFields(packageJson) {
    const parsed = PackageJsonSchema.safeParse(packageJson);
    if (!parsed.success) {
        console.error("Package.json validation failed:", parsed.error.format());
        return false;
    }
    return parsed.data;
}
export function DependencyVersions(props) {
    const [isOpen, setIsOpen] = useState(false);
    const { packageJson, phCliVersion } = props;
    const validatedData = verifyPackageJsonFields(packageJson);
    if (!validatedData) {
        console.error("Failed to validate package.json data");
        return null;
    }
    return (_jsx(Disclosure, { isOpen: isOpen, onOpenChange: () => setIsOpen(!isOpen), title: `App version: ${validatedData.version}`, toggleClassName: "text-gray-900 text-sm", children: _jsxs("ul", { className: "text-sm text-gray-600", children: [Object.entries(validatedData.dependencies).map(([dep, version]) => (_jsxs("li", { className: "my-1 flex justify-between pr-1", children: [_jsxs("span", { children: [dep.replace("@powerhousedao/", ""), ":"] }), _jsx("span", { className: "font-normal", children: version })] }, dep))), phCliVersion && (_jsxs("li", { className: "my-1 flex justify-between pr-1", children: [_jsx("span", { children: "@powerhousedao/ph-cli:" }), _jsx("span", { className: "font-normal", children: phCliVersion })] }, "ph-cli"))] }) }));
}
//# sourceMappingURL=dependency-versions.js.map