const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_index = require("./NoteItem/index.js");
const require_index$1 = require("../addNote/index.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");
require("./index.less");
//#region src/components/booking/notes/NoteBlock/index.tsx
const NoteBlock = (props) => {
	var _props$apis, _props$apis2, _props$apis3, _props$apis4;
	const [isModalOpen, setIsModalOpen] = (0, react.useState)(false);
	const [value, setValue] = (0, react.useState)("");
	const [editNote, setEditNote] = (0, react.useState)(null);
	const { title, describe, isEdit, notes, loading, maxLength = 1, noteType } = props;
	const _add = (0, ahooks.useRequest)((_props$apis = props.apis) === null || _props$apis === void 0 ? void 0 : _props$apis.addNotesByOrder, {
		manual: true,
		onSuccess: (res) => {
			var _props$onChange;
			antd.message.success(_pisell_utils.locales.getText("pisell2.text.status.add-note-successfully"));
			(_props$onChange = props.onChange) === null || _props$onChange === void 0 || _props$onChange.call(props, res, "add", noteType);
			handleCancel();
		}
	});
	const _remove = (0, ahooks.useRequest)((_props$apis2 = props.apis) === null || _props$apis2 === void 0 ? void 0 : _props$apis2.removeNoteByOrder, {
		manual: true,
		onSuccess: () => {
			antd.message.success(_pisell_utils.locales.getText("pisell2.text.status.remove-note-successfully"));
			handleCancel();
		}
	});
	const _edit = (0, ahooks.useRequest)((_props$apis3 = props.apis) === null || _props$apis3 === void 0 ? void 0 : _props$apis3.editNoteByOrder, {
		manual: true,
		onSuccess: (res) => {
			var _props$onChange2;
			antd.message.success(_pisell_utils.locales.getText("pisell2.text.status.edit-note-successfully"));
			(_props$onChange2 = props.onChange) === null || _props$onChange2 === void 0 || _props$onChange2.call(props, res, "edit", noteType);
			handleCancel();
		},
		onError: (err) => {}
	});
	const _editOrderNote = (0, ahooks.useRequest)((_props$apis4 = props.apis) === null || _props$apis4 === void 0 ? void 0 : _props$apis4.editOrderNote, {
		manual: true,
		onSuccess: (res, param) => {
			var _props$onChange3;
			antd.message.success(_pisell_utils.locales.getText("pisell2.text.status.edit-note-successfully"));
			(_props$onChange3 = props.onChange) === null || _props$onChange3 === void 0 || _props$onChange3.call(props, {
				id: 0,
				content: param[0].note
			}, "edit", noteType);
			handleCancel();
		},
		onError: (err) => {}
	});
	const _noteChange = (res, type) => {
		if (type === "remove") {
			var _props$onChange4;
			let _item = { id: res.id };
			if (res.id !== 0) _remove.run(_item);
			else _editOrderNote.run({ note: "" });
			(_props$onChange4 = props.onChange) === null || _props$onChange4 === void 0 || _props$onChange4.call(props, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, _item), {}, { content: "" }), "remove", noteType);
		}
		if (type === "edit") {
			setEditNote(res);
			handleOpen(res.content);
		}
	};
	const handleOk = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
			if (noteType === "internal") {
				if (editNote) {
					let val = {
						id: editNote.id,
						content: value
					};
					_edit.run(val);
				} else _add.run({
					schedule_event_id: props.bookingId,
					content: value
				});
				return;
			}
			if (noteType === "order") {
				_editOrderNote.run({ note: value });
				return;
			}
		});
		return function handleOk() {
			return _ref.apply(this, arguments);
		};
	}();
	const handleOpen = (val) => {
		setValue(val || "");
		setIsModalOpen(true);
	};
	const handleCancel = () => {
		setValue("");
		setIsModalOpen(false);
		setEditNote(null);
	};
	const isAdd = (0, react.useMemo)(() => {
		return notes.length < maxLength;
	}, [notes.length, maxLength]);
	return /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__booking-notes-wrap" }, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__booking-notes-title" }, /* @__PURE__ */ react.default.createElement("span", { className: "pisell-lowcode__booking-notes-title-text" }, title)), /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__booking-notes-describe" }, describe), /* @__PURE__ */ react.default.createElement(antd.Skeleton, { loading }, notes.map((d) => {
		return /* @__PURE__ */ react.default.createElement(require_index.default, {
			key: d.id,
			isEdit: !!isEdit,
			item: d,
			onChange: _noteChange
		});
	})), /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		title: editNote ? _pisell_utils.locales.getText("pisell2.text.status.edit-note") : _pisell_utils.locales.getText("pisell2.text.status.add-note"),
		open: isModalOpen,
		onOk: handleOk,
		confirmLoading: _add.loading || _edit.loading || _editOrderNote.loading,
		okButtonProps: { disabled: noteType === "internal" ? !value : false },
		onCancel: () => {
			handleCancel();
		}
	}, /* @__PURE__ */ react.default.createElement(require_index$1.default, {
		title,
		value,
		placeholder: props.placeholder,
		onChange: (e) => {
			setValue(e);
		},
		wait: 10
	})), isAdd ? /* @__PURE__ */ react.default.createElement(antd.Button, {
		size: "large",
		className: "pisell-lowcode__booking-notes-add",
		onClick: () => {
			handleOpen("");
		}
	}, _pisell_utils.locales.getText("pisell2.text.add-notes")) : null));
};
//#endregion
exports.default = NoteBlock;
