const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_useEngineContext = require("../../../hooks/useEngineContext.js");
const require_serve = require("../../orderList/serve.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
//#region src/plus/pisellSalesGrid/hooks/useActions.tsx
const useActions = (options = {}) => {
	const { onClearSelection, onRefresh } = options;
	const context = require_useEngineContext.default();
	const [loadingStates, setLoadingStates] = (0, react.useState)({});
	const handleSuccess = (0, react.useCallback)((successMessage) => {
		onRefresh === null || onRefresh === void 0 || onRefresh();
		onClearSelection === null || onClearSelection === void 0 || onClearSelection();
	}, [onRefresh, onClearSelection]);
	const handleError = (0, react.useCallback)((error, defaultMessage) => {}, []);
	const handleShip = (0, react.useCallback)(function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (keys, rows) {
			let checked = true;
			_pisell_materials.PisellModal.confirm({
				title: _pisell_utils.locales.getText("pisell2.orderList.batchAction.ship.title")(keys.length),
				cancelText: _pisell_utils.locales.getText("pisell2.orderList.batchAction.button.cancel"),
				okText: _pisell_utils.locales.getText("pisell2.orderList.batchAction.button.ok"),
				content: /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement("p", null, _pisell_utils.locales.getText("pisell2.orderList.batchAction.ship.content")), /* @__PURE__ */ react.default.createElement(antd.Checkbox, {
					defaultChecked: true,
					onChange: (e) => {
						checked = e.target.checked;
					}
				}, _pisell_utils.locales.getText("page.order.fa.check"))),
				onOk: function() {
					var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
						try {
							setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { ship: true }));
							yield require_serve.editordership({
								ids: keys,
								is_notice: checked ? 1 : 0
							});
							handleSuccess();
						} catch (error) {
							handleError();
						} finally {
							setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { ship: false }));
						}
					});
					return function onOk() {
						return _ref.apply(this, arguments);
					};
				}()
			});
		});
		return function(_x, _x2) {
			return _ref2.apply(this, arguments);
		};
	}(), [handleSuccess, handleError]);
	const handleInvoice = (0, react.useCallback)(function() {
		var _ref4 = require_asyncToGenerator._asyncToGenerator(function* (keys, rows) {
			_pisell_materials.PisellModal.confirm({
				title: _pisell_utils.locales.getText("page.order.table.list.button.send.invoice.tip"),
				cancelText: _pisell_utils.locales.getText("pisell2.orderList.batchAction.button.cancel"),
				okText: _pisell_utils.locales.getText("pisell2.orderList.batchAction.button.ok"),
				onOk: function() {
					var _ref3 = require_asyncToGenerator._asyncToGenerator(function* () {
						try {
							setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { invoice: true }));
							yield require_serve.postSendInvoice({ order_ids: keys });
							handleSuccess();
						} catch (error) {
							handleError();
						} finally {
							setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { invoice: false }));
						}
					});
					return function onOk() {
						return _ref3.apply(this, arguments);
					};
				}()
			});
		});
		return function(_x3, _x4) {
			return _ref4.apply(this, arguments);
		};
	}(), [handleSuccess, handleError]);
	const handlePrintPack = (0, react.useCallback)((keys, rows) => {
		var _window, _window$getStorage, _JSON$parse;
		const coreStorage = (_window = window) === null || _window === void 0 || (_window = _window.app) === null || _window === void 0 || (_window = _window.data) === null || _window === void 0 || (_window = _window.storage) === null || _window === void 0 || (_window$getStorage = _window.getStorage) === null || _window$getStorage === void 0 ? void 0 : _window$getStorage.call(_window, "core");
		const shopId = coreStorage ? (_JSON$parse = JSON.parse(coreStorage)) === null || _JSON$parse === void 0 || (_JSON$parse = _JSON$parse.mypisell_data) === null || _JSON$parse === void 0 ? void 0 : _JSON$parse.shop_id : void 0;
		if (!shopId) {
			console.log("shopId is not found");
			return;
		}
		require_serve.printPack({
			host: "",
			shop_id: shopId,
			ids: keys
		});
	}, [context]);
	const handlePrintPicking = (0, react.useCallback)((keys, rows) => {
		var _window2, _window2$getStorage, _JSON$parse2;
		const coreStorage = (_window2 = window) === null || _window2 === void 0 || (_window2 = _window2.app) === null || _window2 === void 0 || (_window2 = _window2.data) === null || _window2 === void 0 || (_window2 = _window2.storage) === null || _window2 === void 0 || (_window2$getStorage = _window2.getStorage) === null || _window2$getStorage === void 0 ? void 0 : _window2$getStorage.call(_window2, "core");
		const shopId = coreStorage ? (_JSON$parse2 = JSON.parse(coreStorage)) === null || _JSON$parse2 === void 0 || (_JSON$parse2 = _JSON$parse2.mypisell_data) === null || _JSON$parse2 === void 0 ? void 0 : _JSON$parse2.shop_id : void 0;
		if (!shopId) {
			console.log("shopId is not found");
			return;
		}
		require_serve.printPicking({
			host: "",
			shop_id: shopId,
			ids: keys
		});
	}, [context]);
	const handleAddTags = (0, react.useCallback)(function() {
		var _ref6 = require_asyncToGenerator._asyncToGenerator(function* (keys, rows) {
			let tags = [];
			_pisell_materials.PisellModal.confirm({
				title: _pisell_utils.locales.getText("pisell2.orderList.batchAction.addTags.title")(keys.length),
				width: 520,
				cancelText: _pisell_utils.locales.getText("pisell2.orderList.batchAction.button.cancel"),
				okText: _pisell_utils.locales.getText("pisell2.orderList.batchAction.button.ok"),
				content: /* @__PURE__ */ react.default.createElement("div", { style: { paddingTop: 16 } }, /* @__PURE__ */ react.default.createElement("div", { style: {
					marginBottom: 8,
					color: "#666"
				} }, _pisell_utils.locales.getText("pisell2.orderList.column.tags")), /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellQuickFilter, {
					hasForm: false,
					filterList: [{
						name: "tag_ids",
						type: "select",
						key: "tag_ids",
						label: _pisell_utils.locales.getText("pisell2.orderList.filter.tags.label"),
						other: {
							allowClear: true,
							mode: "multiple",
							isSearchServer: true,
							showSearch: true,
							onChange: (value, options) => {
								tags = options.map((opt) => opt.label);
							},
							style: { width: "470px" },
							placeholder: _pisell_utils.locales.getText("pisell2.orderList.filter.tags.placeholder"),
							options: (params) => require_serve.getTagSelectOptions(params).then((res) => res.map((item) => ({
								label: item.name,
								value: item.id
							})))
						}
					}]
				})),
				onOk: function() {
					var _ref5 = require_asyncToGenerator._asyncToGenerator(function* () {
						try {
							setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { addTags: true }));
							yield require_serve.batchAddOrderTags({
								name: tags,
								ids: keys
							});
							handleSuccess();
						} catch (error) {
							handleError();
						} finally {
							setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { addTags: false }));
						}
					});
					return function onOk() {
						return _ref5.apply(this, arguments);
					};
				}()
			});
		});
		return function(_x5, _x6) {
			return _ref6.apply(this, arguments);
		};
	}(), [handleSuccess, handleError]);
	const handleRemoveTags = (0, react.useCallback)(function() {
		var _ref8 = require_asyncToGenerator._asyncToGenerator(function* (keys, rows) {
			let tags = [];
			_pisell_materials.PisellModal.confirm({
				title: _pisell_utils.locales.getText("pisell2.orderList.batchAction.removeTags.title")(keys.length),
				width: 520,
				cancelText: _pisell_utils.locales.getText("pisell2.orderList.batchAction.button.cancel"),
				okText: _pisell_utils.locales.getText("pisell2.orderList.batchAction.button.ok"),
				content: /* @__PURE__ */ react.default.createElement("div", { style: { paddingTop: 16 } }, /* @__PURE__ */ react.default.createElement("div", { style: {
					marginBottom: 8,
					color: "#666"
				} }, _pisell_utils.locales.getText("pisell2.orderList.column.tags")), /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellQuickFilter, {
					hasForm: false,
					filterList: [{
						name: "tag_ids",
						type: "select",
						key: "tag_ids",
						label: _pisell_utils.locales.getText("pisell2.orderList.filter.tags.label"),
						other: {
							allowClear: true,
							mode: "multiple",
							isSearchServer: true,
							showSearch: true,
							onChange: (value, options) => {
								tags = options.map((opt) => opt.label);
							},
							style: { width: "470px" },
							placeholder: _pisell_utils.locales.getText("pisell2.orderList.filter.tags.placeholder"),
							options: (params) => require_serve.getTagSelectOptions(params).then((res) => res.map((item) => ({
								label: item.name,
								value: item.id
							})))
						}
					}]
				})),
				onOk: function() {
					var _ref7 = require_asyncToGenerator._asyncToGenerator(function* () {
						try {
							setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { removeTags: true }));
							yield require_serve.batchDeleteOrderTags({
								name: tags,
								ids: keys
							});
							handleSuccess();
						} catch (error) {
							handleError();
						} finally {
							setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { removeTags: false }));
						}
					});
					return function onOk() {
						return _ref7.apply(this, arguments);
					};
				}()
			});
		});
		return function(_x7, _x8) {
			return _ref8.apply(this, arguments);
		};
	}(), [handleSuccess, handleError]);
	const handleMarkAsOpen = (0, react.useCallback)(function() {
		var _ref10 = require_asyncToGenerator._asyncToGenerator(function* (keys, rows) {
			const open = rows === null || rows === void 0 ? void 0 : rows.filter((item) => item.status === "open").length;
			const completed = rows === null || rows === void 0 ? void 0 : rows.filter((item) => item.status === "completed").length;
			const cancelled = rows === null || rows === void 0 ? void 0 : rows.filter((item) => item.status === "cancelled").length;
			_pisell_materials.PisellModal.confirm({
				title: _pisell_utils.locales.getText("page.order.cancel.title")(_pisell_utils.locales.getText("pisell2.orderList.orderStatus.open")),
				cancelText: _pisell_utils.locales.getText("pisell2.orderList.batchAction.button.cancel"),
				okText: _pisell_utils.locales.getText("pisell2.orderList.batchAction.button.ok"),
				content: _pisell_utils.locales.getText("page.order.lists.cancel.content")(open, completed, cancelled, _pisell_utils.locales.getText("pisell2.orderList.orderStatus.completed"), true, "void"),
				onOk: function() {
					var _ref9 = require_asyncToGenerator._asyncToGenerator(function* () {
						try {
							setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { markAsOpen: true }));
							yield require_serve.batchPutOrderStatus({
								order_ids: keys,
								status: "open"
							});
							handleSuccess();
						} catch (error) {
							handleError();
						} finally {
							setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { markAsOpen: false }));
						}
					});
					return function onOk() {
						return _ref9.apply(this, arguments);
					};
				}()
			});
		});
		return function(_x9, _x10) {
			return _ref10.apply(this, arguments);
		};
	}(), [handleSuccess, handleError]);
	const handleMarkAsCompleted = (0, react.useCallback)(function() {
		var _ref12 = require_asyncToGenerator._asyncToGenerator(function* (keys, rows) {
			const open = rows === null || rows === void 0 ? void 0 : rows.filter((item) => item.status === "open").length;
			const completed = rows === null || rows === void 0 ? void 0 : rows.filter((item) => item.status === "completed").length;
			const cancelled = rows === null || rows === void 0 ? void 0 : rows.filter((item) => item.status === "cancelled").length;
			_pisell_materials.PisellModal.confirm({
				title: _pisell_utils.locales.getText("page.order.cancel.title")(_pisell_utils.locales.getText("pisell2.orderList.orderStatus.completed")),
				cancelText: _pisell_utils.locales.getText("pisell2.orderList.batchAction.button.cancel"),
				okText: _pisell_utils.locales.getText("pisell2.orderList.batchAction.button.ok"),
				content: _pisell_utils.locales.getText("page.order.lists.cancel.content")(open, completed, cancelled, _pisell_utils.locales.getText("pisell2.orderList.orderStatus.open"), true, ""),
				onOk: function() {
					var _ref11 = require_asyncToGenerator._asyncToGenerator(function* () {
						try {
							setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { markAsCompleted: true }));
							yield require_serve.batchPutOrderStatus({
								order_ids: keys,
								status: "completed"
							});
							handleSuccess();
						} catch (error) {
							handleError();
						} finally {
							setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { markAsCompleted: false }));
						}
					});
					return function onOk() {
						return _ref11.apply(this, arguments);
					};
				}()
			});
		});
		return function(_x11, _x12) {
			return _ref12.apply(this, arguments);
		};
	}(), [handleSuccess, handleError]);
	const handleMarkAsCancelled = (0, react.useCallback)(function() {
		var _ref14 = require_asyncToGenerator._asyncToGenerator(function* (keys, rows) {
			const open = rows === null || rows === void 0 ? void 0 : rows.filter((item) => item.status === "open").length;
			const completed = rows === null || rows === void 0 ? void 0 : rows.filter((item) => item.status === "completed").length;
			const cancelled = rows === null || rows === void 0 ? void 0 : rows.filter((item) => item.status === "cancelled").length;
			const executeCancelOrder = function() {
				var _ref13 = require_asyncToGenerator._asyncToGenerator(function* (isVoid) {
					try {
						setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { markAsCancelled: true }));
						yield require_serve.batchPutOrderStatus(require_objectSpread2._objectSpread2({
							order_ids: keys,
							status: "cancelled"
						}, isVoid === 1 && { is_void: isVoid }));
						handleSuccess();
						modalInstance.destroy();
					} catch (error) {
						handleError();
					} finally {
						setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { markAsCancelled: false }));
					}
				});
				return function executeCancelOrder(_x13) {
					return _ref13.apply(this, arguments);
				};
			}();
			const modalInstance = _pisell_materials.PisellModal.confirm({
				title: _pisell_utils.locales.getText("page.order.cancel.title")(_pisell_utils.locales.getText("pisell2.orderList.orderStatus.cancelled")),
				content: _pisell_utils.locales.getText("page.order.lists.cancel.content")(open, completed, cancelled, _pisell_utils.locales.getText("pisell2.orderList.orderStatus.cancelled"), true, ""),
				cancelText: _pisell_utils.locales.getText("pisell2.orderList.batchAction.button.cancel"),
				footer: () => /* @__PURE__ */ react.default.createElement("div", { style: {
					display: "flex",
					gap: 10,
					justifyContent: "flex-end",
					width: "100%"
				} }, /* @__PURE__ */ react.default.createElement(antd.Button, {
					key: "cancel",
					onClick: () => modalInstance.destroy()
				}, _pisell_utils.locales.getText("pisell2.orderList.batchAction.button.cancel")), /* @__PURE__ */ react.default.createElement(antd.Button, {
					key: "only-cancel",
					onClick: () => executeCancelOrder(0),
					loading: loadingStates.markAsCancelled
				}, _pisell_utils.locales.getText("pisell2.orderList.batchAction.button.onlyCancel")), /* @__PURE__ */ react.default.createElement(antd.Button, {
					key: "cancel-void",
					type: "primary",
					onClick: () => executeCancelOrder(1),
					loading: loadingStates.markAsCancelled
				}, _pisell_utils.locales.getText("pisell2.orderList.batchAction.button.cancelAndVoid")))
			});
		});
		return function(_x14, _x15) {
			return _ref14.apply(this, arguments);
		};
	}(), [
		handleSuccess,
		handleError,
		loadingStates.markAsCancelled
	]);
	return (0, react.useMemo)(() => [
		{
			key: "ship",
			label: _pisell_utils.locales.getText("page.order.table.shipping_status.mark.fulfilled"),
			icon: "pisell2-check-circle-broken",
			loading: loadingStates.ship,
			onClick: handleShip
		},
		{
			key: "invoice",
			label: _pisell_utils.locales.getText("page.order.table.list.button.send.invoice"),
			icon: "pisell2-sticker-square",
			loading: loadingStates.invoice,
			onClick: handleInvoice
		},
		{
			key: "print-pack",
			label: _pisell_utils.locales.getText("page.order.table.list.printpack"),
			icon: "pisell2-cube-01",
			onClick: handlePrintPack
		},
		{
			key: "print-picking",
			label: _pisell_utils.locales.getText("page.order.table.list.printpink"),
			icon: "pisell2-package",
			onClick: handlePrintPicking
		},
		{
			key: "add-tags",
			label: _pisell_utils.locales.getText("page.order.text.addtags.button"),
			icon: "pisell2-tag-01",
			loading: loadingStates.addTags,
			onClick: handleAddTags
		},
		{
			key: "remove-tags",
			label: _pisell_utils.locales.getText("page.order.table.list.deletetag"),
			icon: "pisell2-tag-01",
			loading: loadingStates.removeTags,
			onClick: handleRemoveTags
		},
		{
			key: "mark-as-open",
			label: _pisell_utils.locales.getText("page.order.open"),
			icon: "pisell2-clipboard",
			loading: loadingStates.markAsOpen,
			onClick: handleMarkAsOpen
		},
		{
			key: "mark-as-completed",
			label: _pisell_utils.locales.getText("page.order.completeds"),
			icon: "pisell2-clipboard-check",
			loading: loadingStates.markAsCompleted,
			onClick: handleMarkAsCompleted
		},
		{
			key: "mark-as-cancelled",
			label: _pisell_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
	]);
};
//#endregion
exports.default = useActions;
