var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/booking/notes/NoteBlock/index.tsx
var NoteBlock_exports = {};
__export(NoteBlock_exports, {
  default: () => NoteBlock_default
});
module.exports = __toCommonJS(NoteBlock_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_ahooks = require("ahooks");
var import_NoteItem = __toESM(require("./NoteItem"));
var import_addNote = __toESM(require("../addNote"));
var import_index = require("./index.less");
var import_utils = require("@pisell/utils");
var NoteBlock = (props) => {
  var _a, _b, _c, _d;
  const [isModalOpen, setIsModalOpen] = (0, import_react.useState)(false);
  const [value, setValue] = (0, import_react.useState)("");
  const [editNote, setEditNote] = (0, import_react.useState)(null);
  const {
    title,
    describe,
    isEdit,
    notes,
    loading,
    maxLength = 1,
    noteType
  } = props;
  const _add = (0, import_ahooks.useRequest)((_a = props.apis) == null ? void 0 : _a.addNotesByOrder, {
    manual: true,
    onSuccess: (res) => {
      var _a2;
      import_antd.message.success(
        import_utils.locales.getText("pisell2.text.status.add-note-successfully")
      );
      (_a2 = props.onChange) == null ? void 0 : _a2.call(props, res, "add", noteType);
      handleCancel();
    }
  });
  const _remove = (0, import_ahooks.useRequest)((_b = props.apis) == null ? void 0 : _b.removeNoteByOrder, {
    manual: true,
    onSuccess: () => {
      import_antd.message.success(
        import_utils.locales.getText("pisell2.text.status.remove-note-successfully")
      );
      handleCancel();
    }
  });
  const _edit = (0, import_ahooks.useRequest)((_c = props.apis) == null ? void 0 : _c.editNoteByOrder, {
    manual: true,
    onSuccess: (res) => {
      var _a2;
      import_antd.message.success(
        import_utils.locales.getText("pisell2.text.status.edit-note-successfully")
      );
      (_a2 = props.onChange) == null ? void 0 : _a2.call(props, res, "edit", noteType);
      handleCancel();
    },
    onError: (err) => {
    }
  });
  const _editOrderNote = (0, import_ahooks.useRequest)((_d = props.apis) == null ? void 0 : _d.editOrderNote, {
    manual: true,
    onSuccess: (res, param) => {
      var _a2;
      import_antd.message.success(
        import_utils.locales.getText("pisell2.text.status.edit-note-successfully")
      );
      (_a2 = props.onChange) == null ? void 0 : _a2.call(props, { id: 0, content: param[0].note }, "edit", noteType);
      handleCancel();
    },
    onError: (err) => {
    }
  });
  const _noteChange = (res, type) => {
    var _a2;
    if (type === "remove") {
      let _item = { id: res.id };
      if (res.id !== 0) {
        _remove.run(_item);
      } else {
        _editOrderNote.run({ note: "" });
      }
      (_a2 = props.onChange) == null ? void 0 : _a2.call(props, { ..._item, content: "" }, "remove", noteType);
    }
    if (type === "edit") {
      setEditNote(res);
      handleOpen(res.content);
    }
  };
  const handleOk = async () => {
    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;
    }
  };
  const handleOpen = (val) => {
    setValue(val || "");
    setIsModalOpen(true);
  };
  const handleCancel = () => {
    setValue("");
    setIsModalOpen(false);
    setEditNote(null);
  };
  const isAdd = (0, import_react.useMemo)(() => {
    return notes.length < maxLength;
  }, [notes.length, maxLength]);
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__booking-notes-wrap" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__booking-notes-title" }, /* @__PURE__ */ import_react.default.createElement("span", { className: "pisell-lowcode__booking-notes-title-text" }, title)), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__booking-notes-describe" }, describe), /* @__PURE__ */ import_react.default.createElement(import_antd.Skeleton, { loading }, notes.map((d) => {
    return /* @__PURE__ */ import_react.default.createElement(
      import_NoteItem.default,
      {
        key: d.id,
        isEdit: !!isEdit,
        item: d,
        onChange: _noteChange
      }
    );
  })), /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Modal,
    {
      title: editNote ? import_utils.locales.getText("pisell2.text.status.edit-note") : import_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__ */ import_react.default.createElement(
      import_addNote.default,
      {
        title,
        value,
        placeholder: props.placeholder,
        onChange: (e) => {
          setValue(e);
        },
        wait: 10
      }
    )
  ), isAdd ? /* @__PURE__ */ import_react.default.createElement(
    import_antd.Button,
    {
      size: "large",
      className: "pisell-lowcode__booking-notes-add",
      onClick: () => {
        handleOpen("");
      }
    },
    import_utils.locales.getText("pisell2.text.add-notes")
  ) : null));
};
var NoteBlock_default = NoteBlock;
