import { _asyncToGenerator } from "../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import useEngineContext from "../../../../../hooks/useEngineContext.js";
import locales_default from "./locales.js";
import PageSettingItem from "../PageSettingItem/index.js";
import React, { forwardRef, useEffect, useImperativeHandle, useRef, useState } from "react";
import { Checkbox, Typography } from "antd";
import { locales } from "@pisell/utils";
import classNames from "classnames";
import "./PageSetting.less";
//#region src/components/authentication/admin/ChannelAuthSetting/PageSetting/index.tsx
const { Title, Paragraph } = Typography;
const PageSetting = forwardRef(({ className, style, value, onChange, showRegisterPage = true, showImageUpload = true }, ref) => {
	var _context$engine;
	const context = useEngineContext();
	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 registerPageRef = useRef(null);
	const loginPageRef = useRef(null);
	useImperativeHandle(ref, () => ({ validateFields: function() {
		var _ref = _asyncToGenerator(function* () {
			const promises = [];
			if (showRegisterPage && registerPageRef.current) promises.push(registerPageRef.current.validateFields());
			if ((!sameAsRegister || !showRegisterPage) && loginPageRef.current) promises.push(loginPageRef.current.validateFields());
			return Promise.all(promises);
		});
		return function validateFields() {
			return _ref.apply(this, arguments);
		};
	}() }));
	const [registerPageValue, setRegisterPageValue] = useState({
		backgroundImage: void 0,
		termsConditions: []
	});
	const [sameAsRegister, setSameAsRegister] = useState(true);
	const [loginPageValue, setLoginPageValue] = useState({
		backgroundImage: void 0,
		termsConditions: []
	});
	useEffect(() => {
		if (value) {
			var _value$loginPage$same, _value$loginPage;
			setRegisterPageValue(value.registerPage || {
				backgroundImage: void 0,
				termsConditions: []
			});
			setSameAsRegister((_value$loginPage$same = (_value$loginPage = value.loginPage) === null || _value$loginPage === void 0 ? void 0 : _value$loginPage.sameAsRegister) !== null && _value$loginPage$same !== void 0 ? _value$loginPage$same : true);
			setTimeout(() => {
				var _value$loginPage2;
				setLoginPageValue(((_value$loginPage2 = value.loginPage) === null || _value$loginPage2 === void 0 ? void 0 : _value$loginPage2.settings) || {
					backgroundImage: void 0,
					termsConditions: []
				});
			}, 0);
		}
	}, [value]);
	const handleRegisterPageChange = (val) => {
		onChange === null || onChange === void 0 || onChange({
			registerPage: val,
			loginPage: {
				sameAsRegister,
				settings: loginPageValue
			}
		});
	};
	const handleLoginPageChange = (val) => {
		onChange === null || onChange === void 0 || onChange({
			registerPage: registerPageValue,
			loginPage: {
				sameAsRegister,
				settings: val
			}
		});
	};
	const handleSameAsRegisterChange = (checked) => {
		onChange === null || onChange === void 0 || onChange({
			registerPage: registerPageValue,
			loginPage: {
				sameAsRegister: checked,
				settings: loginPageValue
			}
		});
	};
	return /* @__PURE__ */ React.createElement("div", {
		className: classNames("page-setting", className),
		style
	}, showRegisterPage && /* @__PURE__ */ React.createElement("div", { className: "page-setting-group" }, /* @__PURE__ */ React.createElement("div", { className: "page-setting-group-header" }, /* @__PURE__ */ React.createElement(Title, {
		level: 2,
		className: "page-setting-group-title"
	}, locales.getText("page.setting.register.title")), /* @__PURE__ */ React.createElement(Paragraph, { className: "page-setting-group-description" }, locales.getText("page.setting.register.description"))), /* @__PURE__ */ React.createElement("div", { className: "page-setting-group-content" }, /* @__PURE__ */ React.createElement(PageSettingItem, {
		ref: registerPageRef,
		value: registerPageValue,
		onChange: handleRegisterPageChange,
		showImageUpload
	}))), /* @__PURE__ */ React.createElement("div", { className: "page-setting-group" }, /* @__PURE__ */ React.createElement("div", { className: "page-setting-group-header" }, /* @__PURE__ */ React.createElement(Title, {
		level: 2,
		className: "page-setting-group-title"
	}, locales.getText("page.setting.login.title")), /* @__PURE__ */ React.createElement(Paragraph, { className: "page-setting-group-description" }, locales.getText("page.setting.login.description"))), /* @__PURE__ */ React.createElement("div", { className: "page-setting-group-content" }, showRegisterPage && /* @__PURE__ */ React.createElement("div", { className: "page-setting-checkbox" }, /* @__PURE__ */ React.createElement(Checkbox, {
		checked: sameAsRegister,
		onChange: (e) => handleSameAsRegisterChange(e.target.checked)
	}, /* @__PURE__ */ React.createElement("div", { className: "page-setting-checkbox-label" }, /* @__PURE__ */ React.createElement("span", { className: "page-setting-checkbox-label-text" }, locales.getText("page.setting.login.sameAsRegister")), /* @__PURE__ */ React.createElement("span", { className: "page-setting-checkbox-label-description" }, locales.getText("page.setting.login.sameAsRegister.description"))))), (!sameAsRegister || !showRegisterPage) && /* @__PURE__ */ React.createElement(PageSettingItem, {
		ref: loginPageRef,
		value: loginPageValue,
		onChange: handleLoginPageChange,
		showImageUpload
	}))));
});
PageSetting.displayName = "PageSetting";
//#endregion
export { PageSetting as default };
