const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_useEngineContext = require("../../../../hooks/useEngineContext.js");
const require_index = require("./PageSetting/index.js");
const require_index$1 = require("./AuthenticationMethods/index.js");
const require_index$2 = require("./CollectInfo/index.js");
const require_locales = require("./locales.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("./ChannelAuthSetting.less");
//#region src/components/authentication/admin/ChannelAuthSetting/index.tsx
const { Title, Paragraph } = antd.Typography;
const ChannelAuthSetting = ({ className, style, value, defaultActiveTab, visibleTabs, showRegisterPage = true, onSave }) => {
	var _context$engine;
	const context = require_useEngineContext.default();
	const { useParams } = context.appHelper.utils.getApp().history;
	const params = useParams();
	_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");
	console.log("ChannelAuthSetting", params);
	const allTabs = [
		"pageSetting",
		"authenticationMethods",
		"collectInfo"
	];
	const displayTabs = (0, react.useMemo)(() => {
		if (visibleTabs && visibleTabs.length > 0) return allTabs.filter((tab) => visibleTabs.includes(tab));
		return allTabs;
	}, [visibleTabs]);
	const [activeTab, setActiveTab] = (0, react.useState)((0, react.useMemo)(() => {
		if (defaultActiveTab && displayTabs.includes(defaultActiveTab)) return defaultActiveTab;
		return displayTabs[0] || "pageSetting";
	}, [defaultActiveTab, displayTabs]));
	const [internalValue, setInternalValue] = (0, react.useState)(value || {});
	const pageSettingRef = (0, react.useRef)(null);
	(0, react.useEffect)(() => {
		if (value) setInternalValue(value);
	}, [value]);
	const handlePageSettingChange = (pageSettingValue) => {
		setInternalValue((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { pageSetting: pageSettingValue }));
	};
	const handleAuthenticationMethodsChange = (authMethodsValue) => {
		setInternalValue((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { authenticationMethods: authMethodsValue }));
	};
	const handleCollectInfoChange = (collectInfoValue) => {
		setInternalValue((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { collectInfo: collectInfoValue }));
	};
	const handleSave = function() {
		var _ref = require_asyncToGenerator._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) {
				antd.message.error(_pisell_utils.locales.getText("channel.auth.setting.validation.error") || "请检查表单填写是否正确");
				console.error("表单校验失败:", error);
			}
		});
		return function handleSave() {
			return _ref.apply(this, arguments);
		};
	}();
	const allTabItems = (0, react.useMemo)(() => ({
		pageSetting: {
			key: "pageSetting",
			label: _pisell_utils.locales.getText("channel.auth.setting.tab.pageSetting"),
			children: /* @__PURE__ */ react.default.createElement(require_index.default, {
				ref: pageSettingRef,
				value: internalValue.pageSetting,
				onChange: handlePageSettingChange,
				showRegisterPage
			})
		},
		authenticationMethods: {
			key: "authenticationMethods",
			label: _pisell_utils.locales.getText("channel.auth.setting.tab.authenticationMethods"),
			children: /* @__PURE__ */ react.default.createElement(require_index$1.default, {
				value: internalValue.authenticationMethods,
				onChange: handleAuthenticationMethodsChange
			})
		},
		collectInfo: {
			key: "collectInfo",
			label: _pisell_utils.locales.getText("channel.auth.setting.tab.collectInfo"),
			children: /* @__PURE__ */ react.default.createElement(require_index$2.default, {
				value: internalValue.collectInfo,
				onChange: handleCollectInfoChange
			})
		}
	}), [internalValue, showRegisterPage]);
	const tabItems = (0, react.useMemo)(() => {
		return displayTabs.map((tabKey) => allTabItems[tabKey]);
	}, [displayTabs, allTabItems]);
	return /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)("channel-auth-setting", className),
		style
	}, /* @__PURE__ */ react.default.createElement("div", { className: "channel-auth-setting-header" }, /* @__PURE__ */ react.default.createElement(Title, {
		level: 1,
		className: "channel-auth-setting-title"
	}, _pisell_utils.locales.getText("channel.auth.setting.title")), /* @__PURE__ */ react.default.createElement(Paragraph, { className: "channel-auth-setting-description" }, _pisell_utils.locales.getText("channel.auth.setting.description"))), /* @__PURE__ */ react.default.createElement("div", { className: "channel-auth-setting-tabs" }, /* @__PURE__ */ react.default.createElement(antd.Tabs, {
		activeKey: activeTab,
		onChange: setActiveTab,
		items: tabItems
	})), /* @__PURE__ */ react.default.createElement("div", { className: "channel-auth-setting-footer" }, /* @__PURE__ */ react.default.createElement(antd.Button, {
		type: "primary",
		onClick: handleSave
	}, _pisell_utils.locales.getText("channel.auth.setting.button.save"))));
};
//#endregion
exports.default = ChannelAuthSetting;
