const require_runtime = require("../../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_mock = require("../../../mock.js");
const require_utils = require("../../../utils.js");
const require_model = require("../../model.js");
const require_index = require("../serviceItem/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
require("./index.less");
//#region src/components/booking/info/service/serviceList/index.tsx
const ServiceList = ({ onEdit }) => {
	var _state$service;
	const { state, dispatch } = (0, react.useContext)(require_model.Context);
	const errorTypes = {
		product: _pisell_utils.locales.getText("pisell2.text.status.service-error-1"),
		time: _pisell_utils.locales.getText("pisell2.text.status.service-error-2"),
		limit: _pisell_utils.locales.getText("pisell2.text.status.service-error-3"),
		maxLimit: _pisell_utils.locales.getText("pisell2.text.status.service-error-3")
	};
	const handleEditService = (item) => {
		var _window$openBookingEd, _window;
		if (state.disabledEdit || state.disableEventBookingEdit) return;
		(_window$openBookingEd = (_window = window).openBookingEditModal) === null || _window$openBookingEd === void 0 || _window$openBookingEd.call(_window);
		dispatch({
			type: "setService",
			payload: {
				editModal: true,
				cacheItem: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, { new: 0 })
			}
		});
		onEdit === null || onEdit === void 0 || onEdit(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, { new: 0 }));
	};
	const handleDeleteService = (item) => {
		var _window$closeBookingE, _window2;
		if (state.disabledEdit || state.disableEventBookingEdit || state.channelDisabledEdit) return;
		let _list = [...state.service.value];
		_list = _list.filter((d) => d._id !== item._id);
		(_window$closeBookingE = (_window2 = window).closeBookingEditModal) === null || _window$closeBookingE === void 0 || _window$closeBookingE.call(_window2);
		dispatch({
			type: "setService",
			payload: {
				editModal: false,
				value: _list,
				edit: true,
				cacheItem: null
			}
		});
	};
	const getResourceItem = (0, react.useCallback)((id) => {
		if (id === 0) return require_mock.getNoResource();
		return state.service.resources.find((d) => d.form_record_id === id);
	}, [state.service.resources]);
	const getErrorMessage = (0, react.useCallback)((service, resourceItem) => {
		const errorList = require_utils.getErrorList(resourceItem, service);
		let errorMessage = "";
		if (!service.id) return errorList[0].label;
		if (errorList.length) if (errorList.length > 1) errorMessage = `${errorList.length} ${_pisell_utils.locales.getText("pisell2.text.warnings")}`;
		else errorMessage = (resourceItem === null || resourceItem === void 0 ? void 0 : resourceItem.labelText) + " " + errorTypes[errorList[0].type];
		return errorMessage;
	}, []);
	const handleLike = (status, item) => {
		if (state.disabledEdit) return;
		let _list = [...state.service.value];
		let _id = item === null || item === void 0 ? void 0 : item._id;
		_list = _list.map((d) => d._id === _id ? require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, d), {}, { _extend: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, d._extend), {}, { like_status: status }) }) : d);
		dispatch({
			type: "setService",
			payload: {
				value: _list,
				edit: true
			}
		});
	};
	return /* @__PURE__ */ react.default.createElement("div", { className: "" }, (_state$service = state.service) === null || _state$service === void 0 || (_state$service = _state$service.value) === null || _state$service === void 0 ? void 0 : _state$service.map((d) => {
		var _d$_extend;
		const resourceItem = getResourceItem((_d$_extend = d._extend) === null || _d$_extend === void 0 ? void 0 : _d$_extend.resource);
		let errorMessage = getErrorMessage(d, resourceItem);
		return /* @__PURE__ */ react.default.createElement(require_index.default, {
			key: d._id,
			getResourceItem,
			errorMessage,
			resourceItem,
			d,
			handleEditService,
			state,
			handleLike,
			handleDeleteService
		});
	}));
};
//#endregion
exports.default = ServiceList;
