import { _asyncToGenerator } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import usePasswordValidationRules from "../hooks/usePasswordValidationRules.js";
import React, { useState } from "react";
import { Form, message } from "antd";
import { locales } from "@pisell/utils";
import { Button as Button$1, Input as Input$1 } from "@pisell/materials";
import { EyeInvisibleOutlined, EyeOutlined } from "@ant-design/icons";
//#region src/pro/Login2.0/steps/SetPassword.tsx
/**
* 设置密码页面（对应图5）
*/
function SetPassword({ flowData, onSubmit, onBack, loading = false, passwordRules = {
	minLength: 8,
	requireNumber: true
}, scenario = "create" }) {
	const [form] = Form.useForm();
	const [showPassword, setShowPassword] = useState(false);
	const [showConfirmPassword, setShowConfirmPassword] = useState(false);
	const handleSubmit = function() {
		var _ref = _asyncToGenerator(function* () {
			try {
				const values = yield form.validateFields();
				yield onSubmit === null || onSubmit === void 0 ? void 0 : onSubmit(values.password, values.confirmPassword);
			} catch (error) {
				if (error.errorFields) return;
				message.error((error === null || error === void 0 ? void 0 : error.message) || locales.getText("pisell-set-password-failed"));
			}
		});
		return function handleSubmit() {
			return _ref.apply(this, arguments);
		};
	}();
	const passwordValidationRules = usePasswordValidationRules(passwordRules, {
		translate: locales.getText,
		fallbackMinLength: 8,
		messageKeys: {
			required: locales.getText("pisell-set-password-required"),
			minLength: locales.getText("pisell-set-password-min-length"),
			requireUppercase: locales.getText("pisell-set-password-require-uppercase"),
			requireLowercase: locales.getText("pisell-set-password-require-lowercase"),
			requireNumber: locales.getText("pisell-set-password-require-number"),
			requireSpecialCharacters: locales.getText("pisell-set-password-require-special"),
			summary: locales.getText("pisell-set-password-must-contain")
		}
	});
	return /* @__PURE__ */ React.createElement("div", { className: "register-step-container" }, /* @__PURE__ */ React.createElement("div", { className: "register-step-content" }, /* @__PURE__ */ React.createElement("h2", { className: "register-step-title" }, scenario === "create" ? locales.getText("pisell-set-password-create-title") : locales.getText("pisell-set-password-reset-title")), /* @__PURE__ */ React.createElement(Form, {
		form,
		layout: "vertical",
		className: "password-set-form"
	}, /* @__PURE__ */ React.createElement(Form.Item, {
		name: "password",
		rules: passwordValidationRules
	}, /* @__PURE__ */ React.createElement(Input$1.Password, {
		size: "large",
		placeholder: locales.getText("pisell-set-password-placeholder"),
		className: "password-input",
		autoComplete: "new-password",
		iconRender: (visible) => visible ? /* @__PURE__ */ React.createElement(EyeOutlined, null) : /* @__PURE__ */ React.createElement(EyeInvisibleOutlined, null)
	})), /* @__PURE__ */ React.createElement(Form.Item, {
		name: "confirmPassword",
		dependencies: ["password"],
		rules: [{
			required: true,
			message: locales.getText("pisell-set-password-confirm-required")
		}, ({ getFieldValue }) => ({ validator(_, value) {
			if (!value || getFieldValue("password") === value) return Promise.resolve();
			return Promise.reject(new Error(locales.getText("pisell-set-password-not-match")));
		} })]
	}, /* @__PURE__ */ React.createElement(Input$1.Password, {
		size: "large",
		placeholder: locales.getText("pisell-set-password-confirm-placeholder"),
		className: "password-input",
		autoComplete: "new-password",
		iconRender: (visible) => visible ? /* @__PURE__ */ React.createElement(EyeOutlined, null) : /* @__PURE__ */ React.createElement(EyeInvisibleOutlined, null)
	})), /* @__PURE__ */ React.createElement(Button$1, {
		type: "primary",
		size: "large",
		block: true,
		onClick: handleSubmit,
		loading,
		className: "register-submit-btn"
	}, scenario === "create" ? locales.getText("pisell-set-password-submit") : locales.getText("pisell-common-confirm")))));
}
//#endregion
export { SetPassword as default };
