import { useRegistryConnections } from "@/hooks/use-registry-connections";
import { useIsRegistryEnabled } from "@/hooks/use-organization-settings";
import { type RegistrySource } from "@/hooks/use-merged-store-discovery";
import { SELF_MCP_ALIAS_ID, useProjectContext } from "@/sdk";

/**
 * Returns the list of enabled registry sources based on org settings,
 * including the private registry when the plugin is active.
 */
export function useEnabledRegistries(): RegistrySource[] {
  const registryConnections = useRegistryConnections();
  const isRegistryEnabled = useIsRegistryEnabled();
  const enabledPlugins = useProjectContext().project.enabledPlugins ?? [];

  const enabledRegistries: RegistrySource[] = registryConnections
    .filter((c) => isRegistryEnabled(c.id))
    .map((c) => ({ id: c.id, title: c.title, icon: c.icon }));

  if (
    enabledPlugins.includes("private-registry") &&
    isRegistryEnabled(SELF_MCP_ALIAS_ID)
  ) {
    enabledRegistries.push({
      id: SELF_MCP_ALIAS_ID,
      title: "Private Registry",
      icon: null,
    });
  }

  return enabledRegistries;
}
