import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { Context } from "../model.js";
import Search from "../components/Search/index.js";
import CurrentDay from "../components/CurrentDay/index.js";
import add_default from "./add/index.js";
import edit_default from "./edit/index.js";
import { getFormTypes, getRelationResource } from "./serve.js";
import detail_default from "./detail/index.js";
import ResourcesList from "./list/index.js";
import React, { useContext, useEffect, useMemo, useRef, useState } from "react";
import { Collapse, Empty, Skeleton } from "antd";
import { locales } from "@pisell/utils";
import { useRequest } from "ahooks";
import "./index.less";
//#region src/components/schedules/resources/index.tsx
const Resources = () => {
	const { state, dispatch } = useContext(Context);
	const { currentSelectDayDetail, resources, tabKey } = state;
	const cacheResource = resources === null || resources === void 0 ? void 0 : resources.cacheResource;
	const resourcesAddRef = useRef();
	const resourcesEditRef = useRef();
	const resourcesDetailRef = useRef();
	const [seachText, setSearchText] = useState("");
	const [activeKey, setActiveKey] = useState([]);
	const [formTypes, setFormTypes] = useState([]);
	const [relationResource, setRelationResource] = useState([]);
	const searchChange = (e) => {
		setSearchText(e);
	};
	const _getFormTypes = useRequest(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 = useRequest(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);
	};
	useEffect(() => {
		_getFormTypes.run({
			type: "manual",
			num: 500,
			skip: 1
		});
	}, []);
	useEffect(() => {
		init();
	}, [currentSelectDayDetail === null || currentSelectDayDetail === void 0 ? void 0 : currentSelectDayDetail.scheduleIds]);
	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 = 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(_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.createElement(ResourcesList, {
					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.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__schedules-component-resources" }, /* @__PURE__ */ React.createElement("div", { style: { padding: "0 32px" } }, /* @__PURE__ */ React.createElement(CurrentDay, {
		onClose: handleClose,
		currentDay: currentSelectDayDetail === null || currentSelectDayDetail === void 0 ? void 0 : currentSelectDayDetail.date
	})), /* @__PURE__ */ React.createElement("div", { className: "resources-header" }, /* @__PURE__ */ React.createElement(Search, {
		value: seachText,
		onChange: searchChange
	})), /* @__PURE__ */ React.createElement("div", { className: "resources-list" }, /* @__PURE__ */ React.createElement(Skeleton, {
		loading: _getRelationResource.loading,
		style: { marginTop: 12 }
	}, items.length ? /* @__PURE__ */ React.createElement(Collapse, {
		activeKey,
		ghost: true,
		items,
		expandIconPosition: "end",
		onChange: (key) => {
			setActiveKey(key);
		}
	}) : /* @__PURE__ */ React.createElement(Empty, {
		image: Empty.PRESENTED_IMAGE_SIMPLE,
		description: locales.getText("pisell2.text.products.no-resources")
	})))), /* @__PURE__ */ React.createElement(add_default, {
		ref: resourcesAddRef,
		onSuccess: init
	}), /* @__PURE__ */ React.createElement(edit_default, {
		ref: resourcesEditRef,
		onSuccess: init
	}), /* @__PURE__ */ React.createElement(detail_default, {
		tabKey,
		ref: resourcesDetailRef,
		onSuccess: (e) => {
			if (e) init();
		}
	}));
};
//#endregion
export { Resources as default };
