const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_useEngineContext = require("../../../hooks/useEngineContext.js");
const require_forms = require("../../../components/booking/forms/forms.js");
const require_serve = require("../serve.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let ahooks = require("ahooks");
//#region src/plus/pisellSalesManagement/components/BookingFormsPreview.tsx
const BookingFormsPreview = (props) => {
	var _context$appHelper;
	const context = require_useEngineContext.default();
	const utils = (context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {};
	const { value, uniqueTables, bookingLabels, textStyles, relationId, relationType, customer } = props;
	const appointmentForms = Array.isArray(value) ? value : [];
	const [modalOpen, setModalOpen] = (0, react.useState)(false);
	const [activeTable, setActiveTable] = (0, react.useState)(null);
	const _getRelationRecords = (0, ahooks.useRequest)(require_serve.getRelationRecords, { manual: true });
	const [hasRequested, setHasRequested] = (0, react.useState)(false);
	const [tooltipOpen, setTooltipOpen] = (0, react.useState)(false);
	const countsByFormId = (0, react.useMemo)(() => {
		return appointmentForms.reduce((result, item) => {
			const formId = item === null || item === void 0 ? void 0 : item.form_id;
			if (formId != null) {
				const key = String(formId);
				result[key] = (result[key] || 0) + 1;
			}
			return result;
		}, {});
	}, [appointmentForms]);
	const tableItems = (0, react.useMemo)(() => {
		return (uniqueTables || []).map((t) => {
			var _t$form, _t$form2;
			const baseTitle = (t === null || t === void 0 || (_t$form = t.form) === null || _t$form === void 0 || (_t$form = _t$form.setting) === null || _t$form === void 0 || (_t$form = _t$form.title) === null || _t$form === void 0 ? void 0 : _t$form.original) || (t === null || t === void 0 || (_t$form2 = t.form) === null || _t$form2 === void 0 ? void 0 : _t$form2.title) || "";
			if (!baseTitle) return null;
			const formId = t === null || t === void 0 ? void 0 : t.form_id;
			const count = formId != null ? countsByFormId[String(formId)] || 0 : 0;
			return {
				title: `${baseTitle} (${count})`,
				baseTitle,
				formId,
				count,
				table: t
			};
		}).filter((t) => t);
	}, [uniqueTables, countsByFormId]);
	const showAll = tableItems.length <= 3;
	const displayItems = showAll ? tableItems : tableItems.slice(0, 3);
	const moreCount = tableItems.length - displayItems.length;
	const handleOpenModal = (0, react.useCallback)((item, event) => {
		event.stopPropagation();
		setActiveTable(item);
		setModalOpen(true);
		setTooltipOpen(false);
		if (!_getRelationRecords.loading && !hasRequested) {
			setHasRequested(true);
			_getRelationRecords.run({
				relation_type: relationType || "appointment_booking",
				relation_id: relationId
			});
		}
	}, [
		_getRelationRecords,
		relationId,
		hasRequested,
		relationType
	]);
	const relationRecords = (0, react.useMemo)(() => {
		return Array.isArray(_getRelationRecords.data) ? _getRelationRecords.data : [];
	}, [_getRelationRecords.data]);
	const selectedForm = (0, react.useMemo)(() => {
		if (!(activeTable === null || activeTable === void 0 ? void 0 : activeTable.formId)) return null;
		return relationRecords.find((item) => String(item === null || item === void 0 ? void 0 : item.form_id) === String(activeTable === null || activeTable === void 0 ? void 0 : activeTable.formId));
	}, [activeTable, relationRecords]);
	if (tableItems.length === 0) return /* @__PURE__ */ react.default.createElement("div", { style: {
		minWidth: 0,
		maxWidth: "100%",
		textAlign: "center"
	} }, "-");
	const tooltipContent = /* @__PURE__ */ react.default.createElement("div", { style: {
		display: "flex",
		flexDirection: "column",
		gap: 10
	} }, tableItems.map((item, idx) => {
		const clickable = item.count > 0;
		return /* @__PURE__ */ react.default.createElement("div", {
			key: `${item.title}-${idx}`,
			style: {
				fontSize: 12,
				color: clickable ? "#7f56d9" : "#98A2B3",
				textDecoration: clickable ? "underline" : "none",
				whiteSpace: "nowrap",
				lineHeight: "18px",
				cursor: clickable ? "pointer" : "default"
			},
			onClick: (event) => {
				if (!clickable) return;
				handleOpenModal(item, event);
			}
		}, item.title);
	}));
	return /* @__PURE__ */ react.default.createElement("div", {
		onClick: (event) => event.stopPropagation(),
		style: {
			minWidth: 0,
			maxWidth: "100%",
			overflow: "hidden",
			boxSizing: "border-box"
		}
	}, showAll ? /* @__PURE__ */ react.default.createElement("div", { style: {
		display: "flex",
		flexDirection: "column"
	} }, displayItems.map((item, idx) => /* @__PURE__ */ react.default.createElement("div", {
		key: `${item.title}-${idx}`,
		style: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, (textStyles === null || textStyles === void 0 ? void 0 : textStyles.text) || {}), {}, {
			color: "#7f56d9",
			textDecoration: "underline",
			cursor: "pointer",
			textAlign: "left",
			overflow: "hidden",
			textOverflow: "ellipsis",
			whiteSpace: "nowrap"
		}),
		onClick: (event) => handleOpenModal(item, event)
	}, item.title))) : /* @__PURE__ */ react.default.createElement("div", { style: {
		display: "flex",
		flexDirection: "column"
	} }, displayItems.map((item, idx) => /* @__PURE__ */ react.default.createElement("div", {
		key: `${item.title}-${idx}`,
		style: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, (textStyles === null || textStyles === void 0 ? void 0 : textStyles.text) || {}), {}, {
			color: "#475467",
			textDecoration: "none",
			cursor: "default",
			textAlign: "left",
			overflow: "hidden",
			textOverflow: "ellipsis",
			whiteSpace: "nowrap"
		})
	}, item.title)), moreCount > 0 ? /* @__PURE__ */ react.default.createElement(antd.Tooltip, {
		title: tooltipContent,
		open: modalOpen ? false : tooltipOpen,
		onOpenChange: (nextOpen) => {
			if (!modalOpen) setTooltipOpen(nextOpen);
		}
	}, /* @__PURE__ */ react.default.createElement("div", { style: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, (textStyles === null || textStyles === void 0 ? void 0 : textStyles.text) || {}), {}, {
		color: "#7f56d9",
		textDecoration: "underline",
		cursor: "default",
		alignSelf: "flex-start"
	}) }, `+${moreCount} ${_pisell_utils.locales.getText("pisell2.salesManagement.booking.common.more")}`)) : null), /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		open: modalOpen,
		title: bookingLabels === null || bookingLabels === void 0 ? void 0 : bookingLabels.forms,
		width: 600,
		footer: null,
		destroyOnClose: true,
		onCancel: () => setModalOpen(false)
	}, _getRelationRecords.loading || !hasRequested ? /* @__PURE__ */ react.default.createElement("div", { style: {
		display: "flex",
		justifyContent: "center",
		alignItems: "center",
		padding: "48px 0"
	} }, /* @__PURE__ */ react.default.createElement(antd.Spin, null)) : selectedForm ? /* @__PURE__ */ react.default.createElement(require_forms.default, {
		forms: [selectedForm],
		value: { [selectedForm.form_id]: selectedForm.form_record || [] },
		onChange: () => {},
		addCallback: () => {},
		client: customer,
		action: utils.action
	}) : null));
};
//#endregion
exports.default = BookingFormsPreview;
