import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { isWalkIn } from "../utils.js";
import { formatAddFormValues } from "./format.js";
import { Provider } from "./model.js";
import Single from "./single/index.js";
import React, { useMemo } from "react";
import { locales } from "@pisell/utils";
import { useMemoizedFn } from "ahooks";
import "./index.less";
//#region src/components/booking/forms/forms.tsx
/**
* @title: 表单模块
* @description:
* @param {*} Provider
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-01-30 17:10
*/
const Forms = Provider((props) => {
	const { forms, value = [], modalOpen, onChange, action, customSelectProps, optionRender, hideTitle, addCallback, client, loading, addButtonProps, drawerProps, isMinorAndWaiver, waiverForm, addCallbackService, getHolders } = props;
	const onAction = useMemoizedFn(function() {
		var _ref = _asyncToGenerator(function* (formDataConfig, isAdd) {
			let _data = { formDataConfig: isMinorAndWaiver ? _objectSpread2(_objectSpread2({}, waiverForm), {}, {
				customer_id: client === null || client === void 0 ? void 0 : client.id,
				is_franchisor: formDataConfig.is_franchisor
			}) : _objectSpread2({}, formDataConfig) };
			if (formDataConfig.data_range === "customer" && !isWalkIn(client === null || client === void 0 ? void 0 : client.id)) _data.customerId = client.id;
			action({
				type: "pisell1.goCustomFormAddData",
				data: _data,
				callback: (res) => {
					if (isMinorAndWaiver) {
						addCallbackService === null || addCallbackService === void 0 || addCallbackService();
						getHolders === null || getHolders === void 0 || getHolders();
						return;
					}
					let obj = {};
					for (let item of res) obj[item.key] = item.value;
					let _val = [...value[formDataConfig.form_id] || []];
					_val.push(obj);
					if (isAdd) onValueChange(formDataConfig, _val);
					setTimeout(() => {
						addCallback === null || addCallback === void 0 || addCallback(formDataConfig, obj);
					}, 100);
				}
			});
		});
		return function(_x, _x2) {
			return _ref.apply(this, arguments);
		};
	}());
	const onValueChange = useMemoizedFn((form, val) => {
		onChange({
			values: _objectSpread2(_objectSpread2({}, value), {}, { [form.form_id]: formatAddFormValues({
				clientId: client === null || client === void 0 ? void 0 : client.id,
				value: val,
				data_range: form.data_range
			}) }),
			current: {
				field: form,
				value: val
			}
		});
	});
	const handleViewItem = useMemoizedFn((props) => {
		const { form, record } = props;
		let _data = {
			record,
			formDataConfig: form
		};
		if (form.data_range === "customer" && !isWalkIn(client === null || client === void 0 ? void 0 : client.id)) _data.customerId = client.id;
		action({
			type: "pisell1.goCustomFormDetail",
			data: _data,
			callback: (res) => {
				let cur = [...value[form.form_id] || []];
				if (res.type === "delete") cur = cur.filter((d) => d.form_record_id !== record.form_record_id);
				else {
					let obj = {};
					for (let item of res) obj[item.key] = item.value;
					cur = cur.map((d) => d.form_record_id === record.form_record_id ? obj : d);
				}
				onValueChange(form, cur);
			}
		});
	});
	return useMemo(() => {
		return /* @__PURE__ */ React.createElement(React.Fragment, null, (forms || []).map((d) => {
			var _d$form;
			return /* @__PURE__ */ React.createElement("div", {
				className: "pisell-lowcode__booking-field-wrap",
				style: { padding: hideTitle ? 0 : "0" },
				key: d.form_id
			}, hideTitle ? null : /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__booking-field-title" }, d.form.title), /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__booking-field-com" }, /* @__PURE__ */ React.createElement(Single, {
				modalOpen,
				form: d,
				watch: { value },
				mode: d.type,
				value: value[d.form_id] || [],
				onChange: (val) => {
					onValueChange(d, val);
				},
				drawerProps,
				onAdd: () => {
					onAction(d, true);
				},
				addButtonProps: _objectSpread2({
					loading,
					disabled: d.data_range === "customer" && isWalkIn(client === null || client === void 0 ? void 0 : client.id)
				}, addButtonProps),
				customSelectProps: _objectSpread2({
					handleViewItem: (record) => {
						handleViewItem({
							form: d,
							record
						});
					},
					handleCreateItem: () => {
						onAction(d, false);
					},
					createItemText: `${locales.getText("pisell2.text.create-new")} ${((_d$form = d.form) === null || _d$form === void 0 ? void 0 : _d$form.title) || "-"}`
				}, customSelectProps || {}),
				optionRender
			})));
		}));
	}, [
		forms,
		hideTitle,
		modalOpen,
		value,
		loading,
		client === null || client === void 0 ? void 0 : client.id,
		addButtonProps,
		customSelectProps,
		optionRender,
		drawerProps,
		addCallbackService
	]);
});
//#endregion
export { Forms as default };
