import { localizationKeys } from "../localization/localizationKeys.js";
import { ORGANIZATION_PROFILE_NAVBAR_ROUTE_ID, USER_PROFILE_NAVBAR_ROUTE_ID } from "../constants.js";
import code_default from "../icons/code.js";
import credit_card_default from "../icons/credit-card.js";
import organization_default from "../icons/organization.js";
import tick_shield_default from "../icons/tick-shield.js";
import user_default from "../icons/user.js";
import users_default from "../icons/users.js";
import { ExternalElementMounter } from "./ExternalElementMounter.js";
import { isDevelopmentSDK } from "./runtimeEnvironment.js";
import { sanitizeCustomLinkURL } from "./sanitizeCustomLinkURL.js";
import { jsx } from "@emotion/react/jsx-runtime";
import { isValidUrl } from "@clerk/shared/internal/clerk-js/url";
import { disabledOrganizationAPIKeysFeature, disabledOrganizationBillingFeature, disabledUserAPIKeysFeature, disabledUserBillingFeature } from "@clerk/shared/internal/clerk-js/componentGuards";

//#region src/utils/createCustomPages.tsx
const createUserProfileCustomPages = (customPages, clerk, shouldShowBilling, environment) => {
	return createCustomPages({
		customPages,
		getDefaultRoutes: getUserProfileDefaultRoutes,
		setFirstPathToRoot: setFirstPathToUserProfileRoot,
		excludedPathsFromDuplicateWarning: []
	}, clerk, shouldShowBilling, environment);
};
const createOrganizationProfileCustomPages = (customPages, clerk, shouldShowBilling, environment) => {
	return createCustomPages({
		customPages,
		getDefaultRoutes: getOrganizationProfileDefaultRoutes,
		setFirstPathToRoot: setFirstPathToOrganizationProfileRoot,
		excludedPathsFromDuplicateWarning: []
	}, clerk, shouldShowBilling, environment, true);
};
const createCustomPages = ({ customPages, getDefaultRoutes, setFirstPathToRoot, excludedPathsFromDuplicateWarning }, clerk, shouldShowBilling, environment, organization) => {
	const { INITIAL_ROUTES, pageToRootNavbarRouteMap, validReorderItemLabels } = getDefaultRoutes({
		commerce: organization ? !disabledOrganizationBillingFeature(clerk, environment) && shouldShowBilling : !disabledUserBillingFeature(clerk, environment) && shouldShowBilling,
		apiKeys: organization ? !disabledOrganizationAPIKeysFeature(clerk, environment) : !disabledUserAPIKeysFeature(clerk, environment)
	});
	if (isDevelopmentSDK(clerk)) checkForDuplicateUsageOfReorderingItems(customPages, validReorderItemLabels);
	const { allRoutes, contents } = getRoutesAndContents({
		customPages: customPages.filter((cp) => {
			if (!isValidPageItem(cp, validReorderItemLabels)) {
				if (isDevelopmentSDK(clerk)) console.error("Clerk: Invalid custom page data: ", cp);
				return false;
			}
			return true;
		}),
		defaultRoutes: INITIAL_ROUTES
	});
	assertExternalLinkAsRoot(allRoutes);
	const routes = setFirstPathToRoot(allRoutes);
	if (isDevelopmentSDK(clerk)) warnForDuplicatePaths(routes, excludedPathsFromDuplicateWarning);
	return {
		routes,
		contents,
		pageToRootNavbarRouteMap
	};
};
const getRoutesAndContents = ({ customPages, defaultRoutes }) => {
	let remainingDefaultRoutes = defaultRoutes.map((r) => r);
	const contents = [];
	const routesWithoutDefaults = customPages.map((cp, index) => {
		if (isCustomLink(cp)) return {
			name: cp.label,
			id: `custom-page-${index}`,
			icon: (props) => /* @__PURE__ */ jsx(ExternalElementMounter, {
				mount: cp.mountIcon,
				unmount: cp.unmountIcon,
				...props
			}),
			path: sanitizeCustomLinkURL(cp.url),
			external: true
		};
		if (isCustomPage(cp)) {
			const pageURL = sanitizeCustomPageURL(cp.url);
			contents.push({
				url: pageURL,
				mount: cp.mount,
				unmount: cp.unmount
			});
			return {
				name: cp.label,
				id: `custom-page-${index}`,
				icon: (props) => /* @__PURE__ */ jsx(ExternalElementMounter, {
					mount: cp.mountIcon,
					unmount: cp.unmountIcon,
					...props
				}),
				path: pageURL
			};
		}
		const reorderItem = defaultRoutes.find((r) => r.id === cp.label);
		remainingDefaultRoutes = remainingDefaultRoutes.filter(({ id }) => id !== cp.label);
		return { ...reorderItem };
	});
	return {
		allRoutes: [...remainingDefaultRoutes, ...routesWithoutDefaults],
		contents
	};
};
const setFirstPathToUserProfileRoot = (routes) => {
	return routes.map((r, index) => index === 0 ? {
		...r,
		path: "/"
	} : r);
};
const setFirstPathToOrganizationProfileRoot = (routes) => {
	return routes.map((r, index) => index === 0 ? {
		...r,
		path: "/"
	} : r);
};
const checkForDuplicateUsageOfReorderingItems = (customPages, validReorderItems) => {
	customPages.filter((cp) => isReorderItem(cp, validReorderItems)).reduce((acc, cp) => {
		if (acc.includes(cp.label)) console.error(`Clerk: The "${cp.label}" item is used more than once when reordering pages. This may cause unexpected behavior.`);
		return [...acc, cp.label];
	}, []);
};
const warnForDuplicatePaths = (routes, pathsToFilter) => {
	const paths = routes.filter(({ external, path }) => !external && pathsToFilter.every((p) => p !== path)).map(({ path }) => path);
	paths.filter((p, index) => paths.indexOf(p) !== index).forEach((p) => {
		console.error(`Clerk: Duplicate path "${p}" found in custom pages. This may cause unexpected behavior.`);
	});
};
const isValidPageItem = (cp, validReorderItems) => {
	return isCustomPage(cp) || isCustomLink(cp) || isReorderItem(cp, validReorderItems);
};
const isCustomPage = (cp) => {
	return !!cp.url && !!cp.label && !!cp.mount && !!cp.unmount && !!cp.mountIcon && !!cp.unmountIcon;
};
const isCustomLink = (cp) => {
	return !!cp.url && !!cp.label && !cp.mount && !cp.unmount && !!cp.mountIcon && !!cp.unmountIcon;
};
const isReorderItem = (cp, validItems) => {
	return !cp.url && !cp.mount && !cp.unmount && !cp.mountIcon && !cp.unmountIcon && validItems.some((v) => v === cp.label);
};
const sanitizeCustomPageURL = (url) => {
	if (!url) throw new Error("Clerk: URL is required for custom pages");
	if (isValidUrl(url)) throw new Error("Clerk: Absolute URLs are not supported for custom pages");
	return url.charAt(0) === "/" && url.length > 1 ? url.substring(1) : url;
};
const assertExternalLinkAsRoot = (routes) => {
	if (routes[0].external) throw new Error("Clerk: The first route cannot be a custom external link component");
};
const getUserProfileDefaultRoutes = ({ commerce, apiKeys }) => {
	const INITIAL_ROUTES = [{
		name: localizationKeys("userProfile.navbar.account"),
		id: USER_PROFILE_NAVBAR_ROUTE_ID.ACCOUNT,
		icon: user_default,
		path: "account"
	}, {
		name: localizationKeys("userProfile.navbar.security"),
		id: USER_PROFILE_NAVBAR_ROUTE_ID.SECURITY,
		icon: tick_shield_default,
		path: "security"
	}];
	if (commerce) INITIAL_ROUTES.push({
		name: localizationKeys("userProfile.navbar.billing"),
		id: USER_PROFILE_NAVBAR_ROUTE_ID.BILLING,
		icon: credit_card_default,
		path: "billing"
	});
	if (apiKeys) INITIAL_ROUTES.push({
		name: localizationKeys("userProfile.navbar.apiKeys"),
		id: USER_PROFILE_NAVBAR_ROUTE_ID.API_KEYS,
		icon: code_default,
		path: "api-keys"
	});
	return {
		INITIAL_ROUTES,
		pageToRootNavbarRouteMap: {
			profile: INITIAL_ROUTES.find((r) => r.id === USER_PROFILE_NAVBAR_ROUTE_ID.ACCOUNT),
			"email-address": INITIAL_ROUTES.find((r) => r.id === USER_PROFILE_NAVBAR_ROUTE_ID.ACCOUNT),
			"phone-number": INITIAL_ROUTES.find((r) => r.id === USER_PROFILE_NAVBAR_ROUTE_ID.ACCOUNT),
			"connected-account": INITIAL_ROUTES.find((r) => r.id === USER_PROFILE_NAVBAR_ROUTE_ID.ACCOUNT),
			"web3-wallet": INITIAL_ROUTES.find((r) => r.id === USER_PROFILE_NAVBAR_ROUTE_ID.ACCOUNT),
			username: INITIAL_ROUTES.find((r) => r.id === USER_PROFILE_NAVBAR_ROUTE_ID.ACCOUNT),
			"multi-factor": INITIAL_ROUTES.find((r) => r.id === USER_PROFILE_NAVBAR_ROUTE_ID.SECURITY),
			password: INITIAL_ROUTES.find((r) => r.id === USER_PROFILE_NAVBAR_ROUTE_ID.SECURITY)
		},
		validReorderItemLabels: INITIAL_ROUTES.map((r) => r.id)
	};
};
const getOrganizationProfileDefaultRoutes = ({ commerce, apiKeys }) => {
	const INITIAL_ROUTES = [{
		name: localizationKeys("organizationProfile.navbar.general"),
		id: ORGANIZATION_PROFILE_NAVBAR_ROUTE_ID.GENERAL,
		icon: organization_default,
		path: "organization-general"
	}, {
		name: localizationKeys("organizationProfile.navbar.members"),
		id: ORGANIZATION_PROFILE_NAVBAR_ROUTE_ID.MEMBERS,
		icon: users_default,
		path: "organization-members"
	}];
	if (commerce) INITIAL_ROUTES.push({
		name: localizationKeys("organizationProfile.navbar.billing"),
		id: USER_PROFILE_NAVBAR_ROUTE_ID.BILLING,
		icon: credit_card_default,
		path: "organization-billing"
	});
	if (apiKeys) INITIAL_ROUTES.push({
		name: localizationKeys("organizationProfile.navbar.apiKeys"),
		id: ORGANIZATION_PROFILE_NAVBAR_ROUTE_ID.API_KEYS,
		icon: code_default,
		path: "organization-api-keys"
	});
	return {
		INITIAL_ROUTES,
		pageToRootNavbarRouteMap: {
			"invite-members": INITIAL_ROUTES.find((r) => r.id === ORGANIZATION_PROFILE_NAVBAR_ROUTE_ID.MEMBERS),
			domain: INITIAL_ROUTES.find((r) => r.id === ORGANIZATION_PROFILE_NAVBAR_ROUTE_ID.GENERAL),
			profile: INITIAL_ROUTES.find((r) => r.id === ORGANIZATION_PROFILE_NAVBAR_ROUTE_ID.GENERAL),
			leave: INITIAL_ROUTES.find((r) => r.id === ORGANIZATION_PROFILE_NAVBAR_ROUTE_ID.GENERAL),
			delete: INITIAL_ROUTES.find((r) => r.id === ORGANIZATION_PROFILE_NAVBAR_ROUTE_ID.GENERAL)
		},
		validReorderItemLabels: INITIAL_ROUTES.map((r) => r.id)
	};
};

//#endregion
export { createOrganizationProfileCustomPages, createUserProfileCustomPages };
//# sourceMappingURL=createCustomPages.js.map