const require_runtime = require("../../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_useEngineContext = require("../../../../../hooks/useEngineContext.js");
const require_constants = require("../../AuthMethods/constants.js");
const require_index = require("../../AuthMethods/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);
let _ant_design_icons = require("@ant-design/icons");
require("./AuthenticationMethods.less");
//#region src/components/authentication/admin/ChannelAuthSetting/AuthenticationMethods/index.tsx
const { Title, Paragraph, Text } = antd.Typography;
const convertApiDataToComponent = (apiData) => {
	const { method } = apiData;
	const processedKeys = /* @__PURE__ */ new Set();
	const authMethods = Object.keys(method).map((key) => {
		processedKeys.add(key);
		const methodData = method[key];
		const methodId = key === "phone" ? "phone" : key;
		const baseMethod = require_constants.getDefaultAuthMethods().find((m) => m.id === methodId);
		if (!baseMethod) return null;
		return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, baseMethod), {}, {
			enabled: methodData.enabled,
			sort: methodData.sort
		});
	}).filter(Boolean);
	require_constants.getDefaultAuthMethods().forEach((defaultMethod) => {
		if (!processedKeys.has(defaultMethod.id)) authMethods.push(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, defaultMethod), {}, { enabled: false }));
	});
	authMethods.sort((a, b) => {
		var _a$sort, _b$sort;
		return ((_a$sort = a.sort) !== null && _a$sort !== void 0 ? _a$sort : 0) - ((_b$sort = b.sort) !== null && _b$sort !== void 0 ? _b$sort : 0);
	});
	return {
		authMethods,
		globalMethods: authMethods.map((method) => {
			return {
				method: method.id === "phone" ? "phoneNumber" : method.id,
				enabled: method.enabled,
				label: {
					email: {
						en: "Email",
						"zh-CN": "邮箱",
						"zh-HK": "郵箱",
						ja: "メール",
						pt: "E-mail"
					},
					phone: {
						en: "Phone Number",
						"zh-CN": "手机号",
						"zh-HK": "手機號",
						ja: "電話番号",
						pt: "Número de Telefone"
					},
					facebook: {
						en: "Facebook",
						"zh-CN": "Facebook",
						"zh-HK": "Facebook",
						ja: "Facebook",
						pt: "Facebook"
					},
					apple: {
						en: "Apple",
						"zh-CN": "Apple",
						"zh-HK": "Apple",
						ja: "Apple",
						pt: "Apple"
					},
					guest: {
						en: "Guest",
						"zh-CN": "访客",
						"zh-HK": "訪客",
						ja: "ゲスト",
						pt: "Convidado"
					}
				}[method.id] || {
					en: method.id,
					"zh-CN": method.id,
					"zh-HK": method.id
				}
			};
		})
	};
};
const mockGlobalMethods = [
	{
		method: "email",
		enabled: true,
		label: {
			en: "Email",
			"zh-CN": "邮箱",
			"zh-HK": "郵箱",
			ja: "メール",
			pt: "E-mail"
		}
	},
	{
		method: "phoneNumber",
		enabled: true,
		label: {
			en: "Phone Number",
			"zh-CN": "手机号",
			"zh-HK": "手機號",
			ja: "電話番号",
			pt: "Número de Telefone"
		}
	},
	{
		method: "facebook",
		enabled: true,
		label: {
			en: "Facebook",
			"zh-CN": "Facebook",
			"zh-HK": "Facebook",
			ja: "Facebook",
			pt: "Facebook"
		}
	},
	{
		method: "apple",
		enabled: true,
		label: {
			en: "Apple",
			"zh-CN": "Apple",
			"zh-HK": "Apple",
			ja: "Apple",
			pt: "Apple"
		}
	}
];
const AuthenticationMethods = ({ className, style, value, onChange, globalMethods = mockGlobalMethods, onConfigureGlobal }) => {
	var _context$engine;
	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");
	const { request, data, history } = context.appHelper.utils.getApp();
	const dataKey = "auth.customer";
	const [tenantId, setTenantId] = (0, react.useState)(null);
	const [loading, setLoading] = (0, react.useState)(true);
	const [mode, setMode] = (0, react.useState)("default");
	const [authMethods, setAuthMethods] = (0, react.useState)(require_constants.getDefaultAuthMethods());
	const [fetchedGlobalMethods, setFetchedGlobalMethods] = (0, react.useState)(mockGlobalMethods);
	(0, react.useEffect)(() => {
		const coreData = data.storage.getStorage("core");
		if (coreData) try {
			var _coreDataJson$tenant;
			const coreDataJson = JSON.parse(coreData);
			if (coreDataJson === null || coreDataJson === void 0 || (_coreDataJson$tenant = coreDataJson.tenant) === null || _coreDataJson$tenant === void 0 ? void 0 : _coreDataJson$tenant.id) setTenantId(coreDataJson.tenant.id);
		} catch (error) {
			console.error("Error parsing core data:", error);
		}
	}, [data.storage]);
	(0, react.useEffect)(() => {
		if (tenantId) {
			setLoading(true);
			request.get(`/tenant/core/meta?key=${dataKey}&metable_type=tenant&metable_id=${tenantId}`).then((res) => {
				var _res$data;
				console.log("res_customer_auth_methods", res);
				if (res === null || res === void 0 || (_res$data = res.data) === null || _res$data === void 0 ? void 0 : _res$data.length) {
					var _res$data$;
					const { authMethods: convertedAuthMethods, globalMethods: convertedGlobalMethods } = convertApiDataToComponent((_res$data$ = res.data[0]) === null || _res$data$ === void 0 ? void 0 : _res$data$.value);
					setFetchedGlobalMethods(convertedGlobalMethods);
					console.log("Converted data:", {
						authMethods: convertedAuthMethods,
						globalMethods: convertedGlobalMethods
					});
				}
				setLoading(false);
			}).catch((error) => {
				console.error("Error fetching tenant data:", error);
				setLoading(false);
			});
		}
	}, [tenantId, request]);
	(0, react.useEffect)(() => {
		if (value) {
			setMode(value.mode || "default");
			console.log("同步外部value - 输入的 value.methods:", value.methods);
			const updatedMethods = require_constants.getDefaultAuthMethods().map((method, index) => {
				var _value$methods, _method$sort;
				const methodKey = method.id === "phone" ? "phoneNumber" : method.id;
				const config = (_value$methods = value.methods) === null || _value$methods === void 0 ? void 0 : _value$methods[methodKey];
				if (config && typeof config === "object" && "enabled" in config) {
					var _config$enabled, _config$sort;
					return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, method), {}, {
						enabled: (_config$enabled = config.enabled) !== null && _config$enabled !== void 0 ? _config$enabled : method.enabled,
						sort: (_config$sort = config.sort) !== null && _config$sort !== void 0 ? _config$sort : index
					});
				}
				return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, method), {}, { sort: (_method$sort = method.sort) !== null && _method$sort !== void 0 ? _method$sort : index });
			}).sort((a, b) => {
				var _a$sort2, _b$sort2;
				return ((_a$sort2 = a.sort) !== null && _a$sort2 !== void 0 ? _a$sort2 : 0) - ((_b$sort2 = b.sort) !== null && _b$sort2 !== void 0 ? _b$sort2 : 0);
			});
			console.log("同步外部value - 生成的 updatedMethods:", updatedMethods);
			setAuthMethods(updatedMethods);
		}
	}, [value]);
	const convertAuthMethodsToConfig = (methods) => {
		console.log("convertAuthMethodsToConfig - 输入的 methods:", methods);
		const config = {};
		methods.forEach((method, index) => {
			var _method$sort2;
			const key = method.id === "phone" ? "phoneNumber" : method.id;
			config[key] = {
				enabled: method.enabled,
				sort: (_method$sort2 = method.sort) !== null && _method$sort2 !== void 0 ? _method$sort2 : index
			};
		});
		console.log("convertAuthMethodsToConfig - 输出的 config:", config);
		return config;
	};
	const handleModeChange = (e) => {
		setMode(e.target.value);
		onChange === null || onChange === void 0 || onChange({
			mode: e.target.value,
			methods: convertAuthMethodsToConfig(authMethods)
		});
	};
	const handleConfigureGlobal = () => {
		history.push("/system-settings/auth/customer");
	};
	const handleOAuthConfig = () => {
		history.push("/system-settings/auth/oauth-config");
	};
	const handleAuthMethodsOrderChange = (methods) => {
		console.log("handleAuthMethodsOrderChange", methods);
		setAuthMethods(methods);
		onChange === null || onChange === void 0 || onChange({
			mode,
			methods: convertAuthMethodsToConfig(methods)
		});
	};
	const handleAuthMethodsToggle = (method, enabled) => {
		const updatedMethods = authMethods.map((m) => m.id === method.id ? require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, m), {}, { enabled }) : m);
		setAuthMethods(updatedMethods);
		setTimeout(() => {
			onChange === null || onChange === void 0 || onChange({
				mode,
				methods: convertAuthMethodsToConfig(updatedMethods)
			});
		}, 0);
	};
	const renderMethodTag = (method) => {
		var _context$engine2;
		const statusText = method.enabled ? _pisell_utils.locales.getText("auth.methods.status.enabled") : _pisell_utils.locales.getText("auth.methods.status.disabled");
		const locale = (context === null || context === void 0 || (_context$engine2 = context.engine) === null || _context$engine2 === void 0 || (_context$engine2 = _context$engine2.props) === null || _context$engine2 === void 0 ? void 0 : _context$engine2.locale) || "en";
		const label = method.label[locale] || method.label.en;
		return /* @__PURE__ */ react.default.createElement("div", {
			key: method.method,
			className: "auth-method-item"
		}, /* @__PURE__ */ react.default.createElement("span", { className: `auth-method-status ${method.enabled ? "enabled" : "disabled"}` }, statusText), /* @__PURE__ */ react.default.createElement(Text, null, label));
	};
	if (loading) return /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)("authentication-methods", className),
		style: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, style), {}, {
			display: "flex",
			justifyContent: "center",
			alignItems: "center",
			minHeight: "400px"
		})
	}, /* @__PURE__ */ react.default.createElement(antd.Spin, { size: "large" }));
	return /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)("authentication-methods", className),
		style
	}, /* @__PURE__ */ react.default.createElement("div", { className: "authentication-methods-upper" }, /* @__PURE__ */ react.default.createElement("div", { className: "authentication-methods-header" }, /* @__PURE__ */ react.default.createElement(Title, {
		level: 2,
		className: "authentication-methods-title"
	}, _pisell_utils.locales.getText("auth.methods.title"))), /* @__PURE__ */ react.default.createElement("div", { className: "authentication-methods-mode" }, /* @__PURE__ */ react.default.createElement(antd.Radio.Group, {
		value: mode,
		onChange: handleModeChange
	}, /* @__PURE__ */ react.default.createElement("div", { className: "authentication-methods-mode-wrapper" }, /* @__PURE__ */ react.default.createElement("div", { className: "authentication-methods-mode-option" }, /* @__PURE__ */ react.default.createElement(antd.Radio, { value: "default" }, /* @__PURE__ */ react.default.createElement("div", { className: "authentication-methods-mode-content" }, /* @__PURE__ */ react.default.createElement(Text, {
		strong: true,
		className: "authentication-methods-mode-title"
	}, _pisell_utils.locales.getText("auth.methods.mode.default.title")), /* @__PURE__ */ react.default.createElement(Paragraph, { className: "authentication-methods-mode-description" }, _pisell_utils.locales.getText("auth.methods.mode.default.description")), /* @__PURE__ */ react.default.createElement(antd.Button, {
		type: "link",
		className: "authentication-methods-configure-link",
		onClick: handleConfigureGlobal,
		style: {
			padding: 0,
			height: "auto"
		}
	}, _pisell_utils.locales.getText("auth.methods.mode.default.configure"))))), /* @__PURE__ */ react.default.createElement("div", { className: "authentication-methods-mode-option" }, /* @__PURE__ */ react.default.createElement(antd.Radio, { value: "custom" }, /* @__PURE__ */ react.default.createElement("div", { className: "authentication-methods-mode-content" }, /* @__PURE__ */ react.default.createElement(Text, {
		strong: true,
		className: "authentication-methods-mode-title"
	}, _pisell_utils.locales.getText("auth.methods.mode.custom.title")), /* @__PURE__ */ react.default.createElement(Paragraph, { className: "authentication-methods-mode-description" }, _pisell_utils.locales.getText("auth.methods.mode.custom.description"))))))))), /* @__PURE__ */ react.default.createElement("div", { className: "authentication-methods-middle" }, mode === "default" && /* @__PURE__ */ react.default.createElement(antd.Alert, {
		message: _pisell_utils.locales.getText("auth.methods.preview.title"),
		description: _pisell_utils.locales.getText("auth.methods.preview.description"),
		type: "info",
		showIcon: true,
		icon: /* @__PURE__ */ react.default.createElement(_ant_design_icons.InfoCircleOutlined, null)
	}), mode === "custom" && /* @__PURE__ */ react.default.createElement(antd.Alert, {
		message: _pisell_utils.locales.getText("auth.methods.custom.warning.title"),
		description: _pisell_utils.locales.getText("auth.methods.custom.warning.description"),
		type: "warning",
		showIcon: true
	})), /* @__PURE__ */ react.default.createElement("div", { className: "authentication-methods-lower" }, /* @__PURE__ */ react.default.createElement("div", { className: "authentication-methods-customer" }, /* @__PURE__ */ react.default.createElement("div", { className: "authentication-methods-customer-left" }, /* @__PURE__ */ react.default.createElement("div", { className: "authentication-methods-customer-header" }, /* @__PURE__ */ react.default.createElement(_ant_design_icons.UserOutlined, { className: "authentication-methods-customer-icon" }), /* @__PURE__ */ react.default.createElement(Title, {
		level: 4,
		className: "authentication-methods-customer-title"
	}, _pisell_utils.locales.getText("auth.methods.customer.title"))), /* @__PURE__ */ react.default.createElement(Paragraph, { className: "authentication-methods-customer-description" }, _pisell_utils.locales.getText("auth.methods.customer.description"))), /* @__PURE__ */ react.default.createElement("div", { className: "authentication-methods-customer-right" }, mode === "default" ? /* @__PURE__ */ react.default.createElement("div", { className: "authentication-methods-inherited" }, /* @__PURE__ */ react.default.createElement(Paragraph, { className: "authentication-methods-inherited-title" }, _pisell_utils.locales.getText("auth.methods.preview.inherited")), /* @__PURE__ */ react.default.createElement("div", { className: "authentication-methods-list" }, fetchedGlobalMethods.map(renderMethodTag))) : /* @__PURE__ */ react.default.createElement(require_index.default, {
		methods: authMethods,
		onOrderChange: handleAuthMethodsOrderChange,
		onToggle: handleAuthMethodsToggle,
		onConfigClick: handleOAuthConfig,
		enableSetting: false
	})))));
};
//#endregion
exports.default = AuthenticationMethods;
