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 CompensatedPisellContainer from "../../../../pro/compensatedPisellContainer/index.js";
import withResponsive from "../../../../responsive/withResponsive.js";
import NoteItem from "./NoteItem/index.js";
import AddNote from "../addNote/index.js";
import React, { useMemo, useState } from "react";
import { Button, Skeleton, message } from "antd";
import { locales } from "@pisell/utils";
import { useRequest } from "ahooks";
import "./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] = useState(false);
	const [value, setValue] = useState("");
	const [editNote, setEditNote] = useState(null);
	const { title, describe, isEdit, notes, loading, maxLength = 1, noteType, variant } = props;
	const isPhoneVariant = variant === "phone";
	const _add = useRequest((_props$apis = props.apis) === null || _props$apis === void 0 ? void 0 : _props$apis.addNotesByOrder, {
		manual: true,
		onSuccess: (res) => {
			var _props$onChange;
			message.success(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 = useRequest((_props$apis2 = props.apis) === null || _props$apis2 === void 0 ? void 0 : _props$apis2.removeNoteByOrder, {
		manual: true,
		onSuccess: () => {
			message.success(locales.getText("pisell2.text.status.remove-note-successfully"));
			handleCancel();
		}
	});
	const _edit = useRequest((_props$apis3 = props.apis) === null || _props$apis3 === void 0 ? void 0 : _props$apis3.editNoteByOrder, {
		manual: true,
		onSuccess: (res) => {
			var _props$onChange2;
			message.success(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 = useRequest((_props$apis4 = props.apis) === null || _props$apis4 === void 0 ? void 0 : _props$apis4.editOrderNote, {
		manual: true,
		onSuccess: (res, param) => {
			var _props$onChange3;
			message.success(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, _objectSpread2(_objectSpread2({}, _item), {}, { content: "" }), "remove", noteType);
		}
		if (type === "edit") {
			setEditNote(res);
			handleOpen(res.content);
		}
	};
	const handleOk = function() {
		var _ref = _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 = useMemo(() => {
		return notes.length < maxLength;
	}, [notes.length, maxLength]);
	return /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__booking-notes-wrap" }, /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__booking-notes-title" }, /* @__PURE__ */ React.createElement("span", { className: "pisell-lowcode__booking-notes-title-text" }, title)), /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__booking-notes-describe" }, describe), /* @__PURE__ */ React.createElement(Skeleton, { loading }, notes.map((d) => {
		return /* @__PURE__ */ React.createElement(NoteItem, {
			key: d.id,
			isEdit: !!isEdit,
			item: d,
			onChange: _noteChange
		});
	})), /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement(CompensatedPisellContainer, {
		renderType: isPhoneVariant ? "drawer" : "modal",
		props: isPhoneVariant ? {
			title: editNote ? locales.getText("pisell2.text.status.edit-note") : locales.getText("pisell2.text.status.add-note"),
			open: isModalOpen,
			placement: "bottom",
			width: "100%",
			height: "auto",
			onClose: handleCancel,
			footer: /* @__PURE__ */ React.createElement("div", { style: {
				display: "flex",
				gap: 12
			} }, /* @__PURE__ */ React.createElement(Button, {
				style: { flex: 1 },
				onClick: handleCancel
			}, locales.getText("pisell2.text.cancel")), /* @__PURE__ */ React.createElement(Button, {
				style: { flex: 1 },
				type: "primary",
				loading: _add.loading || _edit.loading || _editOrderNote.loading,
				disabled: noteType === "internal" ? !value : false,
				onClick: handleOk
			}, locales.getText("pisell2.text.confirm")))
		} : {
			title: editNote ? locales.getText("pisell2.text.status.edit-note") : 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.createElement(AddNote, {
		title,
		value,
		placeholder: props.placeholder,
		onChange: (e) => {
			setValue(e);
		},
		wait: 10
	})), isAdd ? /* @__PURE__ */ React.createElement(Button, {
		size: "large",
		className: "pisell-lowcode__booking-notes-add",
		onClick: () => {
			handleOpen("");
		}
	}, locales.getText("pisell2.text.add-notes")) : null));
};
NoteBlock.displayName = "NoteBlock";
var NoteBlock_default = withResponsive(NoteBlock);
//#endregion
export { NoteBlock_default as default };
