function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useState, useEffect, useRef } from 'react';
import { PisellModal, Input, Button } from '@pisell/materials';
import { locales } from '@pisell/utils';
import Delete_wght300_24px from '@pisell/icon/es/Delete_wght300_24px';
import AddTempModal from "./AddTempModal";
import "./index.less";
import localeTexts from "./locales";
import useEngineContext from "../../hooks/useEngineContext";
var TextArea = Input.TextArea;
var STORAGE_KEY = 'pisell2_note_templates';
var NoteModalContent = function NoteModalContent(_ref) {
  var _context$engine;
  var _ref$open = _ref.open,
    open = _ref$open === void 0 ? false : _ref$open,
    _ref$title = _ref.title,
    title = _ref$title === void 0 ? '' : _ref$title,
    _ref$value = _ref.value,
    value = _ref$value === void 0 ? '' : _ref$value,
    onConfirm = _ref.onConfirm,
    onDelete = _ref.onDelete,
    onCancel = _ref.onCancel,
    zIndex = _ref.zIndex;
  var context = useEngineContext();
  locales.init(localeTexts, (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');
  var _useState = useState(value),
    _useState2 = _slicedToArray(_useState, 2),
    noteValue = _useState2[0],
    setNoteValue = _useState2[1];
  var _useState3 = useState([]),
    _useState4 = _slicedToArray(_useState3, 2),
    templates = _useState4[0],
    setTemplates = _useState4[1];
  var _useState5 = useState(false),
    _useState6 = _slicedToArray(_useState5, 2),
    templateModalOpen = _useState6[0],
    setTemplateModalOpen = _useState6[1];
  var inputRef = useRef(null);

  // 从localStorage加载模板
  useEffect(function () {
    if (open) {
      var 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(function () {
        var _inputRef$current;
        (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 || _inputRef$current.focus();
      }, 100);
    }
  }, [open]);

  // 保存模板到localStorage
  var saveTemplates = function saveTemplates(newTemplates) {
    localStorage.setItem(STORAGE_KEY, JSON.stringify(newTemplates));
    setTemplates(newTemplates);
  };

  // 点击模板标签
  var handleTemplateClick = function handleTemplateClick(template) {
    setNoteValue(function (prev) {
      return (prev || '') + ' ' + (template || '');
    });
  };

  // 确认主弹窗
  var handleConfirm = function handleConfirm() {
    onConfirm === null || onConfirm === void 0 || onConfirm(noteValue);
  };

  // 删除
  var handleDelete = function handleDelete() {
    setNoteValue('');
    onDelete === null || onDelete === void 0 || onDelete();
  };

  // 取消
  var handleCancel = function handleCancel() {
    setNoteValue('');
    onCancel === null || onCancel === void 0 || onCancel();
  };

  // 添加模板
  var onAddTemplate = function onAddTemplate() {
    setTemplateModalOpen(true);
  };

  // 添加新模板
  var onAddTemplateConfirm = function onAddTemplateConfirm(newTemplate) {
    if (newTemplate.trim()) {
      var updatedTemplates = [].concat(_toConsumableArray(templates), [newTemplate.trim()]);
      saveTemplates(updatedTemplates);
      setTemplateModalOpen(false);
    }
  };
  var onAddTemplateCancel = function onAddTemplateCancel() {
    setTemplateModalOpen(false);
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(PisellModal, {
    open: open,
    title: title || locales.getText('pisell2.text.note.modal.title'),
    headerDivider: false,
    onCancel: handleCancel,
    footer: null,
    width: 680,
    centered: true,
    destroyOnClose: true,
    zIndex: zIndex
  }, /*#__PURE__*/React.createElement("div", {
    className: "note-modal-content"
  }, /*#__PURE__*/React.createElement("div", {
    className: "template-section"
  }, /*#__PURE__*/React.createElement("div", {
    onClick: onAddTemplate,
    className: "add-template-btn"
  }, locales.getText('pisell2.text.note.modal.btn.add.template')), /*#__PURE__*/React.createElement("div", {
    className: "note-modal-templates-container",
    onTouchMove: function onTouchMove(e) {
      return e.stopPropagation();
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: "note-modal-templates-scroll"
  }, templates.map(function (template, index) {
    return /*#__PURE__*/React.createElement("div", {
      key: index,
      style: {
        display: 'inline-block',
        marginLeft: '8px'
      }
    }, /*#__PURE__*/React.createElement("div", {
      className: "note-modal-template-tag",
      onClick: function onClick() {
        return handleTemplateClick(template);
      }
    }, template));
  })))), /*#__PURE__*/React.createElement("div", {
    className: "input-section"
  }, /*#__PURE__*/React.createElement(TextArea, {
    value: noteValue,
    onChange: function onChange(e) {
      return setNoteValue(e.target.value);
    },
    placeholder: locales.getText('pisell2.text.note.modal.placeholder'),
    autoSize: {
      minRows: 4,
      maxRows: 8
    },
    className: "note-input",
    ref: inputRef
  })), /*#__PURE__*/React.createElement("div", {
    className: "action-buttons"
  }, !!value ? /*#__PURE__*/React.createElement(Button, {
    block: true,
    type: "primary",
    danger: true,
    icon: /*#__PURE__*/React.createElement(Delete_wght300_24px, {
      style: {
        fontSize: 20
      }
    }),
    onClick: handleDelete,
    className: "delete-btn"
  }, locales.getText('pisell2.text.note.modal.btn.delete')) : /*#__PURE__*/React.createElement(Button, {
    block: true,
    type: "default",
    onClick: handleCancel,
    className: "delete-btn"
  }, locales.getText('pisell2.text.note.modal.btn.cancel')), /*#__PURE__*/React.createElement(Button, {
    block: true,
    type: "primary",
    onClick: handleConfirm,
    className: "confirm-btn",
    disabled: !noteValue
  }, locales.getText('pisell2.text.note.modal.btn.confirm'))))), templateModalOpen && /*#__PURE__*/React.createElement(AddTempModal, {
    open: templateModalOpen,
    onCancel: onAddTemplateCancel,
    onConfirm: onAddTemplateConfirm
  }));
};
var NoteModal = function NoteModal(props) {
  if (!props.open) {
    return null;
  }
  return /*#__PURE__*/React.createElement(NoteModalContent, props);
};
export default NoteModal;