import { ConfigService } from "../../../configService/index.mjs";
import { useHandleAxiosSnackbar } from "../../../utilities/apiHelpers.mjs";
import { apiMutate } from "../../../utilities/useAxiosMutate.mjs";
import PrimaryActionButton from "../../../UI/inputs/buttons/PrimaryActionButton.mjs";
import { FluentDialog } from "../../../UI/feedback/FluentDialog.mjs";
import { AmbientGridTemplate } from "../../../UI/templates/deprecated/AmbientGridTemplate/AmbientGridTemplate.mjs";
import { ActionsRenderer } from "../../../UI/templates/ui/renderers/ActionsRenderer.mjs";
import UdpVerticalManagementSidesheet from "../UdpVerticalManagementSidesheet/UdpVerticalManagementSidesheet.mjs";
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { Typography } from "@material-ui/core";
import { AddIcon, DeleteIcon, EditIcon } from "@fluentui/react-icons";
import { useTranslation } from "react-i18next";
//#region src/udp/pages/UdpVerticalManagementPage/UdpVerticalManagementPage.jsx
const UdpVerticalManagementPage = (props) => {
	const { params } = props;
	const { nodeObj: environment, inquiryNodeProperties } = params;
	const { t } = useTranslation();
	const [sideSheetOpen, setSideSheetOpen] = useState(false);
	const [sideSheetEdit, setSideSheetEdit] = useState(false);
	const [loading, setLoading] = useState(false);
	const [selectedVertical, setSelectedVertical] = useState(null);
	const [verticals, setVerticals] = useState([]);
	const [productVertical, setProductVertical] = useState([]);
	const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
	const selectedVerticalRef = useRef();
	selectedVerticalRef.current = selectedVertical ? selectedVertical : null;
	const { handleErrorSnackbar, handleSuccessSnackbar } = useHandleAxiosSnackbar();
	const openSheetAsEdit = useCallback((isEdit) => {
		setSideSheetOpen(true);
		setSideSheetEdit(isEdit);
	}, []);
	const closeDeleteDialog = useCallback(() => {
		setDeleteDialogOpen(false);
	}, [setDeleteDialogOpen]);
	const executeMutation = useCallback(async (endpoint, method, data) => apiMutate(ConfigService.uemV1ApiUrl, endpoint, { method: method || "post" }, data), []);
	const getVerticals = useCallback(async (data_0, isFirstLoad = false) => {
		if (isFirstLoad) setLoading(true);
		const response = await executeMutation("uem", "post", data_0);
		if (response?.status === 200) setVerticals(response?.data);
		if (isFirstLoad) setLoading(false);
	}, [executeMutation]);
	const deleteVertical = useCallback(async () => {
		const data_1 = { data: {
			environmentId: environment.environmentId,
			apiPath: `TenantService/api/v2/Vertical/${selectedVerticalRef.current.verticalId}/uemDelete`
		} };
		if ((await executeMutation(`vertical/${selectedVerticalRef.current.verticalId}/uemDelete`, "delete", data_1))?.status === 204) {
			console.log("success");
			handleSuccessSnackbar("Vertical Deleted Successfully");
		}
		getVerticals({ data: {
			customerId: null,
			environmentId: environment.environmentId,
			apiPath: `TenantService/api/v2/Vertical`
		} }, true);
		closeDeleteDialog();
	}, [
		executeMutation,
		selectedVertical,
		closeDeleteDialog
	]);
	const getProductVerticals = useCallback(async (data_2) => {
		const response_1 = await executeMutation("productVertical", "get", data_2);
		if (response_1?.status === 200) setProductVertical(response_1?.data);
	}, [executeMutation]);
	useEffect(() => {
		if (environment && getVerticals) {
			if (environment.environmentId) {
				getProductVerticals({ data: { apiPath: `api/v2/ProductVertical` } });
				getVerticals({ data: {
					customerId: null,
					environmentId: environment.environmentId,
					apiPath: `TenantService/api/v2/Vertical`
				} }, true);
			}
		}
	}, [
		environment,
		getVerticals,
		getProductVerticals,
		deleteVertical
	]);
	const closeSheet = useCallback(() => {
		setSideSheetOpen(false);
		getVerticals({ data: {
			customerId: null,
			environmentId: environment.environmentId,
			apiPath: `TenantService/api/v2/Vertical`
		} });
	}, [getVerticals, environment]);
	const handleEditPage = useCallback((value) => {
		setSelectedVertical(value);
		console.log("Edit page, value: ", value);
		openSheetAsEdit(true);
	}, [t]);
	const handleCreateVertical = useCallback((value_0) => {
		openSheetAsEdit(false);
	}, [t]);
	const handleDelete = useCallback((value_1) => {
		setSelectedVertical(value_1);
		console.log("Delete page, value: ", value_1);
		setDeleteDialogOpen(true);
	}, [t]);
	const actionList = useMemo(() => [{
		id: 1,
		title: t("Edit"),
		icon: EditIcon,
		onClick: handleEditPage,
		disabled: false
	}, {
		id: 2,
		title: t("Delete"),
		icon: DeleteIcon,
		onClick: handleDelete,
		disabled: false
	}], [
		t,
		handleEditPage,
		handleDelete
	]);
	const gridOptions = useMemo(() => ({
		frameworkComponents: { actionsRenderer: ActionsRenderer },
		columnDefs: [
			{
				headerName: t("Name"),
				field: "name",
				minWidth: 300
			},
			{
				headerName: t("Icon"),
				field: "icon"
			},
			{
				headerName: t("Actions"),
				cellRenderer: "actionsRenderer",
				cellRendererParams: { actionList },
				pinned: "right",
				suppressMenu: true,
				sortable: false
			}
		]
	}), [
		t,
		actionList,
		productVertical
	]);
	const pab = /* @__PURE__ */ React.createElement(PrimaryActionButton, { onClick: handleCreateVertical }, /* @__PURE__ */ React.createElement(Typography, { style: { marginRight: 5 } }, "Add"), /* @__PURE__ */ React.createElement(AddIcon, null));
	return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(UdpVerticalManagementSidesheet, {
		open: sideSheetOpen,
		closeSidesheet: closeSheet,
		selectedVertical,
		environment,
		title: t(sideSheetEdit ? "Edit Vertical" : "Create Vertical"),
		associatedProducts: productVertical,
		isEdit: sideSheetEdit
	}), /* @__PURE__ */ React.createElement(AmbientGridTemplate, {
		title: t("Vertical Manager"),
		primaryActionButton: pab,
		data: verticals,
		loading,
		gridOptions,
		groupingData: []
	}), /* @__PURE__ */ React.createElement(FluentDialog, {
		open: deleteDialogOpen,
		handleClose: closeDeleteDialog,
		title: t("Delete Vertical"),
		message: t("Are you sure you want to Delete") + ` "${selectedVertical?.name}"?`,
		actionOne: deleteVertical,
		labelOne: t("Delete"),
		actionTwo: closeDeleteDialog,
		labelTwo: t("Cancel")
	}));
};
//#endregion
export { UdpVerticalManagementPage as default };

//# sourceMappingURL=UdpVerticalManagementPage.mjs.map