import { ConfigService } from "../../../configService/index.mjs";
import { useHandleAxiosSnackbar } from "../../../utilities/apiHelpers.mjs";
import { apiMutate } from "../../../utilities/useAxiosMutate.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 UdpGridPanelManagementSidesheet from "../UdpGridPanelManagementSidesheet/UdpGridPanelManagementSidesheet.mjs";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import "@material-ui/core";
import { DeleteIcon, EditIcon } from "@fluentui/react-icons";
import { useTranslation } from "react-i18next";
//#region src/udp/pages/UdpGridPanelManagementPage/UdpGridPanelManagementPage.jsx
const UdpGridPanelManagementPage = (props) => {
	const { params } = props;
	const { nodeObj: environment } = params;
	const { t } = useTranslation();
	const [loading, setLoading] = useState(false);
	const [gridPanels, setGridPanels] = useState([]);
	const [grids, setGrids] = useState([]);
	const [panels, setPanels] = useState([]);
	const [addSidesheetOpen, setAddSidesheetOpen] = useState(false);
	const [selectedRow, setSelectedRow] = useState();
	const [deleteDialogOpen, setDeleteDialogOpen] = useState();
	const [dataToDelete, setDataToDelete] = useState();
	const [grid, setGrid] = useState(null);
	const [panel, setPanel] = useState(null);
	const { handleErrorSnackbar, handleSuccessSnackbar } = useHandleAxiosSnackbar();
	const executeMutation = useCallback(async (endpoint, method, data) => apiMutate(ConfigService.uemV1ApiUrl, endpoint, { method: method || "post" }, data), []);
	const getGridPanels = useCallback(async () => {
		setLoading(true);
		const response = await executeMutation("uem", "post", { data: {
			customerId: null,
			environmentId: environment.environmentId,
			apiPath: `TenantService/api/v2/gridPanel`
		} });
		if (response?.status === 200) setGridPanels(response?.data);
		setLoading(false);
	}, [environment?.environmentId, executeMutation]);
	const getGrids = useCallback(async () => {
		const response_0 = await executeMutation("uem", "post", { data: {
			customerId: null,
			environmentId: environment.environmentId,
			apiPath: `TenantService/api/v2/grid`
		} });
		if (response_0?.status === 200) setGrids(response_0?.data);
	}, [environment?.environmentId, executeMutation]);
	const getPanels = useCallback(async () => {
		const response_1 = await executeMutation("uem", "post", { data: {
			customerId: null,
			environmentId: environment.environmentId,
			apiPath: `IntegrationService/api/v2/panel`
		} });
		if (response_1?.status === 200) setPanels(response_1?.data);
	}, [environment?.environmentId, executeMutation]);
	const createGridPanel = useCallback(async () => {
		setLoading(true);
		if ((await executeMutation("gridPanel/uemCreate", "post", { data: {
			customerId: null,
			gridId: grid.gridId,
			panelId: panel.panelId,
			environmentId: environment.environmentId,
			apiPath: `api/v1/gridPanel/uemCreate`
		} }))?.status === 201) {
			handleSuccessSnackbar("GridPanel Created Successfully");
			getGridPanels();
			setAddSidesheetOpen(false);
		} else handleErrorSnackbar("Something went wrong");
		setLoading(false);
	}, [
		environment,
		executeMutation,
		getGridPanels,
		grid,
		handleErrorSnackbar,
		handleSuccessSnackbar,
		panel
	]);
	const updateGridPanel = useCallback(async () => {
		setLoading(true);
		if ((await executeMutation("gridPanel/uemUpdate", "put", { data: {
			gridPanelId: selectedRow.gridPanelId,
			customerId: null,
			gridId: grid.gridId,
			panelId: panel.panelId,
			environmentId: environment.environmentId
		} }))?.status === 204) {
			handleSuccessSnackbar("GridPanel Updated Successfully");
			getGridPanels();
			setAddSidesheetOpen(false);
		} else handleErrorSnackbar("Something went wrong");
		setLoading(false);
	}, [
		environment,
		executeMutation,
		getGridPanels,
		grid,
		handleErrorSnackbar,
		handleSuccessSnackbar,
		panel,
		selectedRow
	]);
	const deleteGridPanel = useCallback(async () => {
		setLoading(true);
		if ((await executeMutation("gridPanel/uemDelete", "delete", { data: {
			gridPanelId: dataToDelete.gridPanelId,
			customerId: null,
			gridId: dataToDelete.gridId,
			panelId: dataToDelete.panelId,
			environmentId: environment.environmentId
		} }))?.status === 204) {
			handleSuccessSnackbar("GridPanel Deleted Successfully");
			getGridPanels();
		} else handleErrorSnackbar("Something went wrong");
		setDataToDelete();
		setLoading(false);
	}, [
		environment,
		executeMutation,
		getGridPanels,
		handleErrorSnackbar,
		handleSuccessSnackbar,
		dataToDelete
	]);
	const openDeleteDialog = (data_0) => {
		setDeleteDialogOpen(true);
		setDataToDelete(data_0);
		console.log(data_0);
	};
	useEffect(() => {
		getGridPanels();
		getGrids();
		getPanels();
	}, [
		getGridPanels,
		getGrids,
		getPanels
	]);
	const handleEdit = useCallback((row) => {
		setSelectedRow(row);
	}, []);
	useEffect(() => {
		if (selectedRow) {
			const selectedGrid = grids.find((grid_0) => grid_0.gridId === selectedRow.gridId);
			const selectedPanel = panels.find((panel_0) => panel_0.panelId === selectedRow.panelId);
			setGrid(selectedGrid);
			setPanel(selectedPanel);
			setAddSidesheetOpen(true);
		}
	}, [
		grids,
		panels,
		selectedRow
	]);
	const actionList = useMemo(() => [{
		id: 1,
		title: t("Edit"),
		icon: EditIcon,
		onClick: handleEdit,
		disabled: false
	}, {
		id: 2,
		title: t("Delete"),
		icon: DeleteIcon,
		onClick: openDeleteDialog,
		disabled: false
	}], [t, handleEdit]);
	const gridOptions = useMemo(() => ({
		frameworkComponents: { actionsRenderer: ActionsRenderer },
		columnDefs: [
			{
				headerName: t("gridPanelId"),
				field: "gridPanelId"
			},
			{
				headerName: t("gridId"),
				field: "gridId"
			},
			{
				headerName: t("panelId"),
				field: "panelId"
			},
			{
				headerName: t("Actions"),
				cellRenderer: "actionsRenderer",
				cellRendererParams: { actionList },
				pinned: "right",
				suppressMenu: true,
				sortable: false
			}
		]
	}), [actionList, t]);
	const handleClose = useCallback(() => {
		setGrid();
		setPanel();
		setSelectedRow();
		setAddSidesheetOpen(false);
	}, []);
	return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(UdpGridPanelManagementSidesheet, {
		title: selectedRow ? "Update GridPanel" : "Create GridPanel",
		open: addSidesheetOpen,
		onClose: handleClose,
		environment,
		grids,
		panels,
		grid,
		setGrid,
		panel,
		setPanel,
		onSubmit: selectedRow ? updateGridPanel : createGridPanel
	}), /* @__PURE__ */ React.createElement(FluentDialog, {
		open: deleteDialogOpen,
		handleClose: () => setDeleteDialogOpen(false),
		title: t("Delete GridPanel"),
		message: t("Are you sure?"),
		actionOne: deleteGridPanel,
		labelOne: t("Delete"),
		actionTwo: () => setDeleteDialogOpen(false),
		labelTwo: t("Cancel")
	}), /* @__PURE__ */ React.createElement(AmbientGridTemplate, {
		title: t("Grid Panel Manager"),
		useNewHeader: true,
		data: gridPanels,
		gridOptions,
		onPrimaryActionClick: () => setAddSidesheetOpen(true),
		primaryActionLabel: "Add",
		loading
	}));
};
//#endregion
export { UdpGridPanelManagementPage as default };

//# sourceMappingURL=UdpGridPanelManagementPage.mjs.map