import { _objectSpread2 } from "../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import useEngineContext from "../../../../../hooks/useEngineContext.js";
import PageSetting from "../PageSetting/index.js";
import locales_default from "../locales.js";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { Button, Spin, Tabs, Typography, message } from "antd";
import { locales } from "@pisell/utils";
import classNames from "classnames";
import "./index.less";
//#region src/components/authentication/admin/ChannelAuthSetting/SaaSManagermentAuth/index.tsx
const { Title, Paragraph } = Typography;
const SaaSManagermentAuth = ({ className, style, value, defaultActiveTab, visibleTabs, showRegisterPage = true, onSave }) => {
	var _context$engine;
	const context = useEngineContext();
	const { useParams } = context.appHelper.utils.getApp().history;
	const { channel } = useParams();
	const { request } = context.appHelper.utils.getApp();
	const [loading, setLoading] = useState(true);
	const [channelData, setChannelData] = useState(null);
	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");
	const allTabs = ["pageSetting"];
	const saveData = (data) => {
		request.post(`/api/tenant_channel:update?filter=%7B%22channel_code%22%3A%22${channel}%22%7D`, _objectSpread2(_objectSpread2({}, channelData), {}, { metadata: _objectSpread2(_objectSpread2({}, channelData === null || channelData === void 0 ? void 0 : channelData.metadata), {}, { register_login: data }) }), {
			isNocobase: true,
			headers: { "x-data-source": "pisell2" }
		}).then((res) => {
			message.success(locales.getText("channel.auth.setting.message.saveSuccess"));
		}).catch((error) => {
			console.error("Error saving channel data:", error);
			message.error(locales.getText("channel.auth.setting.message.saveError"));
		});
	};
	useEffect(() => {
		if (channel) {
			setLoading(true);
			request.get(`/api/tenant_channel:get?filter=%7B%22channel_code%22%3A%22${channel}%22%7D&appends=channel`, {}, {
				isNocobase: true,
				headers: { "x-data-source": "pisell2" }
			}).then((res) => {
				if (res === null || res === void 0 ? void 0 : res.data) setChannelData(res === null || res === void 0 ? void 0 : res.data);
				console.log("res_saas_management_auth", res);
				setLoading(false);
			}).catch((error) => {
				console.error("Error fetching channel data:", error);
				setLoading(false);
			});
		}
	}, [request, channel]);
	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 || {});
	useEffect(() => {
		if (value) setInternalValue(value);
	}, [value]);
	useEffect(() => {
		var _channelData$metadata;
		if (channelData === null || channelData === void 0 || (_channelData$metadata = channelData.metadata) === null || _channelData$metadata === void 0 ? void 0 : _channelData$metadata.register_login) {
			const { page } = channelData.metadata.register_login;
			const pageSettingValue = {};
			if (page) {
				var _page$register, _page$register2, _page$login$same_as_r, _page$login, _page$login2, _page$login3, _page$login4;
				pageSettingValue.registerPage = {
					backgroundImage: ((_page$register = page.register) === null || _page$register === void 0 ? void 0 : _page$register.background_image_url) || "",
					termsConditions: ((_page$register2 = page.register) === null || _page$register2 === void 0 || (_page$register2 = _page$register2.terms) === null || _page$register2 === void 0 ? void 0 : _page$register2.map((term) => {
						var _term$name, _term$name2, _term$name3, _term$name4, _term$name5, _term$name6;
						return {
							name: {
								original: ((_term$name = term.name) === null || _term$name === void 0 ? void 0 : _term$name.original) || "",
								en: ((_term$name2 = term.name) === null || _term$name2 === void 0 ? void 0 : _term$name2.en) || "",
								"zh-CN": ((_term$name3 = term.name) === null || _term$name3 === void 0 ? void 0 : _term$name3["zh-CN"]) || "",
								"zh-HK": ((_term$name4 = term.name) === null || _term$name4 === void 0 ? void 0 : _term$name4["zh-HK"]) || "",
								ja: ((_term$name5 = term.name) === null || _term$name5 === void 0 ? void 0 : _term$name5["ja"]) || "",
								pt: ((_term$name6 = term.name) === null || _term$name6 === void 0 ? void 0 : _term$name6["pt"]) || ""
							},
							url: term.url || ""
						};
					})) || []
				};
				pageSettingValue.loginPage = {
					sameAsRegister: (_page$login$same_as_r = (_page$login = page.login) === null || _page$login === void 0 ? void 0 : _page$login.same_as_register) !== null && _page$login$same_as_r !== void 0 ? _page$login$same_as_r : true,
					settings: ((_page$login2 = page.login) === null || _page$login2 === void 0 ? void 0 : _page$login2.same_as_register) ? void 0 : {
						backgroundImage: ((_page$login3 = page.login) === null || _page$login3 === void 0 ? void 0 : _page$login3.background_image_url) || "",
						termsConditions: ((_page$login4 = page.login) === null || _page$login4 === void 0 || (_page$login4 = _page$login4.terms) === null || _page$login4 === void 0 ? void 0 : _page$login4.map((term) => {
							var _term$name7, _term$name8, _term$name9, _term$name10, _term$name11, _term$name12;
							return {
								name: {
									original: ((_term$name7 = term.name) === null || _term$name7 === void 0 ? void 0 : _term$name7.original) || "",
									en: ((_term$name8 = term.name) === null || _term$name8 === void 0 ? void 0 : _term$name8.en) || "",
									"zh-CN": ((_term$name9 = term.name) === null || _term$name9 === void 0 ? void 0 : _term$name9["zh-CN"]) || "",
									"zh-HK": ((_term$name10 = term.name) === null || _term$name10 === void 0 ? void 0 : _term$name10["zh-HK"]) || "",
									ja: ((_term$name11 = term.name) === null || _term$name11 === void 0 ? void 0 : _term$name11["ja"]) || "",
									pt: ((_term$name12 = term.name) === null || _term$name12 === void 0 ? void 0 : _term$name12["pt"]) || ""
								},
								url: term.url || ""
							};
						})) || []
					}
				};
			}
			setInternalValue({ pageSetting: pageSettingValue });
		}
	}, [channelData]);
	const handlePageSettingChange = useCallback((pageSettingValue) => {
		setInternalValue((prev) => {
			if (JSON.stringify(prev.pageSetting) !== JSON.stringify(pageSettingValue)) return _objectSpread2(_objectSpread2({}, prev), {}, { pageSetting: pageSettingValue });
			return prev;
		});
	}, []);
	const transformToNewFormat = (value) => {
		var _pageSetting$register, _pageSetting$register2, _pageSetting$loginPag, _pageSetting$loginPag2, _pageSetting$loginPag3, _pageSetting$loginPag4, _pageSetting$loginPag5, _pageSetting$loginPag6;
		const { pageSetting } = value;
		console.log("pageSetting data:", pageSetting);
		return { page: {
			register: {
				background_image_url: (pageSetting === null || pageSetting === void 0 || (_pageSetting$register = pageSetting.registerPage) === null || _pageSetting$register === void 0 ? void 0 : _pageSetting$register.backgroundImage) || "",
				terms: (pageSetting === null || pageSetting === void 0 || (_pageSetting$register2 = pageSetting.registerPage) === null || _pageSetting$register2 === void 0 || (_pageSetting$register2 = _pageSetting$register2.termsConditions) === null || _pageSetting$register2 === void 0 ? void 0 : _pageSetting$register2.map((term) => ({
					name: {
						original: term.name.original || "",
						en: term.name.en || "",
						"zh-CN": term.name["zh-CN"] || "",
						"zh-HK": term.name["zh-HK"] || "",
						ja: term.name["ja"] || "",
						pt: term.name["pt"] || ""
					},
					url: term.url || ""
				}))) || []
			},
			login: {
				same_as_register: (_pageSetting$loginPag = pageSetting === null || pageSetting === void 0 || (_pageSetting$loginPag2 = pageSetting.loginPage) === null || _pageSetting$loginPag2 === void 0 ? void 0 : _pageSetting$loginPag2.sameAsRegister) !== null && _pageSetting$loginPag !== void 0 ? _pageSetting$loginPag : true,
				background_image_url: (pageSetting === null || pageSetting === void 0 || (_pageSetting$loginPag3 = pageSetting.loginPage) === null || _pageSetting$loginPag3 === void 0 ? void 0 : _pageSetting$loginPag3.sameAsRegister) ? null : (pageSetting === null || pageSetting === void 0 || (_pageSetting$loginPag4 = pageSetting.loginPage) === null || _pageSetting$loginPag4 === void 0 || (_pageSetting$loginPag4 = _pageSetting$loginPag4.settings) === null || _pageSetting$loginPag4 === void 0 ? void 0 : _pageSetting$loginPag4.backgroundImage) || null,
				terms: (pageSetting === null || pageSetting === void 0 || (_pageSetting$loginPag5 = pageSetting.loginPage) === null || _pageSetting$loginPag5 === void 0 ? void 0 : _pageSetting$loginPag5.sameAsRegister) ? [] : (pageSetting === null || pageSetting === void 0 || (_pageSetting$loginPag6 = pageSetting.loginPage) === null || _pageSetting$loginPag6 === void 0 || (_pageSetting$loginPag6 = _pageSetting$loginPag6.settings) === null || _pageSetting$loginPag6 === void 0 || (_pageSetting$loginPag6 = _pageSetting$loginPag6.termsConditions) === null || _pageSetting$loginPag6 === void 0 ? void 0 : _pageSetting$loginPag6.map((term) => ({
					name: {
						original: term.name.original || "",
						en: term.name.en || "",
						"zh-CN": term.name["zh-CN"] || "",
						"zh-HK": term.name["zh-HK"] || "",
						ja: term.name["ja"] || "",
						pt: term.name["pt"] || ""
					},
					url: term.url || ""
				}))) || []
			}
		} };
	};
	const handleSave = () => {
		const transformedValue = transformToNewFormat(internalValue);
		console.log("transformedValue", transformedValue);
		saveData(transformedValue);
	};
	const tabItems = useMemo(() => [{
		key: "pageSetting",
		label: locales.getText("channel.auth.setting.tab.pageSetting"),
		children: /* @__PURE__ */ React.createElement(PageSetting, {
			value: internalValue.pageSetting,
			onChange: handlePageSettingChange,
			showRegisterPage
		})
	}], [internalValue, showRegisterPage]);
	if (loading) return /* @__PURE__ */ React.createElement("div", {
		className: classNames("channel-auth-setting", className),
		style: _objectSpread2(_objectSpread2({}, style), {}, {
			display: "flex",
			justifyContent: "center",
			alignItems: "center",
			minHeight: "400px"
		})
	}, /* @__PURE__ */ React.createElement(Spin, { size: "large" }));
	return /* @__PURE__ */ React.createElement("div", {
		className: classNames("saasmanagermentauth", className),
		style
	}, /* @__PURE__ */ React.createElement("div", { className: "saasmanagermentauth-header" }, /* @__PURE__ */ React.createElement(Title, {
		level: 1,
		className: "saasmanagermentauth-title"
	}, locales.getText("channel.auth.setting.title")), /* @__PURE__ */ React.createElement(Paragraph, { className: "saasmanagermentauth-description" }, locales.getText("channel.auth.setting.description"))), /* @__PURE__ */ React.createElement("div", { className: "saasmanagermentauth-tabs" }, /* @__PURE__ */ React.createElement(Tabs, {
		activeKey: activeTab,
		onChange: setActiveTab,
		items: tabItems
	})), /* @__PURE__ */ React.createElement("div", { className: "saasmanagermentauth-footer" }, /* @__PURE__ */ React.createElement(Button, {
		type: "primary",
		onClick: handleSave
	}, locales.getText("channel.auth.setting.button.save"))));
};
//#endregion
export { SaaSManagermentAuth as default };
