import { getLocale } from "../../utils/locales.js";
import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import useEngineContext from "../../hooks/useEngineContext.js";
import { Context, Provider } from "./model.js";
import { request, saveConfirm, setModal } from "./utils.js";
import Calendar$1 from "./calendar/index.js";
import SchedulesConnect from "./schedules/index.js";
import Products from "./products/index.js";
import Resources from "./resources/index.js";
import OpenHours from "./openHours/index.js";
import locales_default from "./locales.js";
import { scheduleApps } from "./constants.js";
import React, { useContext, useEffect, useMemo, useRef, useState } from "react";
import { Modal, Tabs } from "antd";
import { locales } from "@pisell/utils";
import "./index.less";
//#region src/components/schedules/index.tsx
locales.init(locales_default, getLocale());
const Schedules = Provider((props) => {
	const { globalState } = props;
	const { state, dispatch } = useContext(Context);
	const { tabKey } = state;
	const [forceUpdateOpenHours, setForceUpdateOpenHours] = useState(0);
	const openHoursRef = useRef();
	const context = useEngineContext();
	const [modal, contextHolder] = Modal.useModal();
	request.setRequest(context.appHelper.utils.request);
	setModal(modal);
	const setState = (payload) => {
		dispatch({
			type: "setState",
			payload: _objectSpread2({}, payload)
		});
	};
	useEffect(() => {
		dispatch({
			type: "setGlobalState",
			payload: _objectSpread2({ setGlobalState: props.setGlobalState }, globalState)
		});
	}, [globalState]);
	const onChange = function() {
		var _ref = _asyncToGenerator(function* (key) {
			var _openHoursRef$current;
			if (tabKey === "openHours" && ((_openHoursRef$current = openHoursRef.current) === null || _openHoursRef$current === void 0 ? void 0 : _openHoursRef$current.getIsEdit())) {
				if (!(yield saveConfirm())) return;
				setForceUpdateOpenHours((p) => p + 1);
			}
			setState({ tabKey: key });
		});
		return function onChange(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	useEffect(() => {
		document.body.id = "body";
	}, []);
	const isProduct = useMemo(() => {
		var _context$appHelper$ut, _context$appHelper$ut2;
		return (((_context$appHelper$ut = context.appHelper.utils.store) === null || _context$appHelper$ut === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut.getState) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut2.call(_context$appHelper$ut)) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut2.global) === null || _context$appHelper$ut2 === void 0 ? void 0 : _context$appHelper$ut2.globalConfig.apps) || []).some((d) => scheduleApps.includes(d.module));
	}, []);
	useEffect(() => {
		dispatch({
			type: "setState",
			payload: { tabKey: isProduct ? "products" : "schedules" }
		});
	}, [isProduct]);
	const items = useMemo(() => {
		let list = [
			{
				key: "products",
				label: locales.getText("pisell2.text.schedules.products-title"),
				children: /* @__PURE__ */ React.createElement(Products, null),
				forceRender: true
			},
			{
				key: "schedules",
				label: locales.getText("pisell2.text.schedules.schedules-title"),
				children: /* @__PURE__ */ React.createElement(SchedulesConnect, null),
				forceRender: true
			},
			{
				key: "resources",
				label: locales.getText("pisell2.text.schedules.resources-title"),
				children: /* @__PURE__ */ React.createElement(Resources, null),
				forceRender: true
			},
			{
				key: "openHours",
				label: locales.getText("pisell2.text.schedules.open-hours-title"),
				children: /* @__PURE__ */ React.createElement(OpenHours, {
					ref: openHoursRef,
					key: forceUpdateOpenHours
				}),
				forceRender: true
			}
		];
		if (!isProduct) list = list.filter((item) => item.key !== "products");
		return list;
	}, [
		state,
		openHoursRef,
		forceUpdateOpenHours,
		modal,
		isProduct
	]);
	return /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__schedules-component" }, /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__schedules-component-calendar" }, /* @__PURE__ */ React.createElement(Calendar$1, null)), /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__schedules-component-panels" }, /* @__PURE__ */ React.createElement(Tabs, {
		className: "pisell-lowcode__schedules-tabs-wrap",
		activeKey: tabKey,
		tabBarStyle: { margin: "0 32px" },
		items,
		onChange
	})), contextHolder);
});
//#endregion
export { Schedules as default };
