import { n as useTranslation } from "./i18n-DoP1NB7u.js";
import { p as d, v as u } from "./vendor-preact-DThS2ZQ_.js";
import { c as useManager, o as useModuleTerminalCallbacks } from "./hooks-Da_yP_Dr.js";
import { t as IncodeComponent } from "./incodeComponent-CCP7GTMj.js";
import { r as Spacer, t as LoadingIcon } from "./loadingIcon-B0ysgiNq.js";
import { t as Page } from "./page-DidH_RkX.js";
import { t as Button } from "./button-BMYTQ8fc.js";
import { t as SuccessIcon } from "./successIcon-B5mGvdx1.js";
import { t as InputComposed } from "./inputComposed-C1R2LAa3.js";
import { t as Checkbox } from "./checkbox-1wUHSoaK.js";
import { t as DateInputComposed } from "./dateInputComposed-DuaofcBl.js";
import { t as registerIncodeModuleElement } from "./incodeModule-M8DszCee.js";
import { t as StatusPage } from "./statusPage-BUm5gtQY.js";
import { createCustomFieldsManager } from "@incodetech/core/custom-fields";
//#region src/modules/custom-fields/customFields.tsx
var INPUT_TYPE_MAP = {
	INTEGER: "number",
	DOUBLE: "number",
	STRING: "text"
};
var INPUT_STEP_MAP = { DOUBLE: "any" };
var DateFieldInput = ({ field, isDisabled, onChange }) => {
	const [dateValue, setDateValue] = d("");
	return /* @__PURE__ */ u("div", {
		class: "IncodeCustomFieldsField",
		children: /* @__PURE__ */ u(DateInputComposed, {
			id: `custom-field-${field.name}`,
			name: field.name,
			label: field.label,
			disabled: isDisabled,
			value: dateValue,
			onInput: (e) => {
				const v = e.target.value;
				setDateValue(v);
				onChange(field.name, v);
			}
		})
	});
};
var CustomFieldInput = ({ field, isDisabled, onChange }) => {
	const id = `custom-field-${field.name}`;
	if (field.type === "BOOLEAN") return /* @__PURE__ */ u("div", {
		class: "IncodeCustomFieldsField",
		children: /* @__PURE__ */ u(Checkbox, {
			id,
			name: field.name,
			label: field.label,
			disabled: isDisabled,
			onChange: (e) => onChange(field.name, e.target.checked)
		})
	});
	if (field.type === "DATE") return /* @__PURE__ */ u(DateFieldInput, {
		field,
		isDisabled,
		onChange
	});
	return /* @__PURE__ */ u("div", {
		class: "IncodeCustomFieldsField",
		children: /* @__PURE__ */ u(InputComposed, {
			id,
			name: field.name,
			label: field.label,
			type: INPUT_TYPE_MAP[field.type] ?? "text",
			step: INPUT_STEP_MAP[field.type],
			disabled: isDisabled,
			onInput: (e) => onChange(field.name, e.target.value)
		})
	});
};
var CustomFieldsContent = ({ config, manager: externalManager, onFinish, onError: _onError }) => {
	const { t } = useTranslation();
	const [state, manager] = useManager(() => {
		if (externalManager) return externalManager;
		if (!config) throw new Error("CustomFields config is required");
		return createCustomFieldsManager({ config });
	}, {
		manageLifecycle: !externalManager,
		autoLoad: true
	});
	useModuleTerminalCallbacks({
		status: state.status === "finished" ? "finished" : "loading",
		onFinish
	});
	if (state.status === "success") return /* @__PURE__ */ u(StatusPage, {
		variant: "success",
		icon: /* @__PURE__ */ u(SuccessIcon, { size: 64 }),
		title: t("customFields.completed"),
		hideFooterBranding: true
	});
	if (state.status === "finished") return null;
	const title = state.status === "inputting" || state.status === "submitting" ? state.title ? t(state.title) : t("customFields.title") : "";
	if (state.status === "submitting") return /* @__PURE__ */ u(StatusPage, {
		variant: "loading",
		icon: /* @__PURE__ */ u(LoadingIcon, { size: 64 }),
		title: t("customFields.processing"),
		hideFooterBranding: true
	});
	const canSubmit = state.status === "inputting" ? state.canSubmit : false;
	const customFields = state.status === "inputting" ? state.customFields : [];
	const handleChange = (name, value) => {
		manager.setField(name, value);
	};
	return /* @__PURE__ */ u(Page, {
		className: "IncodeCustomFieldsPage",
		title,
		hideFooterBranding: true,
		children: /* @__PURE__ */ u("form", {
			class: "IncodeCustomFieldsForm",
			onSubmit: (e) => {
				e.preventDefault();
				manager.submit();
			},
			autoComplete: "off",
			children: [
				customFields.map((field) => /* @__PURE__ */ u(CustomFieldInput, {
					field,
					isDisabled: false,
					onChange: handleChange
				}, field.name)),
				/* @__PURE__ */ u(Spacer, {}),
				/* @__PURE__ */ u(Button, {
					type: "submit",
					disabled: !canSubmit,
					children: t("customFields.continue")
				})
			]
		})
	});
};
var CustomFields = ({ config, manager, onFinish, onError }) => {
	return /* @__PURE__ */ u(IncodeComponent, { children: config || manager ? /* @__PURE__ */ u(CustomFieldsContent, {
		config,
		manager,
		onFinish,
		onError
	}) : null });
};
registerIncodeModuleElement(CustomFields, "incode-custom-fields");
//#endregion
export { CustomFields };
