const require_runtime = require("../../../../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_index = require("../../../../../../booking/components/customSelect/index.js");
const require_useEngineContext = require("../../../../../../../hooks/useEngineContext.js");
const require_useComponents = require("../../../../../../../hooks/useComponents.js");
const require_model = require("../../../../../model.js");
const require_serve = require("../../../../serve.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let ahooks = require("ahooks");
require("./index.less");
//#region src/components/schedules/products/ProductDetailDrawer/ProductDetail/SchedulesModule/AddSchedules/index.tsx
const AddSchedules = ({ value, onChange, drawerContainer, isSelf }) => {
	var _state$schedules3;
	const modalRef = (0, react.useRef)(null);
	const { state, dispatch } = (0, react.useContext)(require_model.Context);
	const { appHelper } = require_useEngineContext.default();
	const createSchedules = (0, react.useRef)();
	const AllCreateScheduleRef = (0, react.useRef)(null);
	const [ScheduleCreateType, AllCreateSchedule] = require_useComponents.default({ components: ["ScheduleCreateType", "AllCreateSchedule"] });
	const _getScheduleList = (0, ahooks.useRequest)(require_serve.getSchedules, {
		manual: true,
		onSuccess: (res) => {}
	});
	const onFinish = (e) => {
		const scheduleList = isSelf ? _getScheduleList === null || _getScheduleList === void 0 ? void 0 : _getScheduleList.data : state.schedules.list;
		const idMap = (scheduleList === null || scheduleList === void 0 ? void 0 : scheduleList.reduce((p, c) => {
			return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, p), {}, { [c.id]: c });
		}, {})) || {};
		onChange(e.map((item) => idMap[item.id]));
	};
	const handleCreateSchedule = () => {
		var _createSchedules$curr, _createSchedules$curr2;
		(_createSchedules$curr = createSchedules.current) === null || _createSchedules$curr === void 0 || (_createSchedules$curr2 = _createSchedules$curr.open) === null || _createSchedules$curr2 === void 0 || _createSchedules$curr2.call(_createSchedules$curr, { getContainer: void 0 });
	};
	const handleAddSchedule = (data) => {
		if (isSelf) _getScheduleList.mutate((oldData) => [data, ...oldData || []]);
		else {
			var _state$schedules;
			dispatch({
				type: "setSchedules",
				payload: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, state.schedules), {}, { list: [data, ...(state === null || state === void 0 || (_state$schedules = state.schedules) === null || _state$schedules === void 0 ? void 0 : _state$schedules.list) || []] })
			});
		}
		modalRef.current.setValue((v) => [...v, data]);
		setTimeout(() => {
			var _document$getElementB, _document$getElementB2;
			(_document$getElementB = document.getElementById(data.id)) === null || _document$getElementB === void 0 || (_document$getElementB2 = _document$getElementB.scrollIntoView) === null || _document$getElementB2 === void 0 || _document$getElementB2.call(_document$getElementB, {
				block: "center",
				behavior: "smooth"
			});
		});
	};
	const handleEdit = (e, id) => {
		var _AllCreateScheduleRef, _AllCreateScheduleRef2;
		e.stopPropagation();
		(_AllCreateScheduleRef = AllCreateScheduleRef.current) === null || _AllCreateScheduleRef === void 0 || (_AllCreateScheduleRef2 = _AllCreateScheduleRef.open) === null || _AllCreateScheduleRef2 === void 0 || _AllCreateScheduleRef2.call(_AllCreateScheduleRef, { id });
	};
	const handleChangeResData = (data) => {
		const updateOldData = (oldData) => {
			return (oldData || []).map((item) => {
				if (item.id === data.id) {
					var _appHelper$utils;
					return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, data), {}, { name: (appHelper === null || appHelper === void 0 || (_appHelper$utils = appHelper.utils) === null || _appHelper$utils === void 0 ? void 0 : _appHelper$utils.translationShop(data.name)) || data.name });
				}
				return item;
			});
		};
		if (isSelf) _getScheduleList.mutate(updateOldData);
		else {
			var _state$schedules2;
			dispatch({
				type: "setSchedules",
				payload: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, state.schedules), {}, { list: updateOldData(state === null || state === void 0 || (_state$schedules2 = state.schedules) === null || _state$schedules2 === void 0 ? void 0 : _state$schedules2.list) })
			});
		}
	};
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(antd.Button, {
		ghost: true,
		type: "primary",
		className: "product-detail-card-module-add-button",
		onClick: () => {
			var _modalRef$current;
			(_modalRef$current = modalRef.current) === null || _modalRef$current === void 0 || _modalRef$current.open();
			if (isSelf) _getScheduleList.run({
				num: 999,
				skip: 1
			});
		}
	}, _pisell_utils.locales.getText("pisell2.text.schedules.assign-schedule")), /* @__PURE__ */ react.default.createElement(require_index.default, {
		ref: modalRef,
		rowKey: "id",
		options: isSelf ? _getScheduleList.data || [] : (state === null || state === void 0 || (_state$schedules3 = state.schedules) === null || _state$schedules3 === void 0 ? void 0 : _state$schedules3.list) || [],
		createItemText: _pisell_utils.locales.getText("pisell2.text.schedules.create-new-schedule"),
		loading: _getScheduleList.loading,
		value,
		onValueChange: onFinish,
		handleCreateItem: handleCreateSchedule,
		isPreview: false,
		drawerProps: require_objectSpread2._objectSpread2({
			title: _pisell_utils.locales.getText("pisell2.text.schedules.assign-schedule"),
			destroyOnClose: true,
			width: 480,
			getContainer: drawerContainer || document.querySelector(".pisell-lowcode__schedules-component-products") || document.body,
			rootStyle: { position: "absolute" },
			contentWrapperStyle: { boxShadow: "none" },
			titleNoWrap: true
		}, isSelf ? { zIndex: 1e3 } : {}),
		mode: "multiple",
		optionRender: (item) => {
			return /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__add-schedule-list-item" }, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__add-schedule-list-item-content" }, /* @__PURE__ */ react.default.createElement("div", {
				className: "pisell-lowcode__add-schedule-list-item-color",
				style: { backgroundColor: item.color }
			}), item.name), /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__add-schedule-list-item-action-wrap" }, /* @__PURE__ */ react.default.createElement("div", {
				className: "pisell-lowcode__add-schedule-list-item-action-edit",
				onClick: (e) => handleEdit(e, item.id)
			}, _pisell_utils.locales.getText("pisell2.text.edit"))));
		},
		filterItem: (d, search) => {
			return d.name.toLowerCase().indexOf(search.toLowerCase()) !== -1;
		}
	}), ScheduleCreateType && /* @__PURE__ */ react.default.createElement(ScheduleCreateType, {
		ref: createSchedules,
		onChange: handleAddSchedule
	}), AllCreateSchedule && /* @__PURE__ */ react.default.createElement(AllCreateSchedule, {
		ref: AllCreateScheduleRef,
		onChange: handleChangeResData
	}));
};
//#endregion
exports.default = AddSchedules;
