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 useEngineContext from "../../../hooks/useEngineContext.js";
import TabPane from "../components/tabPane/index.js";
import EmptyBooking from "../components/emptyBooking/index.js";
import { Context, Provider } from "./model.js";
import Forms$1 from "./forms.js";
import Footer from "./footer.js";
import React, { useContext, useEffect, useMemo, useState } from "react";
import { message } from "antd";
import { locales } from "@pisell/utils";
import "./index.less";
//#region src/components/booking/forms/index.tsx
const relation_type = "appointment_booking";
const Forms = Provider((props) => {
	var _context$appHelper, _state$bookingConfig2, _state$bookingDetail2, _state$client, _state$form;
	const { globalState } = props;
	const [loading, setLoading] = useState(true);
	const { state, dispatch } = useContext(Context);
	const context = useEngineContext();
	const { isTerminal } = ((_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {};
	const params = useMemo(() => {
		var _state$bookingConfig;
		return state === null || state === void 0 || (_state$bookingConfig = state.bookingConfig) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.config) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.basic) === null || _state$bookingConfig === void 0 ? void 0 : _state$bookingConfig.appointment_subject;
	}, [state === null || state === void 0 || (_state$bookingConfig2 = state.bookingConfig) === null || _state$bookingConfig2 === void 0 || (_state$bookingConfig2 = _state$bookingConfig2.config) === null || _state$bookingConfig2 === void 0 || (_state$bookingConfig2 = _state$bookingConfig2.basic) === null || _state$bookingConfig2 === void 0 ? void 0 : _state$bookingConfig2.appointment_subject]);
	useEffect(() => {
		var _state$bookingDetail;
		if (!!((_state$bookingDetail = state.bookingDetail) === null || _state$bookingDetail === void 0 || (_state$bookingDetail = _state$bookingDetail.bookings) === null || _state$bookingDetail === void 0 ? void 0 : _state$bookingDetail.id)) geForms();
	}, [(_state$bookingDetail2 = state.bookingDetail) === null || _state$bookingDetail2 === void 0 || (_state$bookingDetail2 = _state$bookingDetail2.bookings) === null || _state$bookingDetail2 === void 0 ? void 0 : _state$bookingDetail2.id, (_state$client = state.client) === null || _state$client === void 0 || (_state$client = _state$client.value) === null || _state$client === void 0 ? void 0 : _state$client.id]);
	const getFranchiseeCompositeData = function() {
		var _ref = _asyncToGenerator(function* ({ request, params }) {
			let arr = [];
			arr.push(request(params));
			if (state.isFranchisee) arr.push(request(_objectSpread2(_objectSpread2({}, params), {}, { source_shop_customer: 1 })));
			return yield Promise.all(arr);
		});
		return function getFranchiseeCompositeData(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	const getFranchiseeForms = function() {
		var _ref2 = _asyncToGenerator(function* (params) {
			const res = yield getFranchiseeCompositeData({
				request: state.apis.getForms,
				params
			});
			if (state.isFranchisee) {
				let arr = [];
				res.forEach((item, index) => {
					arr = arr.concat((item || []).map((d) => {
						return _objectSpread2(_objectSpread2({}, d), {}, { is_franchisor: index === 1 ? 1 : 0 });
					}));
				});
				return arr;
			}
			return res[0];
		});
		return function getFranchiseeForms(_x2) {
			return _ref2.apply(this, arguments);
		};
	}();
	const getFormDataById = function() {
		var _ref3 = _asyncToGenerator(function* (item) {
			var _state$client2;
			let _params = {
				form_id: item.form_id,
				skip: 1,
				num: 999,
				source_shop_customer: item.is_franchisor ? 1 : 0
			};
			let options = [];
			const isCustomerForm = item.data_range === "customer" && !isWalkIn((_state$client2 = state.client) === null || _state$client2 === void 0 || (_state$client2 = _state$client2.value) === null || _state$client2 === void 0 ? void 0 : _state$client2.id);
			if (item.data_range !== "customer" || isCustomerForm) {
				if (isCustomerForm) _params.customer_id = state.client.value.id;
				const data = yield state.apis.getPetsData(_params);
				options = (data === null || data === void 0 ? void 0 : data.list) || [];
			}
			return _objectSpread2(_objectSpread2({}, item), {}, { options });
		});
		return function getFormDataById(_x3) {
			return _ref3.apply(this, arguments);
		};
	}();
	const getFormData = function() {
		var _ref4 = _asyncToGenerator(function* (formList = []) {
			let all = [];
			all = formList.map((d) => getFormDataById(d));
			return Promise.all(all);
		});
		return function getFormData() {
			return _ref4.apply(this, arguments);
		};
	}();
	const getFormValues = (data) => {
		let _values = {};
		for (let item of data) _values[item.form_id] = item.form_record;
		return _values;
	};
	const geForms = function() {
		var _ref5 = _asyncToGenerator(function* () {
			setLoading(true);
			try {
				let data = yield getFranchiseeForms({
					relation_type,
					relation_id: state.bookingDetail.bookings.id
				});
				data = data || [];
				if (params.type === "form") data = data.filter((d) => d.form_id !== (params === null || params === void 0 ? void 0 : params.form_id));
				console.log("datadata", data);
				dispatch({
					type: "setForm",
					payload: {
						forms: yield getFormData(data),
						value: getFormValues(data)
					}
				});
				setLoading(false);
			} catch (err) {
				setLoading(false);
				console.log("form_error", err);
			}
		});
		return function geForms() {
			return _ref5.apply(this, arguments);
		};
	}();
	useEffect(() => {
		document.body.id = "body";
	}, []);
	useEffect(() => {
		dispatch({
			type: "setState",
			payload: { setGlobalState: props.setGlobalState }
		});
	}, []);
	useEffect(() => {
		var _context$appHelper2, _context$appHelper2$g;
		const isFranchisee = context === null || context === void 0 || (_context$appHelper2 = context.appHelper) === null || _context$appHelper2 === void 0 || (_context$appHelper2 = _context$appHelper2.utils) === null || _context$appHelper2 === void 0 || (_context$appHelper2 = _context$appHelper2.businessUtils) === null || _context$appHelper2 === void 0 || (_context$appHelper2$g = _context$appHelper2.getAppByModal) === null || _context$appHelper2$g === void 0 ? void 0 : _context$appHelper2$g.call(_context$appHelper2, "franchisee");
		dispatch({
			type: "setState",
			payload: _objectSpread2(_objectSpread2({}, globalState), {}, { isFranchisee })
		});
	}, [globalState]);
	const saveBookingFormData = function() {
		var _ref6 = _asyncToGenerator(function* (params) {
			try {
				yield state.apis.saveBookingFormData(params);
				message.success(locales.getText("pisell2.text.forms") + locales.getText("pisell2.text.updated"));
				state === null || state === void 0 || state.callback({
					order_id: state.bookingId,
					afterCloseUpdate: true
				});
			} catch (err) {}
		});
		return function saveBookingFormData(_x4) {
			return _ref6.apply(this, arguments);
		};
	}();
	const onValueChange = (data) => {
		const { values, current } = data;
		dispatch({
			type: "setForm",
			payload: { value: values }
		});
		let _arr = current.value || [];
		saveBookingFormData({
			relation_type,
			relation_id: state.bookingDetail.bookings.id,
			form_id: current.field.form_id,
			form_record_ids: _arr.map((d) => d.form_record_id)
		});
	};
	const addCallback = function() {
		var _ref7 = _asyncToGenerator(function* (form) {
			let _data = [...state.form.forms];
			const data = yield getFormDataById(form);
			_data = _data.map((d) => d.form_id === form.form_id ? data : d);
			dispatch({
				type: "setForm",
				payload: { forms: _data }
			});
		});
		return function addCallback(_x5) {
			return _ref7.apply(this, arguments);
		};
	}();
	return /* @__PURE__ */ React.createElement(React.Fragment, null, !!state.setGlobalState ? /* @__PURE__ */ React.createElement(TabPane, {
		header: locales.getText("pisell2.text.forms"),
		loading: loading && globalState.bookingId,
		bodyClassName: "booking-forms-body",
		footer: state.bookingId && (state === null || state === void 0 || (_state$form = state.form) === null || _state$form === void 0 ? void 0 : _state$form.forms.length) && /* @__PURE__ */ React.createElement(Footer, {
			state: globalState,
			reload: geForms
		})
	}, !globalState.bookingId ? /* @__PURE__ */ React.createElement(EmptyBooking, null) : /* @__PURE__ */ React.createElement(Forms$1, {
		forms: state.form.forms,
		action: state.action,
		modalOpen: state.modalOpen,
		value: state.form.value,
		onChange: onValueChange,
		addCallback,
		client: state.client.value,
		customSelectProps: { autoFocus: !state.isTerminal }
	})) : null);
});
//#endregion
export { Forms as default };
