import { ConfigService } from "../../configService/index.mjs";
import { useHandleAxiosSnackbar } from "../../utilities/apiHelpers.mjs";
import { useUser } from "../../utilities/auth/useUser.mjs";
import { apiMutate } from "../../utilities/useAxiosMutate.mjs";
import { usePromotedMethodEntity } from "../../hooks/usePromotedMethodEntity.mjs";
import { executeQueryAdHoc } from "../utilities/methodUtils.mjs";
import { UdpButton } from "../../UI/fluent-web-components/UdpButton.mjs";
import { UdpAutocomplete } from "../../UI/fluent-web-components/UdpAutocomplete.mjs";
import { UdpCard } from "../../UI/fluent-web-components/UdpCard.mjs";
import { UdpDrawer } from "../../UI/fluent-web-components/UdpDrawer.mjs";
import { UdpFileInput } from "../../UI/fluent-web-components/UdpFileInput.mjs";
import { FileImportConfigCard } from "./FileImportConfigCard.mjs";
import FileImportMappingWizard from "./FileImportMappingWizard.mjs";
import { c } from "react-compiler-runtime";
import React, { useEffect, useState } from "react";
//#region src/udp/fileImportMapping/ImportDataFromFileMapping.jsx
const ImportDataFromFileMapping = (props) => {
	const $ = c(44);
	const { onImportSuccess, onImportFail, verifyRequiredFields: t0 } = props;
	const verifyRequiredFields = t0 === void 0 ? true : t0;
	const user = useUser();
	const [selectedFileMapping, setSelectedFileMapping] = useState();
	const [udpImportMappingData, setUdpImportMappingData] = useState();
	const [newFileMappingSidesheetOpen, setNewFileMappingSidesheetOpen] = useState(false);
	const [fileObject, setFileObject] = useState();
	let t1;
	if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
		t1 = [];
		$[0] = t1;
	} else t1 = $[0];
	const [fileItems, setFileItems] = useState(t1);
	const [isLoading, setIsLoading] = useState(false);
	const { handleErrorSnackbar, handleSuccessSnackbar } = useHandleAxiosSnackbar();
	const { data: udpFileImportMappingEntity } = usePromotedMethodEntity("UdpFileImportMapping", true);
	const fm = selectedFileMapping ?? {};
	const executeImportDataFromFile = async (data) => apiMutate(ConfigService.productV1ApiUrl, `${fm.objectInfoSanitizedName}/fileImportMapping`, { method: "post" }, { data });
	let t2;
	if ($[1] !== udpFileImportMappingEntity?.unityBaseSearchMethod?.apiMethodId) {
		t2 = async () => {
			if (udpFileImportMappingEntity?.unityBaseSearchMethod?.apiMethodId) {
				const res = await executeQueryAdHoc({ data: {
					eagerLoad: true,
					pageSize: 200,
					filterElements: []
				} }, udpFileImportMappingEntity?.unityBaseSearchMethod?.apiMethodId);
				if (res?.data?.pageList.length > 0) setUdpImportMappingData(res?.data?.pageList);
			}
		};
		$[1] = udpFileImportMappingEntity?.unityBaseSearchMethod?.apiMethodId;
		$[2] = t2;
	} else t2 = $[2];
	udpFileImportMappingEntity?.unityBaseSearchMethod?.apiMethodId;
	const searchFileImportMappingData = t2;
	let t3;
	let t4;
	if ($[3] !== searchFileImportMappingData) {
		t3 = () => {
			searchFileImportMappingData();
		};
		t4 = [searchFileImportMappingData];
		$[3] = searchFileImportMappingData;
		$[4] = t3;
		$[5] = t4;
	} else {
		t3 = $[4];
		t4 = $[5];
	}
	useEffect(t3, t4);
	const handleImportDataFromFile = async () => {
		setIsLoading(true);
		const file = fileObject;
		const blob = new Blob([file], { type: file.type });
		const formData = new FormData();
		formData.append("file", blob, file.name);
		formData.append("tenantId", user?.currentTenantId);
		formData.append("name", fm.name);
		formData.append("objectInfoSanitizedName", fm.objectInfoSanitizedName);
		formData.append("fileImportType", fm.fileImportType);
		formData.append("mapping", fm.mapping);
		formData.append("properties", fm.properties);
		formData.append("id", fm.id);
		if ((await executeImportDataFromFile(formData).catch((res_0) => {
			setIsLoading(false);
			handleErrorSnackbar(res_0);
			onImportFail();
		}))?.status === 201) {
			handleSuccessSnackbar("Imported Data");
			setIsLoading(false);
			onImportSuccess && onImportSuccess();
		}
	};
	const getFileMappingOptionlabel = _temp;
	let t5;
	if ($[6] === Symbol.for("react.memo_cache_sentinel")) {
		t5 = (e) => {
			const items = e.target.value;
			setFileItems(items);
			setFileObject(items.find(_temp2)?.file);
		};
		$[6] = t5;
	} else t5 = $[6];
	const handleAddFileToUpload = t5;
	let t6;
	if ($[7] === Symbol.for("react.memo_cache_sentinel")) {
		t6 = { display: "flex" };
		$[7] = t6;
	} else t6 = $[7];
	let t7;
	if ($[8] === Symbol.for("react.memo_cache_sentinel")) {
		t7 = (e_0) => setSelectedFileMapping(e_0.target.value);
		$[8] = t7;
	} else t7 = $[8];
	let t8;
	if ($[9] !== selectedFileMapping || $[10] !== udpImportMappingData) {
		t8 = /* @__PURE__ */ React.createElement("div", { style: t6 }, /* @__PURE__ */ React.createElement(UdpAutocomplete, {
			name: "fileMapping",
			label: "File Mapping",
			onChange: t7,
			options: udpImportMappingData,
			optionLabel: getFileMappingOptionlabel,
			optionValue: "id",
			value: selectedFileMapping,
			required: true,
			optionGroup: _temp3
		}));
		$[9] = selectedFileMapping;
		$[10] = udpImportMappingData;
		$[11] = t8;
	} else t8 = $[11];
	let t9;
	if ($[12] !== fileItems || $[13] !== selectedFileMapping) {
		t9 = selectedFileMapping && /* @__PURE__ */ React.createElement(UdpFileInput, {
			value: fileItems,
			onChange: handleAddFileToUpload,
			accept: ".csv,.txt,.xlsx,.json",
			maxFiles: 1
		});
		$[12] = fileItems;
		$[13] = selectedFileMapping;
		$[14] = t9;
	} else t9 = $[14];
	let t10;
	if ($[15] === Symbol.for("react.memo_cache_sentinel")) {
		t10 = {
			display: "flex",
			flexDirection: "row",
			gap: 5,
			float: "right",
			marginTop: 10
		};
		$[15] = t10;
	} else t10 = $[15];
	let t11;
	if ($[16] === Symbol.for("react.memo_cache_sentinel")) {
		t11 = /* @__PURE__ */ React.createElement(UdpButton, {
			onClick: () => setNewFileMappingSidesheetOpen(true),
			appearance: "outline"
		}, "Create New Mapping");
		$[16] = t11;
	} else t11 = $[16];
	const t12 = !fileObject;
	let t13;
	if ($[17] !== handleImportDataFromFile || $[18] !== isLoading || $[19] !== t12) {
		t13 = /* @__PURE__ */ React.createElement("div", { style: t10 }, t11, /* @__PURE__ */ React.createElement(UdpButton, {
			appearance: "primary",
			onClick: handleImportDataFromFile,
			loading: isLoading,
			disabled: t12
		}, "Import Data"));
		$[17] = handleImportDataFromFile;
		$[18] = isLoading;
		$[19] = t12;
		$[20] = t13;
	} else t13 = $[20];
	let t14;
	if ($[21] === Symbol.for("react.memo_cache_sentinel")) {
		t14 = () => setNewFileMappingSidesheetOpen(false);
		$[21] = t14;
	} else t14 = $[21];
	let t15;
	if ($[22] !== searchFileImportMappingData || $[23] !== verifyRequiredFields) {
		t15 = /* @__PURE__ */ React.createElement(FileImportMappingWizard, {
			refetchMappings: searchFileImportMappingData,
			setNewFileMappingSidesheetOpen,
			verifyRequiredFields
		});
		$[22] = searchFileImportMappingData;
		$[23] = verifyRequiredFields;
		$[24] = t15;
	} else t15 = $[24];
	let t16;
	if ($[25] !== newFileMappingSidesheetOpen || $[26] !== t15) {
		t16 = /* @__PURE__ */ React.createElement(UdpDrawer, {
			drawerTitle: "Create New File Mapping",
			open: newFileMappingSidesheetOpen,
			onDrawerClose: t14,
			drawerWidth: "60%"
		}, t15);
		$[25] = newFileMappingSidesheetOpen;
		$[26] = t15;
		$[27] = t16;
	} else t16 = $[27];
	let t17;
	if ($[28] !== t13 || $[29] !== t16 || $[30] !== t8 || $[31] !== t9) {
		t17 = /* @__PURE__ */ React.createElement(UdpCard, { width: "100%" }, t8, t9, t13, t16);
		$[28] = t13;
		$[29] = t16;
		$[30] = t8;
		$[31] = t9;
		$[32] = t17;
	} else t17 = $[32];
	let t18;
	if ($[33] === Symbol.for("react.memo_cache_sentinel")) {
		t18 = { marginTop: 10 };
		$[33] = t18;
	} else t18 = $[33];
	const T0 = FileImportConfigCard;
	const t19 = fm.udpFileImportMappingUdpObjectInfo?.sanitizedName.replace(/([A-Z])/g, " $1").trim();
	let t20;
	if ($[34] !== T0 || $[35] !== selectedFileMapping || $[36] !== t19) {
		t20 = /* @__PURE__ */ React.createElement(T0, {
			fileMappingRecord: selectedFileMapping,
			domain: t19
		});
		$[34] = T0;
		$[35] = selectedFileMapping;
		$[36] = t19;
		$[37] = t20;
	} else t20 = $[37];
	let t21;
	if ($[38] !== t18 || $[39] !== t20) {
		t21 = /* @__PURE__ */ React.createElement("div", { style: t18 }, t20);
		$[38] = t18;
		$[39] = t20;
		$[40] = t21;
	} else t21 = $[40];
	let t22;
	if ($[41] !== t17 || $[42] !== t21) {
		t22 = /* @__PURE__ */ React.createElement(React.Fragment, null, t17, t21);
		$[41] = t17;
		$[42] = t21;
		$[43] = t22;
	} else t22 = $[43];
	return t22;
};
function _temp(option) {
	return `${option.name} (${option?.udpFileImportMappingUdpFileImportType?.extension})`;
}
function _temp2(item) {
	return item.kind === "pending";
}
function _temp3(option_0) {
	return option_0?.udpFileImportMappingUdpObjectInfo?.sanitizedName;
}
//#endregion
export { ImportDataFromFileMapping };

//# sourceMappingURL=ImportDataFromFileMapping.mjs.map