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 TabPane from "../components/tabPane/index.js";
import EmptyBooking from "../components/emptyBooking/index.js";
import NoteBlock from "./NoteBlock/index.js";
import { Context, Provider } from "./model.js";
import React, { useContext, useEffect, useMemo, useState } from "react";
import { locales } from "@pisell/utils";
import "./index.less";
//#region src/components/booking/notes/index.tsx
const Notes = Provider((props) => {
	var _modal$state5, _modal$state6, _modal$state$bookingD2;
	const { globalState } = props;
	const modal = useContext(Context);
	const [internalLoading, setInternalLoading] = useState(false);
	const getNotes = function() {
		var _ref = _asyncToGenerator(function* () {
			var _modal$state$bookingD;
			setInternalLoading(true);
			const data = yield modal.state.apis.getNotesByOrder({
				schedule_event_id: (_modal$state$bookingD = modal.state.bookingDetail) === null || _modal$state$bookingD === void 0 || (_modal$state$bookingD = _modal$state$bookingD.bookings) === null || _modal$state$bookingD === void 0 ? void 0 : _modal$state$bookingD.id,
				skip: 1,
				num: 99
			});
			modal.dispatch({
				type: "setNotes",
				payload: { internalNotes: (data === null || data === void 0 ? void 0 : data.list) || [] }
			});
			setInternalLoading(false);
		});
		return function getNotes() {
			return _ref.apply(this, arguments);
		};
	}();
	useEffect(() => {
		if (modal.state.bookingDetail) getNotes();
	}, [modal.state.bookingDetail]);
	useEffect(() => {
		document.body.id = "body";
	}, []);
	useEffect(() => {
		modal.dispatch({
			type: "setState",
			payload: _objectSpread2({ setGlobalState: props.setGlobalState }, globalState)
		});
	}, [globalState]);
	const noteChange = ({ note, status, noteType }) => {
		var _modal$state, _modal$state$refreshD;
		(_modal$state = modal.state) === null || _modal$state === void 0 || (_modal$state$refreshD = _modal$state.refreshData) === null || _modal$state$refreshD === void 0 || _modal$state$refreshD.call(_modal$state, {});
		if (noteType === "internal") {
			var _modal$state2;
			let _list = [...(_modal$state2 = modal.state) === null || _modal$state2 === void 0 ? void 0 : _modal$state2.notes.internalNotes];
			if (status == "add") _list.push(note);
			if (status == "remove") _list = _list.filter((item) => item.id != note.id);
			if (status == "edit") _list = _list.map((item) => {
				if (item.id == note.id) return note;
				return item;
			});
			modal.dispatch({
				type: "setNotes",
				payload: { internalNotes: _list }
			});
			return;
		}
		if (noteType === "order") modal.dispatch({
			type: "setNotes",
			payload: {
				orderNote: note.content || "",
				edit: false
			}
		});
	};
	const customerNote = useMemo(() => {
		var _bookingDetail$custom;
		const { bookingDetail } = modal.state;
		return [{
			id: 0,
			content: (bookingDetail === null || bookingDetail === void 0 || (_bookingDetail$custom = bookingDetail.customer) === null || _bookingDetail$custom === void 0 ? void 0 : _bookingDetail$custom.note) || ""
		}];
	}, [modal.state.bookingDetail]);
	const orderNote = useMemo(() => {
		var _modal$state3, _modal$state4;
		if (!((_modal$state3 = modal.state) === null || _modal$state3 === void 0 || (_modal$state3 = _modal$state3.notes) === null || _modal$state3 === void 0 ? void 0 : _modal$state3.orderNote)) return [];
		return [{
			id: 0,
			content: ((_modal$state4 = modal.state) === null || _modal$state4 === void 0 || (_modal$state4 = _modal$state4.notes) === null || _modal$state4 === void 0 ? void 0 : _modal$state4.orderNote) || ""
		}];
	}, [(_modal$state5 = modal.state) === null || _modal$state5 === void 0 || (_modal$state5 = _modal$state5.notes) === null || _modal$state5 === void 0 ? void 0 : _modal$state5.orderNote]);
	return /* @__PURE__ */ React.createElement(React.Fragment, null, !!modal.state.setGlobalState && /* @__PURE__ */ React.createElement(TabPane, { header: locales.getText("pisell2.text.notes") }, !globalState.bookingId ? /* @__PURE__ */ React.createElement(EmptyBooking, null) : /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(NoteBlock, {
		maxLength: 0,
		noteType: "customer",
		title: `${locales.getText("pisell2.text.status.notes-for-customer")}:`,
		describe: locales.getText("pisell2.text.status.visible-only-to-your-team-members"),
		notes: customerNote,
		isEdit: false
	}), /* @__PURE__ */ React.createElement(NoteBlock, {
		noteType: "order",
		maxLength: 1,
		title: `${locales.getText("pisell2.text.order-notes")}:`,
		describe: "",
		notes: orderNote,
		apis: modal.state.apis,
		placeholder: locales.getText("pisell2.text.add-order-notes"),
		isEdit: true,
		onChange: (e, type, noteType) => {
			noteChange({
				note: e,
				status: type,
				noteType
			});
		}
	}), /* @__PURE__ */ React.createElement(NoteBlock, {
		maxLength: 99,
		noteType: "internal",
		loading: internalLoading,
		title: `${locales.getText("pisell2.text.status.internal-notes")}:`,
		describe: locales.getText("pisell2.text.status.visible-only-to-your-team-members"),
		placeholder: locales.getText("pisell2.text.add-internal-notes"),
		notes: ((_modal$state6 = modal.state) === null || _modal$state6 === void 0 ? void 0 : _modal$state6.notes.internalNotes) || [],
		isEdit: true,
		apis: modal.state.apis,
		bookingId: (_modal$state$bookingD2 = modal.state.bookingDetail) === null || _modal$state$bookingD2 === void 0 || (_modal$state$bookingD2 = _modal$state$bookingD2.bookings) === null || _modal$state$bookingD2 === void 0 ? void 0 : _modal$state$bookingD2.id,
		onChange: (e, type, noteType) => {
			noteChange({
				note: e,
				status: type,
				noteType
			});
		}
	}))));
});
//#endregion
export { Notes as default };
