const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_objectWithoutProperties = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_useEngineContext = require("../../../hooks/useEngineContext.js");
const require_index = require("../../../utils/index.js");
const require_locales = require("../locales.js");
const require_serve = require("./serve.js");
const require_BasicInfoTab = require("./BasicInfoTab.js");
const require_PermissonTab = require("./PermissonTab.js");
const require_ShiftScheduleTab = require("./ShiftScheduleTab.js");
const require_ClockInMethodTab = require("./ClockInMethodTab.js");
const require_CorrectionPolicyTab = require("./CorrectionPolicyTab.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let ahooks = require("ahooks");
require("./index.less");
//#region src/plus/saasAttendance/settings/index.tsx
const _excluded = ["members"];
const TAB_CONFIGS = [
	{
		key: "basic",
		icon: "pisell2-settings-02",
		labelKey: "pisell2.attendance.group-settings.basic-info"
	},
	{
		key: "shift",
		icon: "pisell2-calendar",
		labelKey: "pisell2.attendance.group-settings.shift-schedule"
	},
	{
		key: "clock",
		icon: "pisell2-marker-pin-01",
		labelKey: "pisell2.attendance.group-settings.clock-in-method"
	},
	{
		key: "correction",
		icon: "pisell2-clock",
		labelKey: "pisell2.attendance.group-settings.correction-policy"
	},
	{
		key: "permission",
		icon: "pisell2-user-01",
		labelKey: "pisell2.attendance.group-settings.data-permission"
	}
];
const generateSimpleUUID = () => {
	return Math.random().toString(36).substring(2) + Date.now().toString(36);
};
const settingsContext = (0, react.createContext)({ members: [] });
const Settings = (0, react.forwardRef)((props, ref) => {
	var _context$engine, _context$appHelper;
	const { value, customScrollParent } = props;
	const context = require_useEngineContext.default();
	_pisell_utils.locales.init(require_locales.default, (context === null || context === void 0 || (_context$engine = context.engine) === null || _context$engine === void 0 || (_context$engine = _context$engine.props) === null || _context$engine === void 0 ? void 0 : _context$engine.locale) || "en");
	if (context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.request) require_index.request.setRequest(context.appHelper.utils.request);
	const [form] = antd.Form.useForm();
	const [activeKey, setActiveKey] = (0, react.useState)("basic");
	const _uniqueId = (0, react.useRef)(generateSimpleUUID().substring(0, 8)).current;
	const isManualChangeRef = (0, react.useRef)(false);
	(0, react.useEffect)(() => {
		document.body.id = "body";
	}, []);
	const { data: membersResp } = (0, ahooks.useRequest)(require_serve.getAllMembers, { defaultParams: [{
		page: 1,
		size: 499,
		with: ["account"]
	}] });
	(0, react.useEffect)(() => {
		if (!value) return;
		const valueAny = value;
		const rawMembers = valueAny === null || valueAny === void 0 ? void 0 : valueAny.members;
		const memberIdsFromMembers = Array.isArray(rawMembers) ? Array.from(new Set(rawMembers.map((item) => item === null || item === void 0 ? void 0 : item.tenant_account_id).filter((id) => typeof id === "number" && !Number.isNaN(id)))) : [];
		const existingMemberIds = Array.isArray(valueAny === null || valueAny === void 0 ? void 0 : valueAny.member_ids) ? valueAny.member_ids : [];
		const resolvedMemberIds = existingMemberIds.length > 0 ? existingMemberIds : memberIdsFromMembers;
		const { members } = valueAny, rest = require_objectWithoutProperties._objectWithoutProperties(valueAny, _excluded);
		form.setFieldsValue(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, rest), {}, { member_ids: resolvedMemberIds }));
	}, [value, form]);
	const debounceIndicator = (0, react.useCallback)((delay) => {
		isManualChangeRef.current = true;
		const timeoutId = setTimeout(() => {
			isManualChangeRef.current = false;
		}, delay);
		return () => clearTimeout(timeoutId);
	}, []);
	(0, react.useImperativeHandle)(ref, () => require_objectSpread2._objectSpread2({}, form));
	const switchTab = (0, react.useCallback)((key) => {
		setActiveKey(key);
		debounceIndicator(300);
		let scrollParent = null;
		const formContainer = document.getElementById(`saas-attendance-group-settings-${_uniqueId}`);
		if (typeof customScrollParent === "string") scrollParent = document.querySelector(customScrollParent);
		else scrollParent = customScrollParent || formContainer;
		if (!scrollParent || !formContainer) return;
		const target = document.getElementById(`saas-attendance-settings-section-${_uniqueId}-${key}`);
		if (!target) return;
		const scrollParentRect = scrollParent.getBoundingClientRect();
		if (TAB_CONFIGS.findIndex((item) => item.key === key) === 0) scrollParent.scrollTop = 0;
		else {
			const scrollOffset = target.getBoundingClientRect().top - scrollParentRect.top + scrollParent.scrollTop;
			scrollParent.scrollTop = scrollOffset - 16;
		}
	}, [
		customScrollParent,
		debounceIndicator,
		_uniqueId
	]);
	(0, react.useEffect)(() => {
		let scrollParent = null;
		const formContainer = document.getElementById(`saas-attendance-group-settings-${_uniqueId}`);
		if (typeof customScrollParent === "string") scrollParent = document.querySelector(customScrollParent);
		else scrollParent = customScrollParent || formContainer;
		if (!scrollParent || !formContainer) return;
		const handleScroll = () => {
			if (isManualChangeRef.current) return;
			const tabsWrapper = document.getElementById(`saas-attendance-settings-${_uniqueId}_tabs`);
			const tabsHeight = (tabsWrapper === null || tabsWrapper === void 0 ? void 0 : tabsWrapper.offsetHeight) || 0;
			const checkPoint = scrollParent.getBoundingClientRect().top + tabsHeight + 20;
			let currentKey = "basic";
			for (const config of TAB_CONFIGS) {
				const section = document.getElementById(`saas-attendance-settings-section-${_uniqueId}-${config.key}`);
				if (section) {
					if (section.getBoundingClientRect().top <= checkPoint) currentKey = config.key;
				}
			}
			if (currentKey !== activeKey) setActiveKey(currentKey);
		};
		scrollParent.addEventListener("scroll", handleScroll);
		return () => {
			scrollParent === null || scrollParent === void 0 || scrollParent.removeEventListener("scroll", handleScroll);
		};
	}, [
		customScrollParent,
		_uniqueId,
		activeKey
	]);
	const tabItems = (0, react.useMemo)(() => TAB_CONFIGS.map((config) => ({
		key: config.key,
		label: /* @__PURE__ */ react.default.createElement("span", { className: "saas-attendance-group-settings__tab-label" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Icon, {
			type: config.icon,
			size: 20
		}), _pisell_utils.locales.getText(config.labelKey)),
		children: null
	})), []);
	return /* @__PURE__ */ react.default.createElement(settingsContext.Provider, { value: { members: (membersResp === null || membersResp === void 0 ? void 0 : membersResp.list) || [] } }, /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)("saas-attendance-group-settings", props.className),
		style: props.style
	}, props.loading ? /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__loading" }, /* @__PURE__ */ react.default.createElement(antd.Spin, null)) : null, /* @__PURE__ */ react.default.createElement("div", {
		className: "saas-attendance-group-settings__tabs-wrapper",
		id: `saas-attendance-settings-${_uniqueId}_tabs`
	}, /* @__PURE__ */ react.default.createElement(antd.Tabs, {
		activeKey,
		items: tabItems,
		onChange: switchTab,
		renderTabBar: (tabBarProps, DefaultTabBar) => /* @__PURE__ */ react.default.createElement(DefaultTabBar, tabBarProps)
	})), /* @__PURE__ */ react.default.createElement(antd.Form, {
		id: `saas-attendance-group-settings-${_uniqueId}`,
		className: "saas-attendance-group-settings__form",
		form,
		layout: "vertical",
		size: "large",
		onValuesChange: props.onValuesChange,
		onFinish: props.onFinish,
		scrollToFirstError: true
	}, /* @__PURE__ */ react.default.createElement(require_BasicInfoTab.default, { id: `saas-attendance-settings-section-${_uniqueId}-basic` }), /* @__PURE__ */ react.default.createElement(require_ShiftScheduleTab.default, { id: `saas-attendance-settings-section-${_uniqueId}-shift` }), /* @__PURE__ */ react.default.createElement(require_ClockInMethodTab.default, {
		form,
		id: `saas-attendance-settings-section-${_uniqueId}-clock`
	}), /* @__PURE__ */ react.default.createElement(require_CorrectionPolicyTab.default, {
		form,
		id: `saas-attendance-settings-section-${_uniqueId}-correction`
	}), /* @__PURE__ */ react.default.createElement(require_PermissonTab.default, { id: `saas-attendance-settings-section-${_uniqueId}-permission` }))));
});
//#endregion
exports.default = Settings;
exports.settingsContext = settingsContext;
