import { ConfigService } from "../../../configService/index.mjs";
import { useUser } from "../../../utilities/auth/useUser.mjs";
import { useAxiosGet } from "../../../utilities/useAxiosGet.mjs";
import { useApiCatalog } from "../../../hooks/useApiCatalog.mjs";
import { UdpButton } from "../../../UI/fluent-web-components/UdpButton.mjs";
import { UdpDialog } from "../../../UI/fluent-web-components/UdpDialog.mjs";
import { ClientSideUdpGrid } from "../../../UI/templates/newGrid/ClientSideUdpGrid.mjs";
import { UdpText } from "../../../UI/fluent-web-components/UdpText.mjs";
import { useLookups } from "../../lookups/useLookups.mjs";
import LookupCreationSheet from "../../lookups/LookupCreationSheet.mjs";
import React, { useCallback, useMemo, useState } from "react";
import { LookupTypeId } from "udp-stencil-component-library";
//#region src/udp/pages/UdpLookupManagementPage/UdpLookupManagementPage.jsx
const UdpLookupManagementPage = ({ productId }) => {
	const tenantId = useUser()?.currentTenantId;
	const resolvedProductId = productId ?? ConfigService.config.UNITY_PRODUCT_ID;
	const [{ data: productCatalog }] = useAxiosGet(ConfigService.integrationV1ApiUrl, `tenantProductApiCatalogBase/tenant/${tenantId}/product/${resolvedProductId}`, {}, !tenantId || !resolvedProductId, true);
	const apiCatalogId = productCatalog?.apiCatalogId;
	const { lookups, loading: lookupsLoading, refetch, methodStatus, createLookup, updateLookup, deleteLookup, fetchColumns, createLookupColumn, updateLookupColumn, fetchValues, createLookupValue, deleteLookupValue } = useLookups(apiCatalogId, productId);
	const [loading, setLoading] = useState(false);
	const [sheetOpen, setSheetOpen] = useState(false);
	const { data: catalogData } = useApiCatalog(sheetOpen ? apiCatalogId : void 0);
	const catalogObjectList = catalogData?.ctlg?.catalogObjectList ?? [];
	const [editLookup, setEditLookup] = useState(null);
	const [deleteTarget, setDeleteTarget] = useState(null);
	const [deleting, setDeleting] = useState(false);
	const handleRefresh = useCallback(() => {
		setLoading(true);
		refetch();
	}, [refetch]);
	const handleCreate = useCallback(() => {
		setEditLookup(null);
		setSheetOpen(true);
	}, []);
	const handleEdit = useCallback((_, params) => {
		setEditLookup(params?.data);
		setSheetOpen(true);
	}, []);
	const handleSheetClose = useCallback(() => {
		setSheetOpen(false);
		setEditLookup(null);
	}, []);
	const handleLookupCreatedOrUpdated = useCallback(() => {
		refetch();
	}, [refetch]);
	const handleDeleteConfirm = useCallback(async () => {
		if (!deleteTarget) return;
		setDeleting(true);
		try {
			await deleteLookup(deleteTarget);
			setDeleteTarget(null);
			refetch();
		} finally {
			setDeleting(false);
		}
	}, [
		deleteTarget,
		deleteLookup,
		refetch
	]);
	useMemo(() => ({
		true: "success",
		false: "secondary"
	}), []);
	const columnDefs = useMemo(() => [
		{
			headerName: "Name",
			field: "name"
		},
		{
			headerName: "Description",
			field: "description"
		},
		{
			headerName: "Domain",
			field: "domainName",
			valueFormatter: (params_0) => {
				if (!params_0.value) return "";
				const parts = params_0.value.split(".");
				return parts[parts.length - 1];
			}
		},
		{
			headerName: "Type",
			field: "typeId",
			cellRenderer: "statusRenderer",
			cellRendererParams: { getStatusConfig: (params_1) => {
				if (params_1.value === LookupTypeId.Domain) return {
					label: "Domain",
					color: "warning"
				};
				if (params_1.value === LookupTypeId.Lookup) return {
					label: "Virtual",
					color: "info"
				};
				return null;
			} }
		},
		{
			headerName: "Key Name",
			field: "keyName"
		},
		{
			headerName: "Value Name",
			field: "valueName"
		},
		{
			field: "actions",
			cellRenderer: "actionsRenderer",
			cellRendererParams: { actionList: [{
				id: 1,
				iconName: "edit",
				title: "Edit",
				handleOnClick: handleEdit
			}, {
				id: 2,
				iconName: "delete",
				title: "Delete",
				handleOnClick: (__0, params_2) => setDeleteTarget(params_2?.data)
			}] }
		}
	], [handleEdit]);
	const gridHeaderConfig = useMemo(() => ({
		title: "Lookups",
		primaryActionButton: {
			label: "Create Lookup",
			iconName: "add",
			onClick: handleCreate
		}
	}), [handleCreate]);
	const gridFunctions = useMemo(() => [{
		name: "agGridRefreshData",
		params: {
			onRefreshStart: handleRefresh,
			onRefreshComplete: () => setLoading(false)
		}
	}, { name: "quickFilter" }], [handleRefresh]);
	return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(ClientSideUdpGrid, {
		rowData: lookups ?? [],
		loading: loading || lookupsLoading,
		columnDefs,
		gridHeaderConfig,
		gridFunctions,
		gridOptions: { pagination: true }
	}), /* @__PURE__ */ React.createElement(LookupCreationSheet, {
		open: sheetOpen,
		onClose: handleSheetClose,
		lookup: editLookup,
		onCreateLookup: createLookup,
		onCreateLookupColumn: createLookupColumn,
		onCreateLookupValue: createLookupValue,
		onUpdateLookup: updateLookup,
		onUpdateLookupColumn: updateLookupColumn,
		onDeleteLookupValue: deleteLookupValue,
		fetchColumns,
		fetchValues,
		onLookupCreated: handleLookupCreatedOrUpdated,
		catalogObjectList,
		lookupMethodStatus: methodStatus
	}), /* @__PURE__ */ React.createElement(UdpDialog, {
		open: !!deleteTarget,
		dialogTitle: "Delete Lookup",
		onDialogClose: () => setDeleteTarget(null)
	}, /* @__PURE__ */ React.createElement(UdpText, null, "Are you sure you want to delete “", deleteTarget?.name, "”? This action cannot be undone."), /* @__PURE__ */ React.createElement(UdpButton, {
		appearance: "outline",
		onClick: () => setDeleteTarget(null),
		slot: "action"
	}, "Cancel"), /* @__PURE__ */ React.createElement(UdpButton, {
		appearance: "primary",
		onClick: handleDeleteConfirm,
		loading: deleting,
		slot: "action"
	}, "Delete")));
};
//#endregion
export { UdpLookupManagementPage };

//# sourceMappingURL=UdpLookupManagementPage.mjs.map