import { ConfigService } from "../../configService/index.mjs";
import { useUser } from "../../utilities/auth/useUser.mjs";
import { apiMutate } from "../../utilities/useAxiosMutate.mjs";
import { useActions } from "../../hooks/useActions.mjs";
import React, { Suspense, lazy, useCallback, useEffect, useRef, useState } from "react";
import { IconButton, List, ListItem, ListItemIcon, ListItemText, Popover, Typography, makeStyles } from "@material-ui/core";
import { useTranslation } from "react-i18next";
import { Chat, Close, HelpOutline } from "@material-ui/icons";
//#region src/shell/ui/HelpCenter.jsx
const UserFormSideSheet = lazy(() => import("./user/UserFormSideSheet.mjs"));
const SupportCaseSideSheet = lazy(() => import("../../UI/support/SupportCaseSideSheet.mjs"));
const preloadUserFormSideSheet = () => import("./user/UserFormSideSheet.mjs");
const preloadSupportCaseSideSheet = () => import("../../UI/support/SupportCaseSideSheet.mjs");
const useStyles = makeStyles((theme) => ({
	root: { display: "flex" },
	popover: {
		padding: theme.spacing(2),
		maxWidth: 300
	},
	icon: { minWidth: 40 },
	closeButton: {
		position: "absolute",
		right: theme.spacing(1),
		top: theme.spacing(1),
		color: theme.palette.grey[500]
	},
	header: {
		display: "flex",
		alignItems: "center",
		justifyContent: "space-between"
	},
	dialog: {
		minWidth: "80vw",
		minHeight: "80vh"
	}
}));
const HelpCenter = () => {
	const classes = useStyles();
	const { t } = useTranslation();
	const { triggerAction } = useActions();
	const user = useUser();
	const productId = Number(ConfigService.config.UNITY_PRODUCT_ID);
	const udpaiAPI = ConfigService.config.UDPAI_API_BASE;
	const udpaisearch = ConfigService.config.AI_SEARCH_URL;
	const udpaisearchkey = ConfigService.config.AI_SEARCH_KEY;
	const udpaisearchindex = ConfigService.config.AI_SEARCH_INDEX_NAME;
	const [anchorEl, setAnchorEl] = useState(null);
	const [isSupportCaseSideSheetOpen, setSupportCaseSideSheetOpen] = useState(false);
	const [isUserFormOpen, setUserFormOpen] = useState(false);
	const [supportCaseHealthOk, setSupportCaseHealthOk] = useState(false);
	const hasPreloadedRef = useRef(false);
	const handleOpenPopover = (event) => {
		setAnchorEl(event.currentTarget);
		if (!hasPreloadedRef.current) {
			hasPreloadedRef.current = true;
			try {
				preloadUserFormSideSheet();
			} catch (e) {}
			try {
				preloadSupportCaseSideSheet();
			} catch (e_0) {}
		}
	};
	const handleClosePopover = () => {
		setAnchorEl(null);
	};
	const handleLaunchAction = useCallback(() => {
		setAnchorEl(null);
		triggerAction("CEE75B36-01B0-4FAC-9AE7-C1AC8A2A194F");
	}, [triggerAction]);
	const handleOpenSupportCaseSideSheet = () => {
		setSupportCaseSideSheetOpen(true);
		setAnchorEl(null);
	};
	const handleCloseSupportCaseSideSheet = () => {
		setSupportCaseSideSheetOpen(false);
	};
	const handleOpenUserForm = () => {
		setUserFormOpen(true);
		setAnchorEl(null);
	};
	const handleCloseUserForm = () => {
		setUserFormOpen(false);
	};
	const open = Boolean(anchorEl);
	const id = open ? "help-center-popover" : void 0;
	const shouldShowAddUsers = (productId_0) => {
		const isValidProduct = [41, 53].includes(productId_0);
		const isSecurityApiConfigured = !!ConfigService.securityV1ApiUrl;
		const isValidUser = user.can.CAN_MANAGE_ROLES_USERS && user.can.CAN_MANAGE_USERS && user.can.CAN_VIEW_USERS;
		return isValidProduct && isSecurityApiConfigured && isValidUser;
	};
	const getSupportCaseStatus = useCallback(async () => apiMutate(ConfigService.productV1ApiUrl, `udpunitysupport/healthCheck`, {
		method: "get",
		headers: { currentTenantId: user?.currentTenantId }
	}), [user?.currentTenantId]);
	const getSupportCaseStatusUnity = useCallback(async () => apiMutate(ConfigService.supportV1ApiUrl, `ServiceHealth/Support`, {
		method: "get",
		headers: { currentTenantId: user?.currentTenantId }
	}), [user?.currentTenantId]);
	const showAddUsers = shouldShowAddUsers(productId);
	const shouldShowChatAssistant = [
		2,
		41,
		51,
		68
	].includes(productId) && udpaiAPI && udpaisearch && udpaisearchkey && udpaisearchindex;
	const shouldShowSupportTicket = productId && supportCaseHealthOk;
	useEffect(() => {
		const supportCaseStatus = async () => {
			try {
				if (ConfigService.config.UNITY_PRODUCT_ID === "1") setSupportCaseHealthOk((await getSupportCaseStatusUnity())?.status === 200);
				else setSupportCaseHealthOk((await getSupportCaseStatus())?.status === 200);
			} catch (error) {
				setSupportCaseHealthOk(false);
			}
		};
		supportCaseStatus();
	}, [getSupportCaseStatus, getSupportCaseStatusUnity]);
	if (!showAddUsers && !shouldShowChatAssistant && !shouldShowSupportTicket) return /* @__PURE__ */ React.createElement("div", { id: "udpRecord-HelpCenter" });
	return /* @__PURE__ */ React.createElement("div", {
		id: "udpRecord-HelpCenter",
		className: classes.root
	}, /* @__PURE__ */ React.createElement(IconButton, {
		id: "udpRecord-HelpCenter-IconButton",
		"aria-controls": id,
		"aria-haspopup": "true",
		onClick: handleOpenPopover
	}, /* @__PURE__ */ React.createElement(HelpOutline, { style: {
		color: "white",
		opacity: .8
	} })), /* @__PURE__ */ React.createElement(Popover, {
		id,
		open,
		anchorEl,
		onClose: handleClosePopover,
		anchorOrigin: {
			vertical: "bottom",
			horizontal: "center"
		},
		transformOrigin: {
			vertical: "top",
			horizontal: "center"
		}
	}, /* @__PURE__ */ React.createElement("div", { className: classes.popover }, /* @__PURE__ */ React.createElement("div", { className: classes.header }, /* @__PURE__ */ React.createElement(Typography, { variant: "h6" }, t("Help")), /* @__PURE__ */ React.createElement(IconButton, {
		size: "small",
		className: classes.closeButton,
		onClick: handleClosePopover
	}, /* @__PURE__ */ React.createElement(Close, null))), /* @__PURE__ */ React.createElement(List, {
		component: "nav",
		"aria-label": "Help options"
	}, showAddUsers && /* @__PURE__ */ React.createElement(ListItem, {
		button: true,
		id: "udpRecord-HelpCenter-AddUsers",
		onClick: handleOpenUserForm
	}, /* @__PURE__ */ React.createElement(ListItemIcon, { className: classes.icon }, /* @__PURE__ */ React.createElement(HelpOutline, null)), /* @__PURE__ */ React.createElement(ListItemText, { primary: t("Add User") })), shouldShowChatAssistant && /* @__PURE__ */ React.createElement(ListItem, {
		button: true,
		id: "udpRecord-HelpCenter-ChatAssistant",
		onClick: handleLaunchAction
	}, /* @__PURE__ */ React.createElement(ListItemIcon, { className: classes.icon }, /* @__PURE__ */ React.createElement(Chat, null)), /* @__PURE__ */ React.createElement(ListItemText, { primary: t("Chat Assistant") })), shouldShowSupportTicket && /* @__PURE__ */ React.createElement(ListItem, {
		button: true,
		id: "udpRecord-HelpCenter-SupportTicket",
		onClick: handleOpenSupportCaseSideSheet
	}, /* @__PURE__ */ React.createElement(ListItemIcon, { className: classes.icon }, /* @__PURE__ */ React.createElement(HelpOutline, null)), /* @__PURE__ */ React.createElement(ListItemText, { primary: t("Submit Support Ticket") }))))), /* @__PURE__ */ React.createElement(Suspense, { fallback: null }, /* @__PURE__ */ React.createElement(UserFormSideSheet, {
		open: isUserFormOpen,
		onClose: handleCloseUserForm
	})), /* @__PURE__ */ React.createElement(Suspense, { fallback: null }, /* @__PURE__ */ React.createElement(SupportCaseSideSheet, {
		open: isSupportCaseSideSheetOpen,
		onClose: handleCloseSupportCaseSideSheet,
		handleCloseSupportCaseSideSheet,
		onSubmit: (values) => {
			console.log("Form submitted", values);
		},
		initialValues: {}
	})));
};
//#endregion
export { HelpCenter };

//# sourceMappingURL=HelpCenter.mjs.map