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/plus/saleDetail/hooks/useOrderNoteModal.tsx
var useOrderNoteModal_exports = {};
__export(useOrderNoteModal_exports, {
  useOrderNoteModal: () => useOrderNoteModal
});
module.exports = __toCommonJS(useOrderNoteModal_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_utils = require("@pisell/utils");
var import_noteModal = __toESM(require("../../noteModal"));
function alertMissingAction(actionName) {
  const messageText = `[SaleDetail] Missing action: ${actionName}`;
  if (typeof window !== "undefined" && typeof window.alert === "function") {
    window.alert(messageText);
    return;
  }
  import_antd.message.error(messageText);
}
function useOrderNoteModal({
  context,
  onAfterChange
}) {
  var _a;
  const [open, setOpen] = (0, import_react.useState)(false);
  const openOrderNoteModal = (0, import_react.useCallback)(() => {
    if (typeof context.sales.setOrderNote !== "function") {
      alertMissingAction("sales.setOrderNote");
      return;
    }
    setOpen(true);
  }, [context.sales]);
  const closeOrderNoteModal = (0, import_react.useCallback)(() => {
    setOpen(false);
  }, []);
  const handleConfirm = (0, import_react.useCallback)(
    async (noteValue) => {
      context.sales.setOrderNote(noteValue);
      await (onAfterChange == null ? void 0 : onAfterChange());
      setOpen(false);
    },
    [context.sales, onAfterChange]
  );
  const handleDelete = (0, import_react.useCallback)(async () => {
    if (typeof context.sales.setOrderNote !== "function") {
      alertMissingAction("sales.setOrderNote");
      return;
    }
    context.sales.setOrderNote("");
    await (onAfterChange == null ? void 0 : onAfterChange());
    setOpen(false);
  }, [context.sales, onAfterChange]);
  const orderNoteModal = (0, import_react.useMemo)(
    () => {
      var _a2;
      return open ? /* @__PURE__ */ import_react.default.createElement(
        import_noteModal.default,
        {
          open,
          title: import_utils.locales.getText("pisell2.text.add-order-notes"),
          value: ((_a2 = context.sales.tempOrder) == null ? void 0 : _a2.note) || "",
          onConfirm: handleConfirm,
          onDelete: handleDelete,
          onCancel: closeOrderNoteModal
        }
      ) : null;
    },
    [
      closeOrderNoteModal,
      (_a = context.sales.tempOrder) == null ? void 0 : _a.note,
      handleConfirm,
      handleDelete,
      open
    ]
  );
  return {
    openOrderNoteModal,
    closeOrderNoteModal,
    removeOrderNote: handleDelete,
    orderNoteModal
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useOrderNoteModal
});
