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/index.tsx
var notes_exports = {};
__export(notes_exports, {
  default: () => notes_default
});
module.exports = __toCommonJS(notes_exports);
var import_react = __toESM(require("react"));
var import_tabPane = __toESM(require("../components/tabPane"));
var import_NoteBlock = __toESM(require("./NoteBlock"));
var import_model = require("./model");
var import_emptyBooking = __toESM(require("../components/emptyBooking"));
var import_index = require("./index.less");
var import_utils = require("@pisell/utils");
var Notes = (0, import_model.Provider)((props) => {
  var _a, _b, _c, _d, _e;
  const { globalState } = props;
  const modal = (0, import_react.useContext)(import_model.Context);
  const [internalLoading, setInternalLoading] = (0, import_react.useState)(false);
  const getNotes = async () => {
    var _a2, _b2;
    setInternalLoading(true);
    const data = await modal.state.apis.getNotesByOrder({
      schedule_event_id: (_b2 = (_a2 = modal.state.bookingDetail) == null ? void 0 : _a2.bookings) == null ? void 0 : _b2.id,
      skip: 1,
      num: 99
    });
    modal.dispatch({
      type: "setNotes",
      payload: {
        internalNotes: (data == null ? void 0 : data.list) || []
      }
    });
    setInternalLoading(false);
  };
  (0, import_react.useEffect)(() => {
    if (modal.state.bookingDetail) {
      getNotes();
    }
  }, [modal.state.bookingDetail]);
  (0, import_react.useEffect)(() => {
    document.body.id = "body";
  }, []);
  (0, import_react.useEffect)(() => {
    modal.dispatch({
      type: "setState",
      payload: {
        setGlobalState: props.setGlobalState,
        ...globalState
      }
    });
  }, [globalState]);
  const noteChange = ({ note, status, noteType }) => {
    var _a2, _b2, _c2;
    (_b2 = (_a2 = modal.state) == null ? void 0 : _a2.refreshData) == null ? void 0 : _b2.call(_a2, {});
    if (noteType === "internal") {
      let _list = [...(_c2 = modal.state) == null ? void 0 : _c2.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 = (0, import_react.useMemo)(() => {
    var _a2;
    const { bookingDetail } = modal.state;
    return [
      {
        id: 0,
        content: ((_a2 = bookingDetail == null ? void 0 : bookingDetail.customer) == null ? void 0 : _a2.note) || ""
      }
    ];
  }, [modal.state.bookingDetail]);
  const orderNote = (0, import_react.useMemo)(() => {
    var _a2, _b2, _c2, _d2;
    if (!((_b2 = (_a2 = modal.state) == null ? void 0 : _a2.notes) == null ? void 0 : _b2.orderNote)) {
      return [];
    }
    return [
      {
        id: 0,
        content: ((_d2 = (_c2 = modal.state) == null ? void 0 : _c2.notes) == null ? void 0 : _d2.orderNote) || ""
      }
    ];
  }, [(_b = (_a = modal.state) == null ? void 0 : _a.notes) == null ? void 0 : _b.orderNote]);
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, !!modal.state.setGlobalState && /* @__PURE__ */ import_react.default.createElement(import_tabPane.default, { header: import_utils.locales.getText("pisell2.text.notes") }, !globalState.bookingId ? /* @__PURE__ */ import_react.default.createElement(import_emptyBooking.default, null) : /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
    import_NoteBlock.default,
    {
      maxLength: 0,
      noteType: "customer",
      title: `${import_utils.locales.getText(
        "pisell2.text.status.notes-for-customer"
      )}:`,
      describe: import_utils.locales.getText(
        "pisell2.text.status.visible-only-to-your-team-members"
      ),
      notes: customerNote,
      isEdit: false
    }
  ), /* @__PURE__ */ import_react.default.createElement(
    import_NoteBlock.default,
    {
      noteType: "order",
      maxLength: 1,
      title: `${import_utils.locales.getText("pisell2.text.order-notes")}:`,
      describe: "",
      notes: orderNote,
      apis: modal.state.apis,
      placeholder: import_utils.locales.getText("pisell2.text.add-order-notes"),
      isEdit: true,
      onChange: (e, type, noteType) => {
        noteChange({
          note: e,
          status: type,
          noteType
        });
      }
    }
  ), /* @__PURE__ */ import_react.default.createElement(
    import_NoteBlock.default,
    {
      maxLength: 99,
      noteType: "internal",
      loading: internalLoading,
      title: `${import_utils.locales.getText(
        "pisell2.text.status.internal-notes"
      )}:`,
      describe: import_utils.locales.getText(
        "pisell2.text.status.visible-only-to-your-team-members"
      ),
      placeholder: import_utils.locales.getText("pisell2.text.add-internal-notes"),
      notes: ((_c = modal.state) == null ? void 0 : _c.notes.internalNotes) || [],
      isEdit: true,
      apis: modal.state.apis,
      bookingId: (_e = (_d = modal.state.bookingDetail) == null ? void 0 : _d.bookings) == null ? void 0 : _e.id,
      onChange: (e, type, noteType) => {
        noteChange({
          note: e,
          status: type,
          noteType
        });
      }
    }
  ))));
});
var notes_default = Notes;
