'use strict';

var React4 = require('react');
var components = require('@tarojs/components');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React4__default = /*#__PURE__*/_interopDefault(React4);

// src/auth/legacy/ui/miniapp/components/LoginModal.tsx
var AuthContext = React4.createContext(void 0);
function useAuth() {
  const context = React4.useContext(AuthContext);
  if (context === void 0) {
    throw new Error("useAuth must be used within an AuthProvider");
  }
  return context;
}

// src/auth/legacy/utils/authUtils.ts
function validatePhoneNumber(phone) {
  return /^1[3-9]\d{9}$/.test(phone);
}
function validatePassword(password) {
  if (!password) {
    return { valid: false, message: "\u5BC6\u7801\u4E0D\u80FD\u4E3A\u7A7A" };
  }
  if (password.length < 6) {
    return { valid: false, message: "\u5BC6\u7801\u957F\u5EA6\u81F3\u5C116\u4F4D" };
  }
  return { valid: true };
}

// src/auth/legacy/ui/miniapp/components/LoginModal.tsx
function LoginModal({ isOpen, onClose, onSuccess, onSwitchToRegister }) {
  const { login } = useAuth();
  const [formData, setFormData] = React4.useState({ phone: "", password: "" });
  const [error, setError] = React4.useState("");
  const [loading, setLoading] = React4.useState(false);
  if (!isOpen) return null;
  const handleSubmit = async () => {
    setError("");
    setLoading(true);
    if (!formData.phone || !formData.password) {
      setError("\u8BF7\u586B\u5199\u5B8C\u6574\u4FE1\u606F");
      setLoading(false);
      return;
    }
    if (!validatePhoneNumber(formData.phone)) {
      setError("\u8BF7\u8F93\u5165\u6B63\u786E\u7684\u624B\u673A\u53F7");
      setLoading(false);
      return;
    }
    const result = await login(formData);
    if (result.success) {
      onSuccess();
    } else {
      setError(result.message || "\u767B\u5F55\u5931\u8D25");
    }
    setLoading(false);
  };
  return /* @__PURE__ */ React4__default.default.createElement(components.View, { className: "auth-modal" }, /* @__PURE__ */ React4__default.default.createElement(components.View, { className: "auth-card" }, /* @__PURE__ */ React4__default.default.createElement(components.View, { className: "auth-header" }, /* @__PURE__ */ React4__default.default.createElement(components.Text, { className: "auth-title" }, "\u7528\u6237\u767B\u5F55"), /* @__PURE__ */ React4__default.default.createElement(components.Button, { className: "auth-close", onClick: onClose }, "\u5173\u95ED")), /* @__PURE__ */ React4__default.default.createElement(components.View, { className: "auth-form" }, /* @__PURE__ */ React4__default.default.createElement(
    components.Input,
    {
      name: "phone",
      value: formData.phone,
      onInput: (e) => setFormData((prev) => ({ ...prev, phone: e.detail.value })),
      placeholder: "\u8BF7\u8F93\u5165\u624B\u673A\u53F7",
      type: "text"
    }
  ), /* @__PURE__ */ React4__default.default.createElement(
    components.Input,
    {
      name: "password",
      value: formData.password,
      onInput: (e) => setFormData((prev) => ({ ...prev, password: e.detail.value })),
      placeholder: "\u8BF7\u8F93\u5165\u5BC6\u7801",
      type: "password"
    }
  ), error && /* @__PURE__ */ React4__default.default.createElement(components.Text, { className: "auth-error" }, error), /* @__PURE__ */ React4__default.default.createElement(components.Button, { className: "auth-submit", loading, onClick: handleSubmit }, loading ? "\u767B\u5F55\u4E2D..." : "\u767B\u5F55"), onSwitchToRegister && /* @__PURE__ */ React4__default.default.createElement(components.Button, { className: "auth-switch", onClick: onSwitchToRegister }, "\u53BB\u6CE8\u518C"))));
}
function RegisterModal({ isOpen, onClose, onSuccess, onSwitchToLogin }) {
  const { register } = useAuth();
  const [formData, setFormData] = React4.useState({
    phone: "",
    name: "",
    password: "",
    confirmPassword: ""
  });
  const [error, setError] = React4.useState("");
  const [loading, setLoading] = React4.useState(false);
  if (!isOpen) return null;
  const handleSubmit = async () => {
    setError("");
    setLoading(true);
    if (!formData.phone || !formData.password || !formData.confirmPassword) {
      setError("\u8BF7\u586B\u5199\u5B8C\u6574\u4FE1\u606F");
      setLoading(false);
      return;
    }
    if (!validatePhoneNumber(formData.phone)) {
      setError("\u8BF7\u8F93\u5165\u6B63\u786E\u7684\u624B\u673A\u53F7");
      setLoading(false);
      return;
    }
    const passwordValidation = validatePassword(formData.password);
    if (!passwordValidation.valid) {
      setError(passwordValidation.message || "\u5BC6\u7801\u683C\u5F0F\u9519\u8BEF");
      setLoading(false);
      return;
    }
    if (formData.password !== formData.confirmPassword) {
      setError("\u4E24\u6B21\u8F93\u5165\u7684\u5BC6\u7801\u4E0D\u4E00\u81F4");
      setLoading(false);
      return;
    }
    const result = await register({
      phone: formData.phone,
      password: formData.password,
      name: formData.name || void 0
    });
    if (result.success) {
      onSuccess();
    } else {
      setError(result.message || "\u6CE8\u518C\u5931\u8D25");
    }
    setLoading(false);
  };
  return /* @__PURE__ */ React4__default.default.createElement(components.View, { className: "auth-modal" }, /* @__PURE__ */ React4__default.default.createElement(components.View, { className: "auth-card" }, /* @__PURE__ */ React4__default.default.createElement(components.View, { className: "auth-header" }, /* @__PURE__ */ React4__default.default.createElement(components.Text, { className: "auth-title" }, "\u7528\u6237\u6CE8\u518C"), /* @__PURE__ */ React4__default.default.createElement(components.Button, { className: "auth-close", onClick: onClose }, "\u5173\u95ED")), /* @__PURE__ */ React4__default.default.createElement(components.View, { className: "auth-form" }, /* @__PURE__ */ React4__default.default.createElement(
    components.Input,
    {
      name: "phone",
      value: formData.phone,
      onInput: (e) => setFormData((prev) => ({ ...prev, phone: e.detail.value })),
      placeholder: "\u8BF7\u8F93\u5165\u624B\u673A\u53F7",
      type: "text"
    }
  ), /* @__PURE__ */ React4__default.default.createElement(
    components.Input,
    {
      name: "name",
      value: formData.name,
      onInput: (e) => setFormData((prev) => ({ ...prev, name: e.detail.value })),
      placeholder: "\u8BF7\u8F93\u5165\u6635\u79F0",
      type: "text"
    }
  ), /* @__PURE__ */ React4__default.default.createElement(
    components.Input,
    {
      name: "password",
      value: formData.password,
      onInput: (e) => setFormData((prev) => ({ ...prev, password: e.detail.value })),
      placeholder: "\u8BF7\u8F93\u5165\u5BC6\u7801",
      type: "password"
    }
  ), /* @__PURE__ */ React4__default.default.createElement(
    components.Input,
    {
      name: "confirmPassword",
      value: formData.confirmPassword,
      onInput: (e) => setFormData((prev) => ({ ...prev, confirmPassword: e.detail.value })),
      placeholder: "\u8BF7\u786E\u8BA4\u5BC6\u7801",
      type: "password"
    }
  ), error && /* @__PURE__ */ React4__default.default.createElement(components.Text, { className: "auth-error" }, error), /* @__PURE__ */ React4__default.default.createElement(components.Button, { className: "auth-submit", loading, onClick: handleSubmit }, loading ? "\u6CE8\u518C\u4E2D..." : "\u6CE8\u518C"), onSwitchToLogin && /* @__PURE__ */ React4__default.default.createElement(components.Button, { className: "auth-switch", onClick: onSwitchToLogin }, "\u53BB\u767B\u5F55"))));
}
function ForgotPasswordModal({ isOpen, onClose, onSuccess }) {
  const [step, setStep] = React4.useState("phone");
  const [formData, setFormData] = React4.useState({
    phone: "",
    verificationCode: "",
    newPassword: "",
    confirmPassword: ""
  });
  const [error, setError] = React4.useState("");
  const [loading, setLoading] = React4.useState(false);
  if (!isOpen) return null;
  const handleSendCode = async () => {
    setError("");
    setLoading(true);
    if (!validatePhoneNumber(formData.phone)) {
      setError("\u8BF7\u8F93\u5165\u6B63\u786E\u7684\u624B\u673A\u53F7");
      setLoading(false);
      return;
    }
    try {
      const response = await fetch("/api/auth/send-verification-code", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ phone: formData.phone })
      });
      const data = await response.json();
      if (data.success) {
        setStep("verify");
      } else {
        setError(data.message || "\u53D1\u9001\u5931\u8D25");
      }
    } catch (error2) {
      console.error("\u{1F4A5} [ForgotPasswordModal] \u53D1\u9001\u9A8C\u8BC1\u7801\u5F02\u5E38:", error2);
      setError("\u53D1\u9001\u5931\u8D25\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5");
    } finally {
      setLoading(false);
    }
  };
  const handleVerifyCode = () => {
    if (!formData.verificationCode) {
      setError("\u8BF7\u8F93\u5165\u9A8C\u8BC1\u7801");
      return;
    }
    setStep("reset");
  };
  const handleResetPassword = async () => {
    const passwordValidation = validatePassword(formData.newPassword);
    if (!passwordValidation.valid) {
      setError(passwordValidation.message || "\u5BC6\u7801\u683C\u5F0F\u9519\u8BEF");
      return;
    }
    if (formData.newPassword !== formData.confirmPassword) {
      setError("\u4E24\u6B21\u8F93\u5165\u7684\u5BC6\u7801\u4E0D\u4E00\u81F4");
      return;
    }
    setLoading(true);
    try {
      const response = await fetch("/api/auth/reset-password", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({
          phone: formData.phone,
          verificationCode: formData.verificationCode,
          newPassword: formData.newPassword
        })
      });
      const data = await response.json();
      if (data.success) {
        onSuccess();
      } else {
        setError(data.message || "\u91CD\u7F6E\u5931\u8D25");
      }
    } catch (error2) {
      console.error("\u{1F4A5} [ForgotPasswordModal] \u91CD\u7F6E\u5BC6\u7801\u5F02\u5E38:", error2);
      setError("\u91CD\u7F6E\u5931\u8D25\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5");
    } finally {
      setLoading(false);
    }
  };
  const renderContent = () => {
    if (step === "phone") {
      return /* @__PURE__ */ React4__default.default.createElement(React4__default.default.Fragment, null, /* @__PURE__ */ React4__default.default.createElement(
        components.Input,
        {
          name: "phone",
          value: formData.phone,
          onInput: (e) => setFormData((prev) => ({ ...prev, phone: e.detail.value })),
          placeholder: "\u8BF7\u8F93\u5165\u624B\u673A\u53F7",
          type: "text"
        }
      ), /* @__PURE__ */ React4__default.default.createElement(components.Button, { className: "auth-submit", loading, onClick: handleSendCode }, loading ? "\u53D1\u9001\u4E2D..." : "\u53D1\u9001\u9A8C\u8BC1\u7801"));
    }
    if (step === "verify") {
      return /* @__PURE__ */ React4__default.default.createElement(React4__default.default.Fragment, null, /* @__PURE__ */ React4__default.default.createElement(
        components.Input,
        {
          name: "verificationCode",
          value: formData.verificationCode,
          onInput: (e) => setFormData((prev) => ({ ...prev, verificationCode: e.detail.value })),
          placeholder: "\u8BF7\u8F93\u5165\u9A8C\u8BC1\u7801",
          type: "text"
        }
      ), /* @__PURE__ */ React4__default.default.createElement(components.Button, { className: "auth-submit", onClick: handleVerifyCode }, "\u4E0B\u4E00\u6B65"));
    }
    return /* @__PURE__ */ React4__default.default.createElement(React4__default.default.Fragment, null, /* @__PURE__ */ React4__default.default.createElement(
      components.Input,
      {
        name: "newPassword",
        value: formData.newPassword,
        onInput: (e) => setFormData((prev) => ({ ...prev, newPassword: e.detail.value })),
        placeholder: "\u8BF7\u8F93\u5165\u65B0\u5BC6\u7801",
        type: "password"
      }
    ), /* @__PURE__ */ React4__default.default.createElement(
      components.Input,
      {
        name: "confirmPassword",
        value: formData.confirmPassword,
        onInput: (e) => setFormData((prev) => ({ ...prev, confirmPassword: e.detail.value })),
        placeholder: "\u8BF7\u786E\u8BA4\u65B0\u5BC6\u7801",
        type: "password"
      }
    ), /* @__PURE__ */ React4__default.default.createElement(components.Button, { className: "auth-submit", loading, onClick: handleResetPassword }, loading ? "\u63D0\u4EA4\u4E2D..." : "\u91CD\u7F6E\u5BC6\u7801"));
  };
  return /* @__PURE__ */ React4__default.default.createElement(components.View, { className: "auth-modal" }, /* @__PURE__ */ React4__default.default.createElement(components.View, { className: "auth-card" }, /* @__PURE__ */ React4__default.default.createElement(components.View, { className: "auth-header" }, /* @__PURE__ */ React4__default.default.createElement(components.Text, { className: "auth-title" }, "\u627E\u56DE\u5BC6\u7801"), /* @__PURE__ */ React4__default.default.createElement(components.Button, { className: "auth-close", onClick: onClose }, "\u5173\u95ED")), /* @__PURE__ */ React4__default.default.createElement(components.View, { className: "auth-form" }, renderContent(), error && /* @__PURE__ */ React4__default.default.createElement(components.Text, { className: "auth-error" }, error))));
}

exports.ForgotPasswordModal = ForgotPasswordModal;
exports.LoginModal = LoginModal;
exports.RegisterModal = RegisterModal;
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map