const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_model = require("../model.js");
const require_index = require("../components/Search/index.js");
const require_index$1 = require("../components/CurrentDay/index.js");
const require_index$2 = require("./add/index.js");
const require_index$3 = require("./edit/index.js");
const require_serve = require("./serve.js");
const require_index$4 = require("./detail/index.js");
const require_index$5 = require("./list/index.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/resources/index.tsx
const Resources = () => {
	const { state, dispatch } = (0, react.useContext)(require_model.Context);
	const { currentSelectDayDetail, resources, tabKey } = state;
	const cacheResource = resources === null || resources === void 0 ? void 0 : resources.cacheResource;
	const resourcesAddRef = (0, react.useRef)();
	const resourcesEditRef = (0, react.useRef)();
	const resourcesDetailRef = (0, react.useRef)();
	const [seachText, setSearchText] = (0, react.useState)("");
	const [activeKey, setActiveKey] = (0, react.useState)([]);
	const [formTypes, setFormTypes] = (0, react.useState)([]);
	const [relationResource, setRelationResource] = (0, react.useState)([]);
	const searchChange = (e) => {
		setSearchText(e);
	};
	const _getFormTypes = (0, ahooks.useRequest)(require_serve.getFormTypes, {
		manual: true,
		onSuccess: (result) => {
			var _result$data;
			const list = (result === null || result === void 0 || (_result$data = result.data) === null || _result$data === void 0 ? void 0 : _result$data.list) || [];
			setFormTypes(list);
			setActiveKey(list.map((item) => item.id));
		}
	});
	const _getRelationResource = (0, ahooks.useRequest)(require_serve.getRelationResource, {
		manual: true,
		onSuccess: (result, params) => {
			var _result$data2;
			setRelationResource((result === null || result === void 0 || (_result$data2 = result.data) === null || _result$data2 === void 0 ? void 0 : _result$data2.list) || []);
		}
	});
	const init = (params) => {
		_getRelationResource.run({
			skip: 1,
			num: 500,
			schedule_ids: currentSelectDayDetail ? currentSelectDayDetail.scheduleIds || [] : void 0,
			exclude_form_code: ["opening_hours"]
		}, params || null);
	};
	(0, react.useEffect)(() => {
		_getFormTypes.run({
			type: "manual",
			num: 500,
			skip: 1
		});
	}, []);
	(0, react.useEffect)(() => {
		init();
	}, [currentSelectDayDetail === null || currentSelectDayDetail === void 0 ? void 0 : currentSelectDayDetail.scheduleIds]);
	(0, react.useEffect)(() => {
		if (cacheResource) {
			var _resourcesEditRef$cur, _resourcesEditRef$cur2;
			(_resourcesEditRef$cur = resourcesEditRef.current) === null || _resourcesEditRef$cur === void 0 || (_resourcesEditRef$cur2 = _resourcesEditRef$cur.open) === null || _resourcesEditRef$cur2 === void 0 || _resourcesEditRef$cur2.call(_resourcesEditRef$cur, cacheResource);
			setTimeout(() => {
				var _el$scrollIntoView;
				const el = document.getElementById(`${cacheResource.id}`);
				console.log("el", el);
				el === null || el === void 0 || (_el$scrollIntoView = el.scrollIntoView) === null || _el$scrollIntoView === void 0 || _el$scrollIntoView.call(el, true);
			}, 100);
		}
	}, [cacheResource]);
	const items = (0, react.useMemo)(() => {
		const _relationResource = relationResource.filter((item) => {
			var _item$title, _item$title$includes;
			return (_item$title = item.title) === null || _item$title === void 0 || (_item$title$includes = _item$title.includes) === null || _item$title$includes === void 0 ? void 0 : _item$title$includes.call(_item$title, seachText);
		});
		return formTypes.reduce((arr, item) => {
			const currentResource = _relationResource.filter((relation) => relation.form_id === item.id);
			if (currentResource.length > 0) arr.push(require_objectSpread2._objectSpread2({
				label: item.title,
				key: item.id,
				children: currentResource
			}, item));
			return arr;
		}, []).map((item) => {
			const resources = item.children || [];
			return {
				label: item.title,
				key: item.id,
				children: /* @__PURE__ */ react.default.createElement(require_index$5.default, {
					list: resources.map((resource) => {
						return {
							id: resource.id,
							title: resource.title,
							form_id: item.id
						};
					}),
					addClick: () => {
						var _resourcesAddRef$curr, _resourcesAddRef$curr2;
						(_resourcesAddRef$curr = resourcesAddRef.current) === null || _resourcesAddRef$curr === void 0 || (_resourcesAddRef$curr2 = _resourcesAddRef$curr.open) === null || _resourcesAddRef$curr2 === void 0 || _resourcesAddRef$curr2.call(_resourcesAddRef$curr, item.id);
					},
					itemClick: (resource) => {
						var _resourcesEditRef$cur3, _resourcesEditRef$cur4;
						(_resourcesEditRef$cur3 = resourcesEditRef.current) === null || _resourcesEditRef$cur3 === void 0 || (_resourcesEditRef$cur4 = _resourcesEditRef$cur3.open) === null || _resourcesEditRef$cur4 === void 0 || _resourcesEditRef$cur4.call(_resourcesEditRef$cur3, resource);
					}
				})
			};
		});
	}, [
		formTypes,
		seachText,
		relationResource
	]);
	const handleClose = () => {
		dispatch({
			type: "setState",
			payload: { currentSelectDayDetail: null }
		});
	};
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__schedules-component-resources" }, /* @__PURE__ */ react.default.createElement("div", { style: { padding: "0 32px" } }, /* @__PURE__ */ react.default.createElement(require_index$1.default, {
		onClose: handleClose,
		currentDay: currentSelectDayDetail === null || currentSelectDayDetail === void 0 ? void 0 : currentSelectDayDetail.date
	})), /* @__PURE__ */ react.default.createElement("div", { className: "resources-header" }, /* @__PURE__ */ react.default.createElement(require_index.default, {
		value: seachText,
		onChange: searchChange
	})), /* @__PURE__ */ react.default.createElement("div", { className: "resources-list" }, /* @__PURE__ */ react.default.createElement(antd.Skeleton, {
		loading: _getRelationResource.loading,
		style: { marginTop: 12 }
	}, items.length ? /* @__PURE__ */ react.default.createElement(antd.Collapse, {
		activeKey,
		ghost: true,
		items,
		expandIconPosition: "end",
		onChange: (key) => {
			setActiveKey(key);
		}
	}) : /* @__PURE__ */ react.default.createElement(antd.Empty, {
		image: antd.Empty.PRESENTED_IMAGE_SIMPLE,
		description: _pisell_utils.locales.getText("pisell2.text.products.no-resources")
	})))), /* @__PURE__ */ react.default.createElement(require_index$2.default, {
		ref: resourcesAddRef,
		onSuccess: init
	}), /* @__PURE__ */ react.default.createElement(require_index$3.default, {
		ref: resourcesEditRef,
		onSuccess: init
	}), /* @__PURE__ */ react.default.createElement(require_index$4.default, {
		tabKey,
		ref: resourcesDetailRef,
		onSuccess: (e) => {
			if (e) init();
		}
	}));
};
//#endregion
exports.default = Resources;
