import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { DEFAULT_ACTION_CONFIG, getActionConfig, getActionKey, getSortedVisibleActionItems, mergeActionItem, toButtonGroupList, toDropdownMenuItem } from "./utils.js";
import { useCancelActionItem, useCheckoutActionItem, useEditActionItem, useNoteActionItem, useRefundActionItem, useReturnActionItem, useSaveActionItem } from "./orderActions.js";
import "./types.js";
import { usePrintActionItem, usePrintAllActionItem, usePrintProductVoucherActionItem, usePrintReceiptsActionItem, usePrintWristbandActionItem } from "./printActions.js";
import { useMemo } from "react";
//#region src/plus/saleDetail/hooks/saleDetailActions/index.ts
const DEFAULT_SALE_DETAIL_ACTIONS = ["checkout"];
function useMoreActionItem({ printReceiptsAction, printWristbandAction, cancelAction }) {
	return useMemo(() => ({
		id: "more",
		isMore: true,
		buttonProps: {
			block: true,
			className: "pisell-sale-detail-button-actions__button"
		},
		menuData: [
			toDropdownMenuItem(printReceiptsAction),
			toDropdownMenuItem(printWristbandAction),
			toDropdownMenuItem(cancelAction)
		]
	}), [
		cancelAction,
		printReceiptsAction,
		printWristbandAction
	]);
}
function useSaleDetailActions({ dataSource, actions = DEFAULT_SALE_DETAIL_ACTIONS }) {
	var _dataSource$sales$tem;
	const editAction = useEditActionItem({ dataSource });
	const refundAction = useRefundActionItem({ dataSource });
	const returnAction = useReturnActionItem({ dataSource });
	const printAction = usePrintActionItem();
	const printProductVoucherAction = usePrintProductVoucherActionItem({ dataSource });
	const printReceiptsAction = usePrintReceiptsActionItem({ dataSource });
	const printWristbandAction = usePrintWristbandActionItem({ dataSource });
	const printAllAction = usePrintAllActionItem({ dataSource });
	const cancelAction = useCancelActionItem({ dataSource });
	const noteAction = useNoteActionItem({ dataSource });
	const saveAction = useSaveActionItem({ dataSource });
	const checkoutAction = useCheckoutActionItem({ dataSource });
	const moreAction = useMoreActionItem({
		printReceiptsAction,
		printWristbandAction,
		cancelAction
	});
	const isCancelled = ((_dataSource$sales$tem = dataSource.sales.tempOrder) === null || _dataSource$sales$tem === void 0 ? void 0 : _dataSource$sales$tem.status) === "cancelled";
	return useMemo(() => {
		if (isCancelled) return [];
		const actionItemMap = {
			edit: editAction,
			refund: refundAction,
			return: returnAction,
			print: printAction,
			"print-product-voucher": printProductVoucherAction,
			"print-receipts": printReceiptsAction,
			"print-wristband": printWristbandAction,
			"print-all": printAllAction,
			cancel: cancelAction,
			note: noteAction,
			save: saveAction,
			checkout: checkoutAction,
			more: moreAction
		};
		const resolveActionItem = (action) => {
			var _config$items;
			const actionKey = getActionKey(action);
			const actionConfig = getActionConfig(action);
			const config = _objectSpread2(_objectSpread2(_objectSpread2({}, DEFAULT_ACTION_CONFIG[actionKey]), actionConfig), {}, { buttonProps: _objectSpread2(_objectSpread2({}, DEFAULT_ACTION_CONFIG[actionKey].buttonProps), actionConfig.buttonProps) });
			const actionItem = mergeActionItem(actionItemMap[actionKey], config);
			if (!((_config$items = config.items) === null || _config$items === void 0 ? void 0 : _config$items.length)) return actionItem;
			const nestedActionItems = getSortedVisibleActionItems(config.items.map(resolveActionItem));
			if (nestedActionItems.length === 1) return _objectSpread2(_objectSpread2({}, nestedActionItems[0]), {}, {
				row: actionItem.row,
				order: actionItem.order
			});
			const menuData = nestedActionItems.map(toDropdownMenuItem);
			return _objectSpread2(_objectSpread2({}, actionItem), {}, {
				isMore: true,
				menuData
			});
		};
		return toButtonGroupList(actions.map(resolveActionItem));
	}, [
		actions,
		cancelAction,
		checkoutAction,
		editAction,
		isCancelled,
		moreAction,
		noteAction,
		printAction,
		printAllAction,
		printProductVoucherAction,
		printReceiptsAction,
		printWristbandAction,
		refundAction,
		returnAction,
		saveAction
	]);
}
//#endregion
export { useSaleDetailActions };
