const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_useEngineContext = require("../../../hooks/useEngineContext.js");
const require_index = require("../../../businessModel/SalesModel/Sales/index.js");
const require_index$1 = require("../Customer/index.js");
const require_index$2 = require("../Cart/index.js");
const require_serve = require("./serve.js");
let react = require("react");
react = require_runtime.__toESM(react);
let ahooks = require("ahooks");
//#region src/components/Sales/Holder/index.tsx
const defaultState = {};
const HolderContext = (0, react.createContext)(void 0);
const HolderProvider = ({ children }) => {
	var _data$holderData3, _data$holderData5;
	const { getUserInfo } = require_index$1.useCustomer();
	const { data, setData, getCurrentData } = require_index.useSales();
	const { createCartItemFromHolder, addHolderToCartItem, deleteCartItemByHolder } = require_index$2.useCart();
	const [open, setOpen] = (0, react.useState)(false);
	const utils = require_useEngineContext.default().appHelper.utils || {};
	const holderData = (0, react.useMemo)(() => {
		return data.holderData || {};
	}, [data]);
	const setHolderData = (newHolderData) => {
		setData({ holderData: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, getCurrentData().holderData), newHolderData) });
	};
	const getHolderList = (0, ahooks.useRequest)(require_serve.getHolderListApi, {
		manual: true,
		onSuccess: (res) => {
			var _res$data, _res$data$forEach;
			const list = [];
			(_res$data = res.data) === null || _res$data === void 0 || (_res$data = _res$data.list) === null || _res$data === void 0 || (_res$data$forEach = _res$data.forEach) === null || _res$data$forEach === void 0 || _res$data$forEach.call(_res$data, (v) => {
				if (!(selectedHolderIds || []).includes(v.form_record_id)) list.push(createHolderItem(v));
			});
			setHolderData({ holderList: list });
		}
	});
	const createHolder = (0, ahooks.useRequest)(require_serve.createHolderApi, { manual: true });
	const refreshHolder = () => {
		var _getUserInfo, _data$initData, _data$initData2;
		const customerId = (_getUserInfo = getUserInfo()) === null || _getUserInfo === void 0 ? void 0 : _getUserInfo.id;
		getHolderList.run({
			form_id: (_data$initData = data.initData) === null || _data$initData === void 0 || (_data$initData = _data$initData.boardSetting) === null || _data$initData === void 0 || (_data$initData = _data$initData.config) === null || _data$initData === void 0 || (_data$initData = _data$initData.basic) === null || _data$initData === void 0 || (_data$initData = _data$initData.appointment_subject) === null || _data$initData === void 0 ? void 0 : _data$initData.form_id,
			shop_id: (_data$initData2 = data.initData) === null || _data$initData2 === void 0 ? void 0 : _data$initData2.shop_id,
			customer_id: customerId
		});
	};
	const updateHolderList = (holderItem) => {
		if (holderItem) {
			var _getCurrentData$holde;
			const holderList = ((_getCurrentData$holde = getCurrentData().holderData) === null || _getCurrentData$holde === void 0 ? void 0 : _getCurrentData$holde.holderList) || [];
			setHolderData({ holderList: [createHolderItem(holderItem), ...holderList] });
		}
	};
	const createHolderItem = (item) => {
		return {
			id: item.form_record_id,
			key: item.form_record_id,
			name: item.main_field,
			label: item.main_field,
			avatar: item.customer_cover,
			_originData: item
		};
	};
	/**
	* @title:
	* @description: 选择holder，分为两种情况：
	* 1.将没有holder信息的商品加上holder信息并更新购物车；
	* 2.用选中的holder信息创建一个购物车商品
	* @param {any} holderItem
	* @param {'addToCart' | 'createCartItem'} type
	* @return {void}
	* @Author: Xxf
	* @Date: 2025-01-20 11:23
	*/
	const selectHolder = (holderItem, type) => {
		var _data$holderData;
		if (type === "addToCart") addHolderToCartItem(holderItem);
		else if (type === "createCartItem") createCartItemFromHolder(holderItem);
		setHolderData({ currentHolderItem: holderItem });
		const selectedHolderItems = ((_data$holderData = data.holderData) === null || _data$holderData === void 0 ? void 0 : _data$holderData.selectedHolderItems) || [];
		if (selectedHolderItems.find((v) => v.id === holderItem.id) === void 0) setHolderData({ selectedHolderItems: [...selectedHolderItems, holderItem] });
	};
	const onHolderOpen = () => {
		setOpen(true);
		refreshHolder();
	};
	const onOpenCreateHolder = ({ form_title, callback }) => {
		var _data$initData3;
		utils.action({
			type: "pisell1.form",
			data: {
				form_id: (_data$initData3 = data.initData) === null || _data$initData3 === void 0 || (_data$initData3 = _data$initData3.boardSetting) === null || _data$initData3 === void 0 || (_data$initData3 = _data$initData3.config) === null || _data$initData3 === void 0 || (_data$initData3 = _data$initData3.basic) === null || _data$initData3 === void 0 || (_data$initData3 = _data$initData3.appointment_subject) === null || _data$initData3 === void 0 ? void 0 : _data$initData3.form_id,
				form_title,
				isV2API: true
			},
			callback
		});
	};
	const onHolderClose = () => {
		setOpen(false);
	};
	const deleteHolderById = (holderId) => {
		var _getCurrentData$holde2;
		setHolderData({ selectedHolderItems: (_getCurrentData$holde2 = getCurrentData().holderData) === null || _getCurrentData$holde2 === void 0 || (_getCurrentData$holde2 = _getCurrentData$holde2.selectedHolderItems) === null || _getCurrentData$holde2 === void 0 ? void 0 : _getCurrentData$holde2.filter((v) => v.id !== holderId) });
		deleteCartItemByHolder(holderId);
	};
	const selectedHolderIds = (0, react.useMemo)(() => {
		var _data$holderData2, _data$holderData2$map;
		return (_data$holderData2 = data.holderData) === null || _data$holderData2 === void 0 || (_data$holderData2 = _data$holderData2.selectedHolderItems) === null || _data$holderData2 === void 0 || (_data$holderData2$map = _data$holderData2.map) === null || _data$holderData2$map === void 0 ? void 0 : _data$holderData2$map.call(_data$holderData2, (v) => {
			return v.id;
		});
	}, [(_data$holderData3 = data.holderData) === null || _data$holderData3 === void 0 ? void 0 : _data$holderData3.selectedHolderItems]);
	const contextValue = {
		holderData,
		getHolderList,
		selectHolder,
		open,
		onOpenCreateHolder,
		onHolderOpen,
		onHolderClose,
		createHolder,
		refreshHolder,
		deleteHolderById,
		selectedHolderIds,
		updateHolderList,
		defaultSelectedHolder: (0, react.useMemo)(() => {
			var _data$holderData4;
			const lists = ((_data$holderData4 = data.holderData) === null || _data$holderData4 === void 0 ? void 0 : _data$holderData4.holderList) || [];
			return (lists === null || lists === void 0 ? void 0 : lists[0]) || {};
		}, [(_data$holderData5 = data.holderData) === null || _data$holderData5 === void 0 ? void 0 : _data$holderData5.holderList])
	};
	return /* @__PURE__ */ react.default.createElement(HolderContext.Provider, { value: contextValue }, children);
};
/**
* 使用步骤管理的Hook
* @throws {Error} 如果在StepProvider外部使用会抛出错误
* @returns {StepContextType} 步骤管理上下文
*/
const useHolder = () => {
	const context = react.default.useContext(HolderContext);
	if (!context) throw new Error("useHolder must be used within a HolderProvider");
	return context;
};
var Holder_default = { defaultState };
//#endregion
exports.HolderProvider = HolderProvider;
exports.default = Holder_default;
exports.useHolder = useHolder;
