import { useRouter } from "../../router/RouteContext.js";
import { useEnvironment } from "../EnvironmentContext.js";
import { ORGANIZATION_PROFILE_NAVBAR_ROUTE_ID } from "../../constants.js";
import { createOrganizationProfileCustomPages } from "../../utils/createCustomPages.js";
import { useStatements, useSubscription } from "./Plans.js";
import { createContext, useContext, useMemo } from "react";
import { useClerk } from "@clerk/shared/react";

//#region src/contexts/components/OrganizationProfile.ts
const OrganizationProfileContext = createContext(null);
const useOrganizationProfileContext = () => {
	const context = useContext(OrganizationProfileContext);
	const { navigate } = useRouter();
	const environment = useEnvironment();
	const clerk = useClerk();
	if (!context || context.componentName !== "OrganizationProfile") throw new Error("Clerk: useOrganizationProfileContext called outside OrganizationProfile.");
	const { componentName, customPages,...ctx } = context;
	const subscription = useSubscription();
	const statements = useStatements();
	const hasNonFreeSubscription = subscription.data?.subscriptionItems.some((item) => item.plan.hasBaseFee);
	const shouldShowBilling = environment.commerceSettings.billing.organization.hasPaidPlans || hasNonFreeSubscription || Boolean(statements.data.length > 0);
	const pages = useMemo(() => createOrganizationProfileCustomPages(customPages || [], clerk, shouldShowBilling, environment), [customPages, shouldShowBilling]);
	const navigateAfterLeaveOrganization = () => navigate(ctx.afterLeaveOrganizationUrl || environment.displayConfig.afterLeaveOrganizationUrl);
	const isMembersPageRoot = pages.routes[0].id === ORGANIZATION_PROFILE_NAVBAR_ROUTE_ID.MEMBERS;
	const isGeneralPageRoot = pages.routes[0].id === ORGANIZATION_PROFILE_NAVBAR_ROUTE_ID.GENERAL;
	const isBillingPageRoot = pages.routes[0].id === ORGANIZATION_PROFILE_NAVBAR_ROUTE_ID.BILLING;
	const isAPIKeysPageRoot = pages.routes[0].id === ORGANIZATION_PROFILE_NAVBAR_ROUTE_ID.API_KEYS;
	const navigateToGeneralPageRoot = () => navigate(isGeneralPageRoot ? "../" : isMembersPageRoot ? "./organization-general" : "../organization-general");
	return {
		...ctx,
		pages,
		navigateAfterLeaveOrganization,
		componentName,
		navigateToGeneralPageRoot,
		isMembersPageRoot,
		isGeneralPageRoot,
		isBillingPageRoot,
		isAPIKeysPageRoot,
		shouldShowBilling
	};
};

//#endregion
export { OrganizationProfileContext, useOrganizationProfileContext };
//# sourceMappingURL=OrganizationProfile.js.map