import { apiMutate } from "../../../utilities/useAxiosMutate.mjs";
import { SideSheet } from "../../surfaces/SideSheet.mjs";
import { useAgGridApi } from "../../../utilities/aggrid/AgTable.mjs";
import { FilterOperators } from "../../../models/SearchOperator.mjs";
import { AmbientGridTemplate } from "../../templates/deprecated/AmbientGridTemplate/AmbientGridTemplate.mjs";
import { c } from "react-compiler-runtime";
import React, { useState } from "react";
import { useTranslation } from "react-i18next";
//#region src/UI/inputs/fieldArrayCard/SearchSideSheet.jsx
const SearchSideSheet = (props) => {
	const $ = c(44);
	const { open, handleClose, exclude, title, productApiBase, searchApi, colDefs: t0, gridTitle, idField, formIdField, handleSearch, width: t1, height: t2 } = props;
	let t3;
	if ($[0] !== t0) {
		t3 = t0 === void 0 ? [] : t0;
		$[0] = t0;
		$[1] = t3;
	} else t3 = $[1];
	const colDefs = t3;
	const width = t1 === void 0 ? "800px" : t1;
	const height = t2 === void 0 ? "58vh" : t2;
	let t4;
	if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
		t4 = [];
		$[2] = t4;
	} else t4 = $[2];
	const [selectedData, setSelectedData] = useState(t4);
	const { t } = useTranslation();
	const { gridApi, onGridReady, gridColumnApi } = useAgGridApi();
	let t5;
	if ($[3] !== productApiBase || $[4] !== searchApi) {
		t5 = async (data) => apiMutate(productApiBase, searchApi, { method: "post" }, data);
		$[3] = productApiBase;
		$[4] = searchApi;
		$[5] = t5;
	} else t5 = $[5];
	const searchData = t5;
	let t6;
	if ($[6] !== gridApi) {
		t6 = () => {
			setSelectedData(gridApi?.getSelectedRows() ?? []);
		};
		$[6] = gridApi;
		$[7] = t6;
	} else t6 = $[7];
	const onSelectionChanged = t6;
	let t7;
	if ($[8] !== handleClose) {
		t7 = (data_0) => {
			handleClose(data_0);
			setSelectedData([]);
		};
		$[8] = handleClose;
		$[9] = t7;
	} else t7 = $[9];
	const handleCloseSideSheet = t7;
	let t8;
	if ($[10] !== handleSearch || $[11] !== searchData) {
		t8 = { getRows: async (t9) => {
			const { request, success, fail } = t9;
			if (handleSearch) {
				handleSearch(20, request, success, fail);
				return;
			}
			await searchData({ data: {
				filterElements: request?.filterModel ? Object.entries(request.filterModel)?.map(_temp) : [],
				orderElements: request?.sortModel ? request.sortModel?.map(_temp2) : [],
				pagesize: 20,
				pageNumber: (request?.startRow ?? 0) / 20 + 1
			} }).then((response) => {
				success({
					rowData: response.data?.pageList,
					rowCount: response.data?.total
				});
			}).catch((error) => {
				fail();
			});
		} };
		$[10] = handleSearch;
		$[11] = searchData;
		$[12] = t8;
	} else t8 = $[12];
	const dataSource = t8;
	let t9;
	if ($[13] !== handleCloseSideSheet) {
		t9 = () => handleCloseSideSheet();
		$[13] = handleCloseSideSheet;
		$[14] = t9;
	} else t9 = $[14];
	let t10;
	if ($[15] !== t) {
		t10 = t("Choose");
		$[15] = t;
		$[16] = t10;
	} else t10 = $[16];
	let t11;
	if ($[17] !== handleCloseSideSheet || $[18] !== selectedData) {
		t11 = () => {
			handleCloseSideSheet(selectedData);
		};
		$[17] = handleCloseSideSheet;
		$[18] = selectedData;
		$[19] = t11;
	} else t11 = $[19];
	let t12;
	if ($[20] !== exclude || $[21] !== formIdField || $[22] !== idField) {
		t12 = {
			field: "",
			checkboxSelection: true,
			maxWidth: 60,
			minWidth: 60,
			cellRenderer: (params) => {
				console.log({
					exclude,
					formIdField,
					idField,
					bool: exclude?.find((selectedItem) => selectedItem[formIdField] === params.data?.[idField])
				});
				if (params?.data && !!exclude?.find((selectedItem_0) => selectedItem_0[formIdField] === params.data?.[idField])) params.node.setSelected(true);
			}
		};
		$[20] = exclude;
		$[21] = formIdField;
		$[22] = idField;
		$[23] = t12;
	} else t12 = $[23];
	let t13;
	if ($[24] !== colDefs || $[25] !== t12) {
		t13 = [t12, ...colDefs];
		$[24] = colDefs;
		$[25] = t12;
		$[26] = t13;
	} else t13 = $[26];
	let t14;
	if ($[27] !== dataSource || $[28] !== gridApi || $[29] !== gridColumnApi || $[30] !== gridTitle || $[31] !== height || $[32] !== onGridReady || $[33] !== onSelectionChanged || $[34] !== t13) {
		t14 = /* @__PURE__ */ React.createElement(AmbientGridTemplate, {
			title: gridTitle,
			rowModelType: "serverSide",
			serverSideDatasource: dataSource,
			cacheBlockSize: 20,
			serverSideStoreType: "partial",
			paginationPageSize: 20,
			columnDefs: t13,
			hideGroupTab: true,
			hideColumnTab: true,
			suppressRowClickSelection: true,
			onSelectionChanged,
			rowSelection: "multiple",
			api: gridApi,
			gridReady: onGridReady,
			columnApi: gridColumnApi,
			dynamicSizing: true,
			height
		});
		$[27] = dataSource;
		$[28] = gridApi;
		$[29] = gridColumnApi;
		$[30] = gridTitle;
		$[31] = height;
		$[32] = onGridReady;
		$[33] = onSelectionChanged;
		$[34] = t13;
		$[35] = t14;
	} else t14 = $[35];
	let t15;
	if ($[36] !== open || $[37] !== t10 || $[38] !== t11 || $[39] !== t14 || $[40] !== t9 || $[41] !== title || $[42] !== width) {
		t15 = /* @__PURE__ */ React.createElement(SideSheet, {
			open,
			onClose: t9,
			title,
			width,
			buttonLabel: t10,
			onSubmit: t11
		}, t14);
		$[36] = open;
		$[37] = t10;
		$[38] = t11;
		$[39] = t14;
		$[40] = t9;
		$[41] = title;
		$[42] = width;
		$[43] = t15;
	} else t15 = $[43];
	return t15;
};
function _temp(t0) {
	const [key, value] = t0;
	return {
		searchField: key,
		searchOperator: FilterOperators[value?.type],
		searchValue: value?.filter
	};
}
function _temp2(sortObj) {
	return {
		sortColumn: sortObj?.colId,
		sortDirection: sortObj?.sort?.toUpperCase()
	};
}
//#endregion
export { SearchSideSheet as default };

//# sourceMappingURL=SearchSideSheet.mjs.map