import { Form } from "../../utilities/form/Form.mjs";
import { SubmitButton } from "../../utilities/form/SubmitButton.mjs";
import { FormButtons } from "../../utilities/form/FormButtons.mjs";
import { ConfigService } from "../../configService/index.mjs";
import { useAxiosGet } from "../../utilities/useAxiosGet.mjs";
import { ParamsMappingFields } from "./ParamsMappingFields.mjs";
import React, { useCallback, useMemo } from "react";
//#region src/dataBrowser/forms/ParamsMappingForm.jsx
const ParamsMappingForm = (props) => {
	const { queryId, paramMappings, properties, onSubmit = () => {}, fieldWidth = 180, propertyLabel = "Property" } = props;
	const [{ data: apiMethodInstance, loading: apiMethodInstancebLoading, error: apiMethodInstanceError }] = useAxiosGet(ConfigService.integrationV1ApiUrl, `apiMethodInstance/${queryId}`, {}, !!!queryId, false);
	const paramOptions = useMemo(() => {
		if (!apiMethodInstance) return [];
		try {
			const options = [];
			const bodyStr = apiMethodInstance.body;
			JSON.parse(bodyStr)?.filterElements?.forEach((filter) => {
				if (filter.searchValue.startsWith("params.")) {
					const key = filter?.searchValue.replace("params.", "");
					options.push({
						label: key,
						value: key
					});
				}
			});
			return options;
		} catch {
			console.warn("Error parsing apiMethodInstance body");
			return [];
		}
	}, [apiMethodInstance]);
	const propertyOptions = useMemo(() => {
		if (!properties) return [];
		return properties.filter((property) => property?.type !== "array" && property?.type !== "object");
	}, [properties]);
	const handleOnSubmit = useCallback((values) => {
		const paramMappings_0 = {};
		const parameters = /* @__PURE__ */ new Set();
		Object.keys(values).forEach((key_0) => {
			const parameter = key_0.split("-")[1];
			parameters.add(parameter);
		});
		parameters.forEach((parameter_0) => {
			const property_0 = values[`property-${parameter_0}`];
			if (parameter_0 && property_0) {
				const parameterValue = parameter_0;
				paramMappings_0[parameterValue] = property_0.name;
			}
		});
		onSubmit(paramMappings_0);
	}, [onSubmit]);
	return /* @__PURE__ */ React.createElement(Form, { onSubmit: handleOnSubmit }, /* @__PURE__ */ React.createElement(ParamsMappingFields, {
		paramMappings,
		paramOptions,
		propertyOptions,
		fieldWidth,
		propertyLabel
	}), /* @__PURE__ */ React.createElement(FormButtons, null, /* @__PURE__ */ React.createElement(SubmitButton, {
		variant: "contained",
		color: "primary",
		disablePristine: false,
		fullWidth: true,
		style: {
			marginRight: 8,
			marginLeft: 8
		}
	}, "Save Mapping")));
};
//#endregion
export { ParamsMappingForm };

//# sourceMappingURL=ParamsMappingForm.mjs.map