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 PageSetting from "./PageSetting/index.js";
import AuthenticationMethods from "./AuthenticationMethods/index.js";
import CollectInfo from "./CollectInfo/index.js";
import locales_default from "./locales.js";
import React, { useEffect, useMemo, useRef, useState } from "react";
import { Button, Tabs, Typography, message } from "antd";
import { locales } from "@pisell/utils";
import classNames from "classnames";
import "./ChannelAuthSetting.less";
//#region src/components/authentication/admin/ChannelAuthSetting/index.tsx
const { Title, Paragraph } = Typography;
const ChannelAuthSetting = ({ className, style, value, defaultActiveTab, visibleTabs, showRegisterPage = true, onSave }) => {
	var _context$engine;
	const context = useEngineContext();
	const { useParams } = context.appHelper.utils.getApp().history;
	const params = useParams();
	locales.init(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");
	console.log("ChannelAuthSetting", params);
	const allTabs = [
		"pageSetting",
		"authenticationMethods",
		"collectInfo"
	];
	const displayTabs = useMemo(() => {
		if (visibleTabs && visibleTabs.length > 0) return allTabs.filter((tab) => visibleTabs.includes(tab));
		return allTabs;
	}, [visibleTabs]);
	const [activeTab, setActiveTab] = useState(useMemo(() => {
		if (defaultActiveTab && displayTabs.includes(defaultActiveTab)) return defaultActiveTab;
		return displayTabs[0] || "pageSetting";
	}, [defaultActiveTab, displayTabs]));
	const [internalValue, setInternalValue] = useState(value || {});
	const pageSettingRef = useRef(null);
	useEffect(() => {
		if (value) setInternalValue(value);
	}, [value]);
	const handlePageSettingChange = (pageSettingValue) => {
		setInternalValue((prev) => _objectSpread2(_objectSpread2({}, prev), {}, { pageSetting: pageSettingValue }));
	};
	const handleAuthenticationMethodsChange = (authMethodsValue) => {
		setInternalValue((prev) => _objectSpread2(_objectSpread2({}, prev), {}, { authenticationMethods: authMethodsValue }));
	};
	const handleCollectInfoChange = (collectInfoValue) => {
		setInternalValue((prev) => _objectSpread2(_objectSpread2({}, prev), {}, { collectInfo: collectInfoValue }));
	};
	const handleSave = function() {
		var _ref = _asyncToGenerator(function* () {
			try {
				const validationPromises = [];
				if (activeTab === "pageSetting" && pageSettingRef.current) validationPromises.push(pageSettingRef.current.validateFields());
				if (validationPromises.length > 0) yield Promise.all(validationPromises);
				const currentTabData = {};
				if (activeTab === "pageSetting") currentTabData.pageSetting = internalValue.pageSetting;
				else if (activeTab === "authenticationMethods") currentTabData.authenticationMethods = internalValue.authenticationMethods;
				else if (activeTab === "collectInfo") currentTabData.collectInfo = internalValue.collectInfo;
				onSave === null || onSave === void 0 || onSave({
					value: currentTabData,
					currentTab: activeTab
				});
			} catch (error) {
				message.error(locales.getText("channel.auth.setting.validation.error") || "请检查表单填写是否正确");
				console.error("表单校验失败:", error);
			}
		});
		return function handleSave() {
			return _ref.apply(this, arguments);
		};
	}();
	const allTabItems = useMemo(() => ({
		pageSetting: {
			key: "pageSetting",
			label: locales.getText("channel.auth.setting.tab.pageSetting"),
			children: /* @__PURE__ */ React.createElement(PageSetting, {
				ref: pageSettingRef,
				value: internalValue.pageSetting,
				onChange: handlePageSettingChange,
				showRegisterPage
			})
		},
		authenticationMethods: {
			key: "authenticationMethods",
			label: locales.getText("channel.auth.setting.tab.authenticationMethods"),
			children: /* @__PURE__ */ React.createElement(AuthenticationMethods, {
				value: internalValue.authenticationMethods,
				onChange: handleAuthenticationMethodsChange
			})
		},
		collectInfo: {
			key: "collectInfo",
			label: locales.getText("channel.auth.setting.tab.collectInfo"),
			children: /* @__PURE__ */ React.createElement(CollectInfo, {
				value: internalValue.collectInfo,
				onChange: handleCollectInfoChange
			})
		}
	}), [internalValue, showRegisterPage]);
	const tabItems = useMemo(() => {
		return displayTabs.map((tabKey) => allTabItems[tabKey]);
	}, [displayTabs, allTabItems]);
	return /* @__PURE__ */ React.createElement("div", {
		className: classNames("channel-auth-setting", className),
		style
	}, /* @__PURE__ */ React.createElement("div", { className: "channel-auth-setting-header" }, /* @__PURE__ */ React.createElement(Title, {
		level: 1,
		className: "channel-auth-setting-title"
	}, locales.getText("channel.auth.setting.title")), /* @__PURE__ */ React.createElement(Paragraph, { className: "channel-auth-setting-description" }, locales.getText("channel.auth.setting.description"))), /* @__PURE__ */ React.createElement("div", { className: "channel-auth-setting-tabs" }, /* @__PURE__ */ React.createElement(Tabs, {
		activeKey: activeTab,
		onChange: setActiveTab,
		items: tabItems
	})), /* @__PURE__ */ React.createElement("div", { className: "channel-auth-setting-footer" }, /* @__PURE__ */ React.createElement(Button, {
		type: "primary",
		onClick: handleSave
	}, locales.getText("channel.auth.setting.button.save"))));
};
//#endregion
export { ChannelAuthSetting as default };
