const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_index = require("../../../components/booking/notes/NoteBlock/index.js");
const require_perspectiveUtils = require("../hooks/perspectiveUtils.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
//#region src/plus/pisellSalesManagement/components/BookingNotesPreview.tsx
const normalizeInternalNotes = (value) => {
	if (!value) return [];
	if (Array.isArray(value)) return value.map((item, index) => {
		if (typeof item === "string" || typeof item === "number") return {
			id: index,
			content: String(item)
		};
		if (item && typeof item === "object") return item;
		return {
			id: index,
			content: String(item)
		};
	}).filter((item) => (item === null || item === void 0 ? void 0 : item.content) != null && (item === null || item === void 0 ? void 0 : item.content) !== "");
	if (typeof value === "string" || typeof value === "number") return [{
		id: 0,
		content: String(value)
	}];
	return [];
};
const BookingNotesPreview = (props) => {
	const { dataSource, labels, style } = props;
	const orderNote = typeof (dataSource === null || dataSource === void 0 ? void 0 : dataSource.note) === "string" ? dataSource.note.trim() : "";
	const internalNotes = (0, react.useMemo)(() => normalizeInternalNotes(dataSource === null || dataSource === void 0 ? void 0 : dataSource.internalNotes), [dataSource === null || dataSource === void 0 ? void 0 : dataSource.internalNotes]);
	const displayLines = (0, react.useMemo)(() => {
		const lines = [];
		if (orderNote) lines.push({
			label: (labels === null || labels === void 0 ? void 0 : labels.orderNote) || _pisell_utils.locales.getText("pisell2.salesManagement.booking.notes.orderNotes"),
			content: orderNote
		});
		internalNotes.forEach((item) => {
			const content = typeof (item === null || item === void 0 ? void 0 : item.content) === "string" ? item.content.trim() : "";
			if (!content) return;
			lines.push({
				label: (labels === null || labels === void 0 ? void 0 : labels.internalNote) || _pisell_utils.locales.getText("pisell2.salesManagement.booking.notes.internalNotes"),
				content
			});
		});
		return lines;
	}, [
		orderNote,
		internalNotes,
		labels === null || labels === void 0 ? void 0 : labels.orderNote,
		labels === null || labels === void 0 ? void 0 : labels.internalNote
	]);
	const visibleLines = displayLines.slice(0, 2);
	const showMore = displayLines.length > 0;
	const [modalOpen, setModalOpen] = (0, react.useState)(false);
	if (!displayLines.length) return null;
	const openNotesModal = (event) => {
		event.stopPropagation();
		setModalOpen(true);
	};
	const orderNotes = orderNote ? [{
		id: 0,
		content: orderNote
	}] : [];
	const isEditable = Boolean(dataSource === null || dataSource === void 0 ? void 0 : dataSource.apis);
	return /* @__PURE__ */ react.default.createElement("div", {
		style,
		onClick: (e) => e.stopPropagation()
	}, /* @__PURE__ */ react.default.createElement("div", {
		style: {
			display: "flex",
			flexDirection: "column",
			cursor: "pointer"
		},
		onClick: openNotesModal
	}, visibleLines.map((line, index) => /* @__PURE__ */ react.default.createElement("div", {
		key: `${line.label}-${index}`,
		style: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, require_perspectiveUtils.textStyles.text), {}, {
			fontSize: 14,
			whiteSpace: "nowrap",
			overflow: "hidden",
			textOverflow: "ellipsis"
		})
	}, /* @__PURE__ */ react.default.createElement("span", null, line.label, ":"), " ", /* @__PURE__ */ react.default.createElement("span", { style: { color: "#eaa96f" } }, line.content))), showMore ? /* @__PURE__ */ react.default.createElement("div", { style: {
		color: "#7f56d9",
		display: "flex",
		alignItems: "center"
	} }, (labels === null || labels === void 0 ? void 0 : labels.more) || _pisell_utils.locales.getText("pisell2.salesManagement.booking.view-more"), /* @__PURE__ */ react.default.createElement(_pisell_materials.Icon, {
		size: 20,
		type: "pisell2-chevron-right-double"
	})) : null), /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		open: modalOpen,
		title: _pisell_utils.locales.getText("pisell2.salesManagement.booking.notes.title"),
		width: 600,
		footer: null,
		onCancel: () => setModalOpen(false),
		destroyOnClose: true,
		bodyStyle: { overflowX: "hidden" }
	}, /* @__PURE__ */ react.default.createElement("div", { style: {
		display: "flex",
		flexDirection: "column",
		gap: 16
	} }, /* @__PURE__ */ react.default.createElement(require_index.default, {
		noteType: "order",
		maxLength: 1,
		title: `${_pisell_utils.locales.getText("pisell2.salesManagement.booking.notes.orderNotes")}:`,
		describe: "",
		notes: orderNotes,
		apis: dataSource === null || dataSource === void 0 ? void 0 : dataSource.apis,
		placeholder: _pisell_utils.locales.getText("pisell2.salesManagement.booking.notes.addOrderNotes"),
		isEdit: isEditable,
		onChange: dataSource === null || dataSource === void 0 ? void 0 : dataSource.onChange
	}), (internalNotes.length > 0 || isEditable) && /* @__PURE__ */ react.default.createElement(require_index.default, {
		maxLength: 99,
		noteType: "internal",
		title: `${_pisell_utils.locales.getText("pisell2.salesManagement.booking.notes.internalNotes")}:`,
		describe: _pisell_utils.locales.getText("pisell2.salesManagement.booking.notes.internalVisibleOnlyTip"),
		placeholder: _pisell_utils.locales.getText("pisell2.salesManagement.booking.notes.addInternalNotes"),
		notes: internalNotes,
		isEdit: isEditable,
		apis: dataSource === null || dataSource === void 0 ? void 0 : dataSource.apis,
		bookingId: dataSource === null || dataSource === void 0 ? void 0 : dataSource.bookingId,
		onChange: dataSource === null || dataSource === void 0 ? void 0 : dataSource.onChange
	}))));
};
//#endregion
exports.default = BookingNotesPreview;
