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/pisellSalesGrid/hooks/useActions.tsx
var useActions_exports = {};
__export(useActions_exports, {
  default: () => useActions_default
});
module.exports = __toCommonJS(useActions_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_serve = require("../../orderList/serve");
var import_useEngineContext = __toESM(require("../../../hooks/useEngineContext"));
var useActions = (options = {}) => {
  const { onClearSelection, onRefresh } = options;
  const context = (0, import_useEngineContext.default)();
  const [loadingStates, setLoadingStates] = (0, import_react.useState)(
    {}
  );
  const handleSuccess = (0, import_react.useCallback)(
    (successMessage) => {
      onRefresh == null ? void 0 : onRefresh();
      onClearSelection == null ? void 0 : onClearSelection();
    },
    [onRefresh, onClearSelection]
  );
  const handleError = (0, import_react.useCallback)(
    (error, defaultMessage) => {
    },
    []
  );
  const handleShip = (0, import_react.useCallback)(
    async (keys, rows) => {
      let checked = true;
      import_materials.PisellModal.confirm({
        title: import_utils.locales.getText("pisell2.orderList.batchAction.ship.title")(
          keys.length
        ),
        cancelText: import_utils.locales.getText(
          "pisell2.orderList.batchAction.button.cancel"
        ),
        okText: import_utils.locales.getText("pisell2.orderList.batchAction.button.ok"),
        content: /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement("p", null, import_utils.locales.getText("pisell2.orderList.batchAction.ship.content")), /* @__PURE__ */ import_react.default.createElement(
          import_antd.Checkbox,
          {
            defaultChecked: true,
            onChange: (e) => {
              checked = e.target.checked;
            }
          },
          import_utils.locales.getText("page.order.fa.check")
        )),
        onOk: async () => {
          try {
            setLoadingStates((prev) => ({ ...prev, ship: true }));
            await (0, import_serve.editordership)({
              ids: keys,
              is_notice: checked ? 1 : 0
            });
            handleSuccess();
          } catch (error) {
            handleError();
          } finally {
            setLoadingStates((prev) => ({ ...prev, ship: false }));
          }
        }
      });
    },
    [handleSuccess, handleError]
  );
  const handleInvoice = (0, import_react.useCallback)(
    async (keys, rows) => {
      import_materials.PisellModal.confirm({
        title: import_utils.locales.getText("page.order.table.list.button.send.invoice.tip"),
        cancelText: import_utils.locales.getText(
          "pisell2.orderList.batchAction.button.cancel"
        ),
        okText: import_utils.locales.getText("pisell2.orderList.batchAction.button.ok"),
        onOk: async () => {
          try {
            setLoadingStates((prev) => ({ ...prev, invoice: true }));
            await (0, import_serve.postSendInvoice)({
              order_ids: keys
            });
            handleSuccess();
          } catch (error) {
            handleError();
          } finally {
            setLoadingStates((prev) => ({ ...prev, invoice: false }));
          }
        }
      });
    },
    [handleSuccess, handleError]
  );
  const handlePrintPack = (0, import_react.useCallback)(
    (keys, rows) => {
      var _a, _b, _c, _d, _e, _f;
      const coreStorage = (_d = (_c = (_b = (_a = window == null ? void 0 : window.app) == null ? void 0 : _a.data) == null ? void 0 : _b.storage) == null ? void 0 : _c.getStorage) == null ? void 0 : _d.call(
        _c,
        "core"
      );
      const shopId = coreStorage ? (_f = (_e = JSON.parse(coreStorage)) == null ? void 0 : _e.mypisell_data) == null ? void 0 : _f.shop_id : void 0;
      if (!shopId) {
        console.log("shopId is not found");
        return;
      }
      (0, import_serve.printPack)({
        host: "",
        shop_id: shopId,
        ids: keys
      });
    },
    [context]
  );
  const handlePrintPicking = (0, import_react.useCallback)(
    (keys, rows) => {
      var _a, _b, _c, _d, _e, _f;
      const coreStorage = (_d = (_c = (_b = (_a = window == null ? void 0 : window.app) == null ? void 0 : _a.data) == null ? void 0 : _b.storage) == null ? void 0 : _c.getStorage) == null ? void 0 : _d.call(
        _c,
        "core"
      );
      const shopId = coreStorage ? (_f = (_e = JSON.parse(coreStorage)) == null ? void 0 : _e.mypisell_data) == null ? void 0 : _f.shop_id : void 0;
      if (!shopId) {
        console.log("shopId is not found");
        return;
      }
      (0, import_serve.printPicking)({
        host: "",
        shop_id: shopId,
        ids: keys
      });
    },
    [context]
  );
  const handleAddTags = (0, import_react.useCallback)(
    async (keys, rows) => {
      let tags = [];
      import_materials.PisellModal.confirm({
        title: import_utils.locales.getText("pisell2.orderList.batchAction.addTags.title")(
          keys.length
        ),
        width: 520,
        cancelText: import_utils.locales.getText(
          "pisell2.orderList.batchAction.button.cancel"
        ),
        okText: import_utils.locales.getText("pisell2.orderList.batchAction.button.ok"),
        content: /* @__PURE__ */ import_react.default.createElement("div", { style: { paddingTop: 16 } }, /* @__PURE__ */ import_react.default.createElement("div", { style: { marginBottom: 8, color: "#666" } }, import_utils.locales.getText("pisell2.orderList.column.tags")), /* @__PURE__ */ import_react.default.createElement(
          import_materials.PisellQuickFilter,
          {
            hasForm: false,
            filterList: [
              {
                name: "tag_ids",
                type: "select",
                key: "tag_ids",
                label: import_utils.locales.getText("pisell2.orderList.filter.tags.label"),
                other: {
                  allowClear: true,
                  mode: "multiple",
                  isSearchServer: true,
                  showSearch: true,
                  onChange: (value, options2) => {
                    tags = options2.map((opt) => opt.label);
                  },
                  style: {
                    width: "470px"
                  },
                  placeholder: import_utils.locales.getText(
                    "pisell2.orderList.filter.tags.placeholder"
                  ),
                  options: (params) => (0, import_serve.getTagSelectOptions)(params).then(
                    (res) => res.map((item) => ({
                      label: item.name,
                      value: item.id
                    }))
                  )
                }
              }
            ]
          }
        )),
        onOk: async () => {
          try {
            setLoadingStates((prev) => ({ ...prev, addTags: true }));
            await (0, import_serve.batchAddOrderTags)({
              name: tags,
              ids: keys
            });
            handleSuccess();
          } catch (error) {
            handleError();
          } finally {
            setLoadingStates((prev) => ({ ...prev, addTags: false }));
          }
        }
      });
    },
    [handleSuccess, handleError]
  );
  const handleRemoveTags = (0, import_react.useCallback)(
    async (keys, rows) => {
      let tags = [];
      import_materials.PisellModal.confirm({
        title: import_utils.locales.getText(
          "pisell2.orderList.batchAction.removeTags.title"
        )(keys.length),
        width: 520,
        cancelText: import_utils.locales.getText(
          "pisell2.orderList.batchAction.button.cancel"
        ),
        okText: import_utils.locales.getText("pisell2.orderList.batchAction.button.ok"),
        content: /* @__PURE__ */ import_react.default.createElement("div", { style: { paddingTop: 16 } }, /* @__PURE__ */ import_react.default.createElement("div", { style: { marginBottom: 8, color: "#666" } }, import_utils.locales.getText("pisell2.orderList.column.tags")), /* @__PURE__ */ import_react.default.createElement(
          import_materials.PisellQuickFilter,
          {
            hasForm: false,
            filterList: [
              {
                name: "tag_ids",
                type: "select",
                key: "tag_ids",
                label: import_utils.locales.getText("pisell2.orderList.filter.tags.label"),
                other: {
                  allowClear: true,
                  mode: "multiple",
                  isSearchServer: true,
                  showSearch: true,
                  onChange: (value, options2) => {
                    tags = options2.map((opt) => opt.label);
                  },
                  style: {
                    width: "470px"
                  },
                  placeholder: import_utils.locales.getText(
                    "pisell2.orderList.filter.tags.placeholder"
                  ),
                  options: (params) => (0, import_serve.getTagSelectOptions)(params).then(
                    (res) => res.map((item) => ({
                      label: item.name,
                      value: item.id
                    }))
                  )
                }
              }
            ]
          }
        )),
        onOk: async () => {
          try {
            setLoadingStates((prev) => ({ ...prev, removeTags: true }));
            await (0, import_serve.batchDeleteOrderTags)({
              name: tags,
              ids: keys
            });
            handleSuccess();
          } catch (error) {
            handleError();
          } finally {
            setLoadingStates((prev) => ({ ...prev, removeTags: false }));
          }
        }
      });
    },
    [handleSuccess, handleError]
  );
  const handleMarkAsOpen = (0, import_react.useCallback)(
    async (keys, rows) => {
      const open = rows == null ? void 0 : rows.filter((item) => item.status === "open").length;
      const completed = rows == null ? void 0 : rows.filter(
        (item) => item.status === "completed"
      ).length;
      const cancelled = rows == null ? void 0 : rows.filter(
        (item) => item.status === "cancelled"
      ).length;
      import_materials.PisellModal.confirm({
        title: import_utils.locales.getText("page.order.cancel.title")(
          import_utils.locales.getText("pisell2.orderList.orderStatus.open")
        ),
        cancelText: import_utils.locales.getText(
          "pisell2.orderList.batchAction.button.cancel"
        ),
        okText: import_utils.locales.getText("pisell2.orderList.batchAction.button.ok"),
        content: import_utils.locales.getText("page.order.lists.cancel.content")(
          open,
          completed,
          cancelled,
          import_utils.locales.getText("pisell2.orderList.orderStatus.completed"),
          true,
          "void"
        ),
        onOk: async () => {
          try {
            setLoadingStates((prev) => ({ ...prev, markAsOpen: true }));
            await (0, import_serve.batchPutOrderStatus)({
              order_ids: keys,
              status: "open"
            });
            handleSuccess();
          } catch (error) {
            handleError();
          } finally {
            setLoadingStates((prev) => ({ ...prev, markAsOpen: false }));
          }
        }
      });
    },
    [handleSuccess, handleError]
  );
  const handleMarkAsCompleted = (0, import_react.useCallback)(
    async (keys, rows) => {
      const open = rows == null ? void 0 : rows.filter((item) => item.status === "open").length;
      const completed = rows == null ? void 0 : rows.filter(
        (item) => item.status === "completed"
      ).length;
      const cancelled = rows == null ? void 0 : rows.filter(
        (item) => item.status === "cancelled"
      ).length;
      import_materials.PisellModal.confirm({
        title: import_utils.locales.getText("page.order.cancel.title")(
          import_utils.locales.getText("pisell2.orderList.orderStatus.completed")
        ),
        cancelText: import_utils.locales.getText(
          "pisell2.orderList.batchAction.button.cancel"
        ),
        okText: import_utils.locales.getText("pisell2.orderList.batchAction.button.ok"),
        content: import_utils.locales.getText("page.order.lists.cancel.content")(
          open,
          completed,
          cancelled,
          import_utils.locales.getText("pisell2.orderList.orderStatus.open"),
          true,
          ""
        ),
        onOk: async () => {
          try {
            setLoadingStates((prev) => ({ ...prev, markAsCompleted: true }));
            await (0, import_serve.batchPutOrderStatus)({
              order_ids: keys,
              status: "completed"
            });
            handleSuccess();
          } catch (error) {
            handleError();
          } finally {
            setLoadingStates((prev) => ({ ...prev, markAsCompleted: false }));
          }
        }
      });
    },
    [handleSuccess, handleError]
  );
  const handleMarkAsCancelled = (0, import_react.useCallback)(
    async (keys, rows) => {
      const open = rows == null ? void 0 : rows.filter((item) => item.status === "open").length;
      const completed = rows == null ? void 0 : rows.filter(
        (item) => item.status === "completed"
      ).length;
      const cancelled = rows == null ? void 0 : rows.filter(
        (item) => item.status === "cancelled"
      ).length;
      const executeCancelOrder = async (isVoid) => {
        try {
          setLoadingStates((prev) => ({ ...prev, markAsCancelled: true }));
          await (0, import_serve.batchPutOrderStatus)({
            order_ids: keys,
            status: "cancelled",
            ...isVoid === 1 && { is_void: isVoid }
          });
          handleSuccess();
          modalInstance.destroy();
        } catch (error) {
          handleError();
        } finally {
          setLoadingStates((prev) => ({ ...prev, markAsCancelled: false }));
        }
      };
      const modalInstance = import_materials.PisellModal.confirm({
        title: import_utils.locales.getText("page.order.cancel.title")(
          import_utils.locales.getText("pisell2.orderList.orderStatus.cancelled")
        ),
        content: import_utils.locales.getText("page.order.lists.cancel.content")(
          open,
          completed,
          cancelled,
          import_utils.locales.getText("pisell2.orderList.orderStatus.cancelled"),
          true,
          ""
        ),
        cancelText: import_utils.locales.getText(
          "pisell2.orderList.batchAction.button.cancel"
        ),
        footer: () => /* @__PURE__ */ import_react.default.createElement(
          "div",
          {
            style: {
              display: "flex",
              gap: 10,
              justifyContent: "flex-end",
              width: "100%"
            }
          },
          /* @__PURE__ */ import_react.default.createElement(import_antd.Button, { key: "cancel", onClick: () => modalInstance.destroy() }, import_utils.locales.getText("pisell2.orderList.batchAction.button.cancel")),
          /* @__PURE__ */ import_react.default.createElement(
            import_antd.Button,
            {
              key: "only-cancel",
              onClick: () => executeCancelOrder(0),
              loading: loadingStates.markAsCancelled
            },
            import_utils.locales.getText(
              "pisell2.orderList.batchAction.button.onlyCancel"
            )
          ),
          /* @__PURE__ */ import_react.default.createElement(
            import_antd.Button,
            {
              key: "cancel-void",
              type: "primary",
              onClick: () => executeCancelOrder(1),
              loading: loadingStates.markAsCancelled
            },
            import_utils.locales.getText(
              "pisell2.orderList.batchAction.button.cancelAndVoid"
            )
          )
        )
      });
    },
    [handleSuccess, handleError, loadingStates.markAsCancelled]
  );
  const actions = (0, import_react.useMemo)(
    () => [
      {
        key: "ship",
        label: import_utils.locales.getText(
          "page.order.table.shipping_status.mark.fulfilled"
        ),
        icon: "pisell2-check-circle-broken",
        loading: loadingStates.ship,
        onClick: handleShip
      },
      {
        key: "invoice",
        label: import_utils.locales.getText("page.order.table.list.button.send.invoice"),
        icon: "pisell2-sticker-square",
        loading: loadingStates.invoice,
        onClick: handleInvoice
      },
      {
        key: "print-pack",
        label: import_utils.locales.getText("page.order.table.list.printpack"),
        icon: "pisell2-cube-01",
        onClick: handlePrintPack
      },
      {
        key: "print-picking",
        label: import_utils.locales.getText("page.order.table.list.printpink"),
        icon: "pisell2-package",
        onClick: handlePrintPicking
      },
      {
        key: "add-tags",
        label: import_utils.locales.getText("page.order.text.addtags.button"),
        icon: "pisell2-tag-01",
        loading: loadingStates.addTags,
        onClick: handleAddTags
      },
      {
        key: "remove-tags",
        label: import_utils.locales.getText("page.order.table.list.deletetag"),
        icon: "pisell2-tag-01",
        loading: loadingStates.removeTags,
        onClick: handleRemoveTags
      },
      {
        key: "mark-as-open",
        label: import_utils.locales.getText("page.order.open"),
        icon: "pisell2-clipboard",
        loading: loadingStates.markAsOpen,
        onClick: handleMarkAsOpen
      },
      {
        key: "mark-as-completed",
        label: import_utils.locales.getText("page.order.completeds"),
        icon: "pisell2-clipboard-check",
        loading: loadingStates.markAsCompleted,
        onClick: handleMarkAsCompleted
      },
      {
        key: "mark-as-cancelled",
        label: import_utils.locales.getText("page.order.cancelleds"),
        icon: "pisell2-slash-circle-01",
        loading: loadingStates.markAsCancelled,
        onClick: handleMarkAsCancelled
      }
    ],
    [
      loadingStates.ship,
      loadingStates.invoice,
      loadingStates.addTags,
      loadingStates.removeTags,
      loadingStates.markAsOpen,
      loadingStates.markAsCompleted,
      loadingStates.markAsCancelled,
      handleShip,
      handleInvoice,
      handlePrintPack,
      handlePrintPicking,
      handleAddTags,
      handleRemoveTags,
      handleMarkAsOpen,
      handleMarkAsCompleted,
      handleMarkAsCancelled
    ]
  );
  return actions;
};
var useActions_default = useActions;
