const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_useEngineContext = require("../../hooks/useEngineContext.js");
const require_locales = require("./locales.js");
const require_AddTempModal = require("./AddTempModal.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let _pisell_icon_es_Delete_wght300_24px = require("@pisell/icon/es/Delete_wght300_24px");
_pisell_icon_es_Delete_wght300_24px = require_runtime.__toESM(_pisell_icon_es_Delete_wght300_24px);
require("./index.less");
//#region src/plus/noteModal/index.tsx
const { TextArea } = _pisell_materials.Input;
const STORAGE_KEY = "pisell2_note_templates";
const NoteModalContent = ({ open = false, title = "", value = "", onConfirm, onDelete, onCancel, zIndex }) => {
	var _context$engine;
	const context = require_useEngineContext.default();
	_pisell_utils.locales.init(require_locales.default, (context === null || context === void 0 || (_context$engine = context.engine) === null || _context$engine === void 0 || (_context$engine = _context$engine.props) === null || _context$engine === void 0 ? void 0 : _context$engine.locale) || "en");
	const [noteValue, setNoteValue] = (0, react.useState)(value);
	const [templates, setTemplates] = (0, react.useState)([]);
	const [templateModalOpen, setTemplateModalOpen] = (0, react.useState)(false);
	const inputRef = (0, react.useRef)(null);
	(0, react.useEffect)(() => {
		if (open) {
			const storedTemplates = localStorage.getItem(STORAGE_KEY);
			if (storedTemplates) try {
				setTemplates(JSON.parse(storedTemplates));
			} catch (error) {
				console.error("Failed to parse templates from localStorage:", error);
				setTemplates([]);
			}
			setTimeout(() => {
				var _inputRef$current;
				(_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 || _inputRef$current.focus();
			}, 100);
		}
	}, [open]);
	const saveTemplates = (newTemplates) => {
		localStorage.setItem(STORAGE_KEY, JSON.stringify(newTemplates));
		setTemplates(newTemplates);
	};
	const handleTemplateClick = (template) => {
		setNoteValue((prev) => (prev || "") + " " + (template || ""));
	};
	const handleConfirm = () => {
		onConfirm === null || onConfirm === void 0 || onConfirm(noteValue);
	};
	const handleDelete = () => {
		setNoteValue("");
		onDelete === null || onDelete === void 0 || onDelete();
	};
	const handleCancel = () => {
		setNoteValue("");
		onCancel === null || onCancel === void 0 || onCancel();
	};
	const onAddTemplate = () => {
		setTemplateModalOpen(true);
	};
	const onAddTemplateConfirm = (newTemplate) => {
		if (newTemplate.trim()) {
			saveTemplates([...templates, newTemplate.trim()]);
			setTemplateModalOpen(false);
		}
	};
	const onAddTemplateCancel = () => {
		setTemplateModalOpen(false);
	};
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		open,
		title: title || _pisell_utils.locales.getText("pisell2.text.note.modal.title"),
		headerDivider: false,
		onCancel: handleCancel,
		footer: null,
		width: 680,
		centered: true,
		destroyOnClose: true,
		zIndex
	}, /* @__PURE__ */ react.default.createElement("div", { className: "note-modal-content" }, /* @__PURE__ */ react.default.createElement("div", { className: "template-section" }, /* @__PURE__ */ react.default.createElement("div", {
		onClick: onAddTemplate,
		className: "add-template-btn"
	}, _pisell_utils.locales.getText("pisell2.text.note.modal.btn.add.template")), /* @__PURE__ */ react.default.createElement("div", {
		className: "note-modal-templates-container",
		onTouchMove: (e) => e.stopPropagation()
	}, /* @__PURE__ */ react.default.createElement("div", { className: "note-modal-templates-scroll" }, templates.map((template, index) => /* @__PURE__ */ react.default.createElement("div", {
		key: index,
		style: {
			display: "inline-block",
			marginLeft: "8px"
		}
	}, /* @__PURE__ */ react.default.createElement("div", {
		className: "note-modal-template-tag",
		onClick: () => handleTemplateClick(template)
	}, template)))))), /* @__PURE__ */ react.default.createElement("div", { className: "input-section" }, /* @__PURE__ */ react.default.createElement(TextArea, {
		value: noteValue,
		onChange: (e) => setNoteValue(e.target.value),
		placeholder: _pisell_utils.locales.getText("pisell2.text.note.modal.placeholder"),
		autoSize: {
			minRows: 4,
			maxRows: 8
		},
		className: "note-input",
		ref: inputRef
	})), /* @__PURE__ */ react.default.createElement("div", { className: "action-buttons" }, !!value ? /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
		block: true,
		type: "primary",
		danger: true,
		icon: /* @__PURE__ */ react.default.createElement(_pisell_icon_es_Delete_wght300_24px.default, { style: { fontSize: 20 } }),
		onClick: handleDelete,
		className: "delete-btn"
	}, _pisell_utils.locales.getText("pisell2.text.note.modal.btn.delete")) : /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
		block: true,
		type: "default",
		onClick: handleCancel,
		className: "delete-btn"
	}, _pisell_utils.locales.getText("pisell2.text.note.modal.btn.cancel")), /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
		block: true,
		type: "primary",
		onClick: handleConfirm,
		className: "confirm-btn",
		disabled: !noteValue
	}, _pisell_utils.locales.getText("pisell2.text.note.modal.btn.confirm"))))), templateModalOpen && /* @__PURE__ */ react.default.createElement(require_AddTempModal.default, {
		open: templateModalOpen,
		onCancel: onAddTemplateCancel,
		onConfirm: onAddTemplateConfirm
	}));
};
const NoteModal = (props) => {
	if (!props.open) return null;
	return /* @__PURE__ */ react.default.createElement(NoteModalContent, props);
};
//#endregion
exports.default = NoteModal;
