import { ConfigService } from "../../configService/index.mjs";
import { useHandleAxiosSnackbar } from "../../utilities/apiHelpers.mjs";
import { apiMutate } from "../../utilities/useAxiosMutate.mjs";
import { usePromotedMethodEntity } from "../../hooks/usePromotedMethodEntity.mjs";
import { usePromotedLookupMethods } from "../../hooks/usePromotedLookupMethods.mjs";
import { useCallback, useEffect, useState } from "react";
//#region src/udp/lookups/useLookups.ts
const resolveMethodId = (methods, key) => methods?.[key]?.apiMethodId;
const showError = (handleErrorSnackbar, message) => handleErrorSnackbar(null, message, null, null);
const useLookups = (apiCatalogId, productId) => {
	const [lookups, setLookups] = useState([]);
	const [loading, setLoading] = useState(false);
	const { handleErrorSnackbar } = useHandleAxiosSnackbar();
	const productIdArg = productId != null ? String(productId) : void 0;
	const productIdOrNull = productId ?? null;
	const { data: lookupMethods, loading: methodsLoading } = usePromotedLookupMethods(apiCatalogId, productIdArg);
	const getAllMethodId = resolveMethodId(lookupMethods, "unityBaseGetAllMethod");
	const getDataMethodId = resolveMethodId(lookupMethods, "unityBaseGetDataMethod") ?? resolveMethodId(lookupMethods, "unityBaseGetMethod");
	const createMethodId = resolveMethodId(lookupMethods, "unityBaseCreateMethod");
	const updateMethodId = resolveMethodId(lookupMethods, "unityBaseUpdateMethod");
	const deleteMethodId = resolveMethodId(lookupMethods, "unityBaseDeleteMethod");
	const { data: columnMethods } = usePromotedMethodEntity("UdpLookupColumn", true, apiCatalogId, productIdOrNull);
	const createColumnMethodId = resolveMethodId(columnMethods, "unityBaseCreateMethod");
	const getAllColumnsMethodId = resolveMethodId(columnMethods, "unityBaseGetAllMethod");
	const updateColumnMethodId = resolveMethodId(columnMethods, "unityBaseUpdateMethod");
	const deleteColumnMethodId = resolveMethodId(columnMethods, "unityBaseDeleteMethod");
	const { data: valueMethods } = usePromotedMethodEntity("UdpLookupValue", true, apiCatalogId, productIdOrNull);
	const createValueMethodId = resolveMethodId(valueMethods, "unityBaseCreateMethod");
	const getAllValuesMethodId = resolveMethodId(valueMethods, "unityBaseGetAllMethod");
	const updateValueMethodId = resolveMethodId(valueMethods, "unityBaseUpdateMethod");
	const deleteValueMethodId = resolveMethodId(valueMethods, "unityBaseDeleteMethod");
	const execute = useCallback(async (methodId, data = {}) => {
		if (!methodId) return null;
		try {
			const response = await apiMutate(ConfigService.integrationV1ApiUrl, `apimethod/executeQueryWithParameters/${methodId}`, { method: "post" }, { data });
			if (response?.status === 200 || response?.status === 201) return response.data ?? null;
		} catch {
			return null;
		}
		return null;
	}, []);
	const fetchLookups = useCallback(async () => {
		if (!getAllMethodId) return;
		setLoading(true);
		try {
			setLookups(await execute(getAllMethodId) ?? []);
		} catch {
			showError(handleErrorSnackbar, "Failed to fetch lookups");
		} finally {
			setLoading(false);
		}
	}, [
		getAllMethodId,
		execute,
		handleErrorSnackbar
	]);
	useEffect(() => {
		fetchLookups();
	}, [fetchLookups]);
	const createLookup = useCallback(async (lookupData) => {
		const result = await execute(createMethodId, { requestBody: lookupData });
		if (!result) showError(handleErrorSnackbar, "Failed to create lookup");
		return result;
	}, [
		createMethodId,
		execute,
		handleErrorSnackbar
	]);
	const updateLookup = useCallback(async (lookupData_0) => {
		const { id, ...rest } = lookupData_0;
		const result_0 = await execute(updateMethodId, {
			id,
			requestBody: rest
		});
		if (!result_0) showError(handleErrorSnackbar, "Failed to update lookup");
		return result_0;
	}, [
		updateMethodId,
		execute,
		handleErrorSnackbar
	]);
	const deleteLookup = useCallback(async (lookupData_1) => {
		const result_1 = await execute(deleteMethodId, { id: lookupData_1.id });
		if (!result_1) showError(handleErrorSnackbar, "Failed to delete lookup");
		return result_1;
	}, [
		deleteMethodId,
		execute,
		handleErrorSnackbar
	]);
	const fetchColumns = useCallback(async () => {
		return await execute(getAllColumnsMethodId) ?? [];
	}, [getAllColumnsMethodId, execute]);
	const createLookupColumn = useCallback(async (columnData) => {
		const result_3 = await execute(createColumnMethodId, { requestBody: columnData });
		if (!result_3) showError(handleErrorSnackbar, "Failed to create lookup column");
		return result_3;
	}, [
		createColumnMethodId,
		execute,
		handleErrorSnackbar
	]);
	const updateLookupColumn = useCallback(async (columnData_0) => {
		const { id: id_0, ...rest_0 } = columnData_0;
		const result_4 = await execute(updateColumnMethodId, {
			id: id_0,
			requestBody: rest_0
		});
		if (!result_4) showError(handleErrorSnackbar, "Failed to update lookup column");
		return result_4;
	}, [
		updateColumnMethodId,
		execute,
		handleErrorSnackbar
	]);
	const deleteLookupColumn = useCallback(async (columnData_1) => {
		const result_5 = await execute(deleteColumnMethodId, { id: columnData_1.id });
		if (!result_5) showError(handleErrorSnackbar, "Failed to delete lookup column");
		return result_5;
	}, [
		deleteColumnMethodId,
		execute,
		handleErrorSnackbar
	]);
	const fetchValues = useCallback(async () => {
		return await execute(getAllValuesMethodId) ?? [];
	}, [getAllValuesMethodId, execute]);
	const createLookupValue = useCallback(async (valueData) => {
		const result_7 = await execute(createValueMethodId, { requestBody: valueData });
		if (!result_7) showError(handleErrorSnackbar, "Failed to create lookup value");
		return result_7;
	}, [
		createValueMethodId,
		execute,
		handleErrorSnackbar
	]);
	const updateLookupValue = useCallback(async (valueData_0) => {
		const { id: id_1, ...rest_1 } = valueData_0;
		const result_8 = await execute(updateValueMethodId, {
			id: id_1,
			requestBody: rest_1
		});
		if (!result_8) showError(handleErrorSnackbar, "Failed to update lookup value");
		return result_8;
	}, [
		updateValueMethodId,
		execute,
		handleErrorSnackbar
	]);
	const deleteLookupValue = useCallback(async (valueData_1) => {
		const result_9 = await execute(deleteValueMethodId, { id: valueData_1.id });
		if (!result_9) showError(handleErrorSnackbar, "Failed to delete lookup value");
		return result_9;
	}, [
		deleteValueMethodId,
		execute,
		handleErrorSnackbar
	]);
	/**
	* Fetch a single lookup's full data (rows + keyName/valueName) by ID.
	*/
	const fetchLookupData = useCallback(async (lookupId) => {
		return execute(getDataMethodId, { id: lookupId });
	}, [getDataMethodId, execute]);
	/**
	* Resolve a raw value through a lookup.
	* Returns the display value, or the raw value if resolution fails.
	*
	* @param lookupId - The lookup to resolve against
	* @param rawValue - The raw value to look up (matched against keyName)
	* @param overrideKeyName - Optional override for which field to match on
	* @param overrideValueName - Optional override for which field to return
	*/
	const resolveLookupValue = useCallback(async (lookupId_0, rawValue, overrideKeyName, overrideValueName) => {
		if (rawValue == null) return rawValue;
		const lookupData_2 = await fetchLookupData(lookupId_0);
		if (!lookupData_2?.data) return rawValue;
		const key = overrideKeyName ?? lookupData_2.keyName;
		const val = overrideValueName ?? lookupData_2.valueName;
		if (!key || !val) return rawValue;
		const match = lookupData_2.data.find((row) => String(row[key]) === String(rawValue));
		return match ? match[val] : rawValue;
	}, [fetchLookupData]);
	return {
		lookups,
		loading: loading || methodsLoading,
		refetch: fetchLookups,
		methodStatus: {
			lookupCreateAvailable: !!createMethodId,
			lookupUpdateAvailable: !!updateMethodId,
			lookupDeleteAvailable: !!deleteMethodId,
			columnCreateAvailable: !!createColumnMethodId,
			columnUpdateAvailable: !!updateColumnMethodId,
			columnDeleteAvailable: !!deleteColumnMethodId,
			valueCreateAvailable: !!createValueMethodId,
			valueUpdateAvailable: !!updateValueMethodId,
			valueDeleteAvailable: !!deleteValueMethodId
		},
		createLookup,
		updateLookup,
		deleteLookup,
		fetchColumns,
		createLookupColumn,
		updateLookupColumn,
		deleteLookupColumn,
		fetchValues,
		createLookupValue,
		updateLookupValue,
		deleteLookupValue,
		fetchLookupData,
		resolveLookupValue
	};
};
//#endregion
export { useLookups };

//# sourceMappingURL=useLookups.mjs.map