import { getLocale } from "../../../utils/locales.js";
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 locales_default from "../locales.js";
import { formatResource, getErrorList } from "./utilsByBooking.js";
import { findNextDuration, flexibleObj, request, sliceDayIntoFiveMinutes } from "../utils.js";
import { Context, Provider } from "../info/model.js";
import client_default from "../info/client/index.js";
import useEngineContext from "../../../hooks/useEngineContext.js";
import { getResourceList } from "./serve.js";
import CartClientCard from "./cartClientCard/index.js";
import TabPane from "../components/tabPane/index.js";
import Footer from "../components/footer/index.js";
import service_default from "./service/index.js";
import Pet from "../info/pet/index.js";
import DateCom, { updateServiceDate } from "../info/date/index.js";
import header from "../info/header/index.js";
import ReloadResourceModal from "../components/reloadResourceModal/index.js";
import Notes from "../info/notes/index.js";
import useCreateBookingPageHeader from "../hooks/useCreateBookingPageHeader.js";
import ContactInfoMode from "./service/contactInfoMode/index.js";
import React, { useContext, useEffect, useMemo, useRef } from "react";
import { Skeleton } from "antd";
import { locales, sendWarningLog } from "@pisell/utils";
import { Iconfont, useToast } from "@pisell/materials";
import classNames from "classnames";
import { useDebounceEffect } from "ahooks";
import "../info/index.less";
//#region src/components/booking/info2/index.tsx
const defaultModules = {
	client: 1,
	date: 1,
	service: 1,
	holder: 1,
	note: 1
};
locales.init(locales_default, getLocale());
const Info = Provider((props) => {
	var _modal$state2, _modal$state3, _modal$state5, _modal$state6, _modal$state7;
	const { style, globalState, modules = defaultModules, slot } = props;
	const modal = useContext(Context);
	const context = useEngineContext();
	request.setRequest(context.appHelper.utils.request);
	const params = useMemo(() => {
		var _modal$state;
		return ((_modal$state = modal.state) === null || _modal$state === void 0 || (_modal$state = _modal$state.bookingConfig) === null || _modal$state === void 0 || (_modal$state = _modal$state.config) === null || _modal$state === void 0 || (_modal$state = _modal$state.basic) === null || _modal$state === void 0 ? void 0 : _modal$state.appointment_subject) || {};
	}, [(_modal$state2 = modal.state) === null || _modal$state2 === void 0 || (_modal$state2 = _modal$state2.bookingConfig) === null || _modal$state2 === void 0 || (_modal$state2 = _modal$state2.config) === null || _modal$state2 === void 0 || (_modal$state2 = _modal$state2.basic) === null || _modal$state2 === void 0 ? void 0 : _modal$state2.appointment_subject]);
	const stateRef = useRef(globalState);
	stateRef.current = globalState;
	const { createHeader, isBookingCreatePage, isBookingCreatePageCreateMode } = useCreateBookingPageHeader(globalState);
	useEffect(() => {
		document.body.id = "body";
		if (props.setState) props.setState({ bookingFunctions: {
			formatResource,
			getErrorList,
			sliceDayIntoFiveMinutes,
			findNextDuration,
			flexibleObj,
			updateServiceDate
		} });
	}, []);
	useToast();
	useMemo(() => {
		var _context$appHelper, _context$appHelper$ge, _context$appHelper2, _context$appHelper2$i, _context$appHelper3;
		const isFranchisee = context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.businessUtils) === null || _context$appHelper === void 0 || (_context$appHelper$ge = _context$appHelper.getAppByModal) === null || _context$appHelper$ge === void 0 ? void 0 : _context$appHelper$ge.call(_context$appHelper, "franchisee");
		modal.dispatch({
			type: "setState",
			payload: _objectSpread2(_objectSpread2({ setGlobalState: props.setGlobalState }, globalState), {}, {
				isTerminal: 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$i = _context$appHelper2.isTerminal) === null || _context$appHelper2$i === void 0 ? void 0 : _context$appHelper2$i.call(_context$appHelper2),
				isFranchisee,
				timezone: context === null || context === void 0 || (_context$appHelper3 = context.appHelper) === null || _context$appHelper3 === void 0 || (_context$appHelper3 = _context$appHelper3.utils) === null || _context$appHelper3 === void 0 || (_context$appHelper3 = _context$appHelper3.store) === null || _context$appHelper3 === void 0 || (_context$appHelper3 = _context$appHelper3.getState()) === null || _context$appHelper3 === void 0 || (_context$appHelper3 = _context$appHelper3.global) === null || _context$appHelper3 === void 0 || (_context$appHelper3 = _context$appHelper3.globalConfig) === null || _context$appHelper3 === void 0 ? void 0 : _context$appHelper3.timezone
			})
		});
	}, [globalState]);
	const dropdownProps = useMemo(() => {
		return { buttonProps: {
			icon: /* @__PURE__ */ React.createElement(Iconfont, {
				type: "pisell2-switch-horizontal-01",
				size: 24,
				style: { color: "#667085" }
			}),
			size: "default"
		} };
	}, [modal.state]);
	const header$1 = useMemo(() => {
		if ((globalState === null || globalState === void 0 ? void 0 : globalState.renderType) === "eventBooking") return locales.getText("pisell2.text.enroll.detail");
		return (globalState === null || globalState === void 0 ? void 0 : globalState.bookingId) ? /* @__PURE__ */ React.createElement(header, {
			state: modal.state,
			dispatch: modal.dispatch
		}) : /* @__PURE__ */ React.createElement("div", {
			className: "",
			style: { height: 0 }
		});
	}, [
		globalState === null || globalState === void 0 ? void 0 : globalState.bookingId,
		modal.state,
		modal.dispatch,
		globalState === null || globalState === void 0 ? void 0 : globalState.renderType
	]);
	const notes = useMemo(() => {
		if (!modules.note) return null;
		return (globalState === null || globalState === void 0 ? void 0 : globalState.bookingId) ? globalState.renderType === "eventBooking" ? /* @__PURE__ */ React.createElement(Notes, null) : null : null;
	}, [
		modules.note,
		globalState === null || globalState === void 0 ? void 0 : globalState.bookingId,
		globalState.render,
		(_modal$state3 = modal.state) === null || _modal$state3 === void 0 || (_modal$state3 = _modal$state3.notes) === null || _modal$state3 === void 0 ? void 0 : _modal$state3.orderNote
	]);
	const currentDate = useMemo(() => {
		var _modal$state4;
		if ((_modal$state4 = modal.state) === null || _modal$state4 === void 0 || (_modal$state4 = _modal$state4.date) === null || _modal$state4 === void 0 ? void 0 : _modal$state4.value) return modal.state.date.value.format("YYYY-MM-DD");
		return null;
	}, [(_modal$state5 = modal.state) === null || _modal$state5 === void 0 || (_modal$state5 = _modal$state5.date) === null || _modal$state5 === void 0 || (_modal$state5 = _modal$state5.value) === null || _modal$state5 === void 0 ? void 0 : _modal$state5.format("YYYY-MM-DD")]);
	/**
	* @title: 获取资源列表数据
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-01-09 13:38
	*/
	const getResourcesData = function() {
		var _ref = _asyncToGenerator(function* () {
			let bookingResourceList = localStorage.getItem("bookingResourceList");
			let timeoutNumber = 0;
			if (bookingResourceList) {
				bookingResourceList = JSON.parse(bookingResourceList);
				if ((bookingResourceList === null || bookingResourceList === void 0 ? void 0 : bookingResourceList.date) === currentDate) {
					timeoutNumber = 200;
					modal.dispatch({
						type: "setService",
						payload: _objectSpread2(_objectSpread2({}, stateRef.current.service), {}, {
							resourcesOrigin: bookingResourceList.list || [],
							resourcesOriginMap: bookingResourceList.listMap
						})
					});
				}
			}
			return new Promise((resolve, reject) => {
				setTimeout(_asyncToGenerator(function* () {
					try {
						var _stateRef$current, _stateRef$current$map;
						let data = yield getResourceList({ date: currentDate }, stateRef === null || stateRef === void 0 || (_stateRef$current = stateRef.current) === null || _stateRef$current === void 0 || (_stateRef$current = _stateRef$current.service) === null || _stateRef$current === void 0 || (_stateRef$current = _stateRef$current.value) === null || _stateRef$current === void 0 || (_stateRef$current$map = _stateRef$current.map) === null || _stateRef$current$map === void 0 ? void 0 : _stateRef$current$map.call(_stateRef$current, (item) => item === null || item === void 0 ? void 0 : item.booking_id));
						try {
							data.date = currentDate;
						} catch (error) {
							sendWarningLog({
								title: `设置资源失败`,
								content: [{
									key: "error",
									value: JSON.stringify({
										message: error === null || error === void 0 ? void 0 : error.message,
										stack: error === null || error === void 0 ? void 0 : error.stack,
										name: error === null || error === void 0 ? void 0 : error.name
									})
								}]
							});
						}
						try {
							data.date = currentDate;
						} catch (err) {}
						modal.dispatch({
							type: "setService",
							payload: _objectSpread2(_objectSpread2({}, stateRef.current.service), {}, {
								resourcesOrigin: data.list || [],
								resourcesOriginMap: data.listMap,
								reloadResourceModal: false
							})
						});
						resolve(data);
					} catch (error) {
						sendWarningLog({
							title: `获取资源失败`,
							content: [{
								key: "error",
								value: JSON.stringify({
									message: error === null || error === void 0 ? void 0 : error.message,
									stack: error === null || error === void 0 ? void 0 : error.stack,
									name: error === null || error === void 0 ? void 0 : error.name
								})
							}]
						});
						modal.dispatch({
							type: "setService",
							payload: _objectSpread2(_objectSpread2({}, stateRef.current.service), {}, { reloadResourceModal: true })
						});
						reject(error);
					}
				}), timeoutNumber);
			});
		});
		return function getResourcesData() {
			return _ref.apply(this, arguments);
		};
	}();
	useDebounceEffect(() => {
		if (currentDate) getResourcesData();
	}, [currentDate], { wait: 300 });
	return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(ReloadResourceModal, {
		modal,
		onReload: getResourcesData,
		onCancel: () => {
			modal.dispatch({
				type: "setService",
				payload: _objectSpread2(_objectSpread2({}, stateRef.current.service), {}, { reloadResourceModal: false })
			});
		}
	}), !!modal.state.setGlobalState ? /* @__PURE__ */ React.createElement(TabPane, {
		style: { height: "100%" },
		header: header$1,
		footer: /* @__PURE__ */ React.createElement(Footer, {
			state: modal.state,
			dispatch: modal.dispatch,
			source: "info"
		}),
		bodyClassName: classNames("booking-info-warp", {
			["create-booking-page-info-wrap"]: isBookingCreatePage,
			["create-booking-page-info-wrap-create"]: isBookingCreatePageCreateMode
		}),
		state: modal.state,
		dispatch: modal.dispatch,
		source: "info"
	}, /* @__PURE__ */ React.createElement("div", { className: "booking-info" }, createHeader, /* @__PURE__ */ React.createElement("div", { className: "booking-info-client-line" }, modules.client ? (globalState === null || globalState === void 0 ? void 0 : globalState.isBookingTicket2) ? /* @__PURE__ */ React.createElement("div", { style: {
		flex: 1,
		overflow: "hidden"
	} }, /* @__PURE__ */ React.createElement(CartClientCard, { clientCardProps: { dropdown: dropdownProps } })) : /* @__PURE__ */ React.createElement(client_default, null) : null, (globalState === null || globalState === void 0 ? void 0 : globalState.isBookingTicket2) ? /* @__PURE__ */ React.createElement("div", {
		className: "booking-info-client-line-code",
		onClick: () => {
			var _globalState$onQrCode;
			globalState === null || globalState === void 0 || (_globalState$onQrCode = globalState.onQrCodeClick) === null || _globalState$onQrCode === void 0 || _globalState$onQrCode.call(globalState);
		}
	}, /* @__PURE__ */ React.createElement(Iconfont, {
		type: "pisell2-a-qr-code-02",
		style: {
			color: "#fff",
			fontSize: 42
		}
	})) : null), (modal === null || modal === void 0 || (_modal$state6 = modal.state) === null || _modal$state6 === void 0 || (_modal$state6 = _modal$state6.bookingConfig) === null || _modal$state6 === void 0 || (_modal$state6 = _modal$state6.config) === null || _modal$state6 === void 0 || (_modal$state6 = _modal$state6.cart_settings_tab) === null || _modal$state6 === void 0 || (_modal$state6 = _modal$state6.display_contact_info) === null || _modal$state6 === void 0 ? void 0 : _modal$state6.show_contact_info) === true ? /* @__PURE__ */ React.createElement(ContactInfoMode, null) : null, modules.holder && !((_modal$state7 = modal.state) === null || _modal$state7 === void 0 ? void 0 : _modal$state7.disabledEdit) ? /* @__PURE__ */ React.createElement(React.Fragment, null, params.type === "form" ? /* @__PURE__ */ React.createElement(Pet, null) : null) : null, modules.date ? /* @__PURE__ */ React.createElement(DateCom, null) : null, /* @__PURE__ */ React.createElement(Skeleton, {
		loading: globalState.loading,
		round: true,
		active: true,
		avatar: false,
		style: { paddingTop: 20 }
	}, modules.service ? /* @__PURE__ */ React.createElement(React.Fragment, null, modal.state.date.value ? /* @__PURE__ */ React.createElement(service_default, null) : null) : null, notes, slot ? slot : null))) : null);
});
//#endregion
export { Info as default };
