import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import useEngineContext from "../../../hooks/useEngineContext.js";
import { batchAddOrderTags, batchDeleteOrderTags, batchPutOrderStatus, editordership, getTagSelectOptions, postSendInvoice, printPack, printPicking } from "../../orderList/serve.js";
import React, { useCallback, useMemo, useState } from "react";
import { Button, Checkbox } from "antd";
import { locales } from "@pisell/utils";
import { PisellModal, PisellQuickFilter } from "@pisell/materials";
//#region src/plus/pisellSalesGrid/hooks/useActions.tsx
const useActions = (options = {}) => {
	const { onClearSelection, onRefresh } = options;
	const context = useEngineContext();
	const [loadingStates, setLoadingStates] = useState({});
	const handleSuccess = useCallback((successMessage) => {
		onRefresh === null || onRefresh === void 0 || onRefresh();
		onClearSelection === null || onClearSelection === void 0 || onClearSelection();
	}, [onRefresh, onClearSelection]);
	const handleError = useCallback((error, defaultMessage) => {}, []);
	const handleShip = useCallback(function() {
		var _ref2 = _asyncToGenerator(function* (keys, rows) {
			let checked = true;
			PisellModal.confirm({
				title: locales.getText("pisell2.orderList.batchAction.ship.title")(keys.length),
				cancelText: locales.getText("pisell2.orderList.batchAction.button.cancel"),
				okText: locales.getText("pisell2.orderList.batchAction.button.ok"),
				content: /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement("p", null, locales.getText("pisell2.orderList.batchAction.ship.content")), /* @__PURE__ */ React.createElement(Checkbox, {
					defaultChecked: true,
					onChange: (e) => {
						checked = e.target.checked;
					}
				}, locales.getText("page.order.fa.check"))),
				onOk: function() {
					var _ref = _asyncToGenerator(function* () {
						try {
							setLoadingStates((prev) => _objectSpread2(_objectSpread2({}, prev), {}, { ship: true }));
							yield editordership({
								ids: keys,
								is_notice: checked ? 1 : 0
							});
							handleSuccess();
						} catch (error) {
							handleError();
						} finally {
							setLoadingStates((prev) => _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 = useCallback(function() {
		var _ref4 = _asyncToGenerator(function* (keys, rows) {
			PisellModal.confirm({
				title: locales.getText("page.order.table.list.button.send.invoice.tip"),
				cancelText: locales.getText("pisell2.orderList.batchAction.button.cancel"),
				okText: locales.getText("pisell2.orderList.batchAction.button.ok"),
				onOk: function() {
					var _ref3 = _asyncToGenerator(function* () {
						try {
							setLoadingStates((prev) => _objectSpread2(_objectSpread2({}, prev), {}, { invoice: true }));
							yield postSendInvoice({ order_ids: keys });
							handleSuccess();
						} catch (error) {
							handleError();
						} finally {
							setLoadingStates((prev) => _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 = 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;
		}
		printPack({
			host: "",
			shop_id: shopId,
			ids: keys
		});
	}, [context]);
	const handlePrintPicking = 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;
		}
		printPicking({
			host: "",
			shop_id: shopId,
			ids: keys
		});
	}, [context]);
	const handleAddTags = useCallback(function() {
		var _ref6 = _asyncToGenerator(function* (keys, rows) {
			let tags = [];
			PisellModal.confirm({
				title: locales.getText("pisell2.orderList.batchAction.addTags.title")(keys.length),
				width: 520,
				cancelText: locales.getText("pisell2.orderList.batchAction.button.cancel"),
				okText: locales.getText("pisell2.orderList.batchAction.button.ok"),
				content: /* @__PURE__ */ React.createElement("div", { style: { paddingTop: 16 } }, /* @__PURE__ */ React.createElement("div", { style: {
					marginBottom: 8,
					color: "#666"
				} }, locales.getText("pisell2.orderList.column.tags")), /* @__PURE__ */ React.createElement(PisellQuickFilter, {
					hasForm: false,
					filterList: [{
						name: "tag_ids",
						type: "select",
						key: "tag_ids",
						label: 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: locales.getText("pisell2.orderList.filter.tags.placeholder"),
							options: (params) => getTagSelectOptions(params).then((res) => res.map((item) => ({
								label: item.name,
								value: item.id
							})))
						}
					}]
				})),
				onOk: function() {
					var _ref5 = _asyncToGenerator(function* () {
						try {
							setLoadingStates((prev) => _objectSpread2(_objectSpread2({}, prev), {}, { addTags: true }));
							yield batchAddOrderTags({
								name: tags,
								ids: keys
							});
							handleSuccess();
						} catch (error) {
							handleError();
						} finally {
							setLoadingStates((prev) => _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 = useCallback(function() {
		var _ref8 = _asyncToGenerator(function* (keys, rows) {
			let tags = [];
			PisellModal.confirm({
				title: locales.getText("pisell2.orderList.batchAction.removeTags.title")(keys.length),
				width: 520,
				cancelText: locales.getText("pisell2.orderList.batchAction.button.cancel"),
				okText: locales.getText("pisell2.orderList.batchAction.button.ok"),
				content: /* @__PURE__ */ React.createElement("div", { style: { paddingTop: 16 } }, /* @__PURE__ */ React.createElement("div", { style: {
					marginBottom: 8,
					color: "#666"
				} }, locales.getText("pisell2.orderList.column.tags")), /* @__PURE__ */ React.createElement(PisellQuickFilter, {
					hasForm: false,
					filterList: [{
						name: "tag_ids",
						type: "select",
						key: "tag_ids",
						label: 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: locales.getText("pisell2.orderList.filter.tags.placeholder"),
							options: (params) => getTagSelectOptions(params).then((res) => res.map((item) => ({
								label: item.name,
								value: item.id
							})))
						}
					}]
				})),
				onOk: function() {
					var _ref7 = _asyncToGenerator(function* () {
						try {
							setLoadingStates((prev) => _objectSpread2(_objectSpread2({}, prev), {}, { removeTags: true }));
							yield batchDeleteOrderTags({
								name: tags,
								ids: keys
							});
							handleSuccess();
						} catch (error) {
							handleError();
						} finally {
							setLoadingStates((prev) => _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 = useCallback(function() {
		var _ref10 = _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;
			PisellModal.confirm({
				title: locales.getText("page.order.cancel.title")(locales.getText("pisell2.orderList.orderStatus.open")),
				cancelText: locales.getText("pisell2.orderList.batchAction.button.cancel"),
				okText: locales.getText("pisell2.orderList.batchAction.button.ok"),
				content: locales.getText("page.order.lists.cancel.content")(open, completed, cancelled, locales.getText("pisell2.orderList.orderStatus.completed"), true, "void"),
				onOk: function() {
					var _ref9 = _asyncToGenerator(function* () {
						try {
							setLoadingStates((prev) => _objectSpread2(_objectSpread2({}, prev), {}, { markAsOpen: true }));
							yield batchPutOrderStatus({
								order_ids: keys,
								status: "open"
							});
							handleSuccess();
						} catch (error) {
							handleError();
						} finally {
							setLoadingStates((prev) => _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 = useCallback(function() {
		var _ref12 = _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;
			PisellModal.confirm({
				title: locales.getText("page.order.cancel.title")(locales.getText("pisell2.orderList.orderStatus.completed")),
				cancelText: locales.getText("pisell2.orderList.batchAction.button.cancel"),
				okText: locales.getText("pisell2.orderList.batchAction.button.ok"),
				content: locales.getText("page.order.lists.cancel.content")(open, completed, cancelled, locales.getText("pisell2.orderList.orderStatus.open"), true, ""),
				onOk: function() {
					var _ref11 = _asyncToGenerator(function* () {
						try {
							setLoadingStates((prev) => _objectSpread2(_objectSpread2({}, prev), {}, { markAsCompleted: true }));
							yield batchPutOrderStatus({
								order_ids: keys,
								status: "completed"
							});
							handleSuccess();
						} catch (error) {
							handleError();
						} finally {
							setLoadingStates((prev) => _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 = useCallback(function() {
		var _ref14 = _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 = _asyncToGenerator(function* (isVoid) {
					try {
						setLoadingStates((prev) => _objectSpread2(_objectSpread2({}, prev), {}, { markAsCancelled: true }));
						yield batchPutOrderStatus(_objectSpread2({
							order_ids: keys,
							status: "cancelled"
						}, isVoid === 1 && { is_void: isVoid }));
						handleSuccess();
						modalInstance.destroy();
					} catch (error) {
						handleError();
					} finally {
						setLoadingStates((prev) => _objectSpread2(_objectSpread2({}, prev), {}, { markAsCancelled: false }));
					}
				});
				return function executeCancelOrder(_x13) {
					return _ref13.apply(this, arguments);
				};
			}();
			const modalInstance = PisellModal.confirm({
				title: locales.getText("page.order.cancel.title")(locales.getText("pisell2.orderList.orderStatus.cancelled")),
				content: locales.getText("page.order.lists.cancel.content")(open, completed, cancelled, locales.getText("pisell2.orderList.orderStatus.cancelled"), true, ""),
				cancelText: locales.getText("pisell2.orderList.batchAction.button.cancel"),
				footer: () => /* @__PURE__ */ React.createElement("div", { style: {
					display: "flex",
					gap: 10,
					justifyContent: "flex-end",
					width: "100%"
				} }, /* @__PURE__ */ React.createElement(Button, {
					key: "cancel",
					onClick: () => modalInstance.destroy()
				}, locales.getText("pisell2.orderList.batchAction.button.cancel")), /* @__PURE__ */ React.createElement(Button, {
					key: "only-cancel",
					onClick: () => executeCancelOrder(0),
					loading: loadingStates.markAsCancelled
				}, locales.getText("pisell2.orderList.batchAction.button.onlyCancel")), /* @__PURE__ */ React.createElement(Button, {
					key: "cancel-void",
					type: "primary",
					onClick: () => executeCancelOrder(1),
					loading: loadingStates.markAsCancelled
				}, locales.getText("pisell2.orderList.batchAction.button.cancelAndVoid")))
			});
		});
		return function(_x14, _x15) {
			return _ref14.apply(this, arguments);
		};
	}(), [
		handleSuccess,
		handleError,
		loadingStates.markAsCancelled
	]);
	return useMemo(() => [
		{
			key: "ship",
			label: locales.getText("page.order.table.shipping_status.mark.fulfilled"),
			icon: "pisell2-check-circle-broken",
			loading: loadingStates.ship,
			onClick: handleShip
		},
		{
			key: "invoice",
			label: locales.getText("page.order.table.list.button.send.invoice"),
			icon: "pisell2-sticker-square",
			loading: loadingStates.invoice,
			onClick: handleInvoice
		},
		{
			key: "print-pack",
			label: locales.getText("page.order.table.list.printpack"),
			icon: "pisell2-cube-01",
			onClick: handlePrintPack
		},
		{
			key: "print-picking",
			label: locales.getText("page.order.table.list.printpink"),
			icon: "pisell2-package",
			onClick: handlePrintPicking
		},
		{
			key: "add-tags",
			label: locales.getText("page.order.text.addtags.button"),
			icon: "pisell2-tag-01",
			loading: loadingStates.addTags,
			onClick: handleAddTags
		},
		{
			key: "remove-tags",
			label: locales.getText("page.order.table.list.deletetag"),
			icon: "pisell2-tag-01",
			loading: loadingStates.removeTags,
			onClick: handleRemoveTags
		},
		{
			key: "mark-as-open",
			label: locales.getText("page.order.open"),
			icon: "pisell2-clipboard",
			loading: loadingStates.markAsOpen,
			onClick: handleMarkAsOpen
		},
		{
			key: "mark-as-completed",
			label: locales.getText("page.order.completeds"),
			icon: "pisell2-clipboard-check",
			loading: loadingStates.markAsCompleted,
			onClick: handleMarkAsCompleted
		},
		{
			key: "mark-as-cancelled",
			label: 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
export { useActions as default };
