function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React from 'react';
import { Drawer, Button } from 'antd';
import { locales } from '@pisell/utils';
import { AuthMethodType } from "./types";
import { EmailSettingsContent, PhoneSettingsContent, DefaultSettingsContent, PasswordRulesSettingsContent } from "./components";
import "./SettingsDrawer.less";
import localeTexts from "./locales";

// 初始化 locales
locales.init(localeTexts, localStorage.getItem('umi_locale') || 'en');
var SettingsDrawer = function SettingsDrawer(_ref) {
  var open = _ref.open,
    method = _ref.method,
    onClose = _ref.onClose,
    onSave = _ref.onSave,
    _ref$mode = _ref.mode,
    mode = _ref$mode === void 0 ? 'auth-methods' : _ref$mode,
    initialSettings = _ref.initialSettings;
  var _React$useState = React.useState(null),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    currentSettings = _React$useState2[0],
    setCurrentSettings = _React$useState2[1];
  var handleSave = function handleSave() {
    // 只在点击保存按钮时才调用 onSave
    if (currentSettings) {
      onSave === null || onSave === void 0 || onSave(currentSettings);
    }
    onClose();
  };
  var handleCancel = function handleCancel() {
    onClose();
  };
  var getDrawerTitle = function getDrawerTitle() {
    if (!method) return locales.getText('auth.email.settings');
    if (method.type === AuthMethodType.EMAIL) {
      return locales.getText('auth.email.settings');
    }
    if (method.type === AuthMethodType.PHONE) {
      return locales.getText('auth.phone.settings');
    }
    if (method.type === AuthMethodType.PASSWORD_RULES) {
      return locales.getText('auth.password.settings');
    }
    return "".concat(method.title, " Settings");
  };
  var handleSettingsChange = function handleSettingsChange(settings) {
    // 实时更新设置到本地状态，不触发保存
    setCurrentSettings(settings);
    console.log('Settings changed:', settings);
  };
  var renderContent = function renderContent() {
    if (!method) return null;
    switch (method.type) {
      case AuthMethodType.EMAIL:
        return /*#__PURE__*/React.createElement(EmailSettingsContent, {
          initialSettings: initialSettings,
          onSettingsChange: handleSettingsChange
        });
      case AuthMethodType.PHONE:
        return /*#__PURE__*/React.createElement(PhoneSettingsContent, {
          initialSettings: initialSettings,
          onSettingsChange: handleSettingsChange
        });
      case AuthMethodType.PASSWORD_RULES:
        return /*#__PURE__*/React.createElement(PasswordRulesSettingsContent, {
          initialSettings: initialSettings,
          onSettingsChange: handleSettingsChange
        });
      default:
        // SettingList 模式下，对于没有特定设置的方法，显示空状态或提示
        if (mode === 'setting-list') {
          return /*#__PURE__*/React.createElement("div", {
            className: "settings-section"
          }, /*#__PURE__*/React.createElement("div", {
            className: "settings-section-description",
            style: {
              padding: '20px 0',
              color: '#8c8c8c'
            }
          }, locales.getText('auth.settings.no.additional')));
        }
        // AuthMethods 模式下，显示默认的启用/禁用设置
        return /*#__PURE__*/React.createElement(DefaultSettingsContent, {
          method: method,
          onSettingsChange: handleSettingsChange
        });
    }
  };
  return /*#__PURE__*/React.createElement(Drawer, {
    title: getDrawerTitle(),
    placement: "right",
    width: 640,
    open: open,
    onClose: handleCancel,
    maskClosable: true,
    keyboard: true,
    footer: /*#__PURE__*/React.createElement("div", {
      className: "drawer-footer"
    }, /*#__PURE__*/React.createElement(Button, {
      onClick: handleCancel
    }, locales.getText('auth.button.cancel')), /*#__PURE__*/React.createElement(Button, {
      type: "primary",
      onClick: handleSave,
      style: {
        marginLeft: 8
      }
    }, locales.getText('auth.button.save')))
  }, /*#__PURE__*/React.createElement("div", {
    className: "settings-drawer-content",
    onClick: function onClick(e) {
      return e.stopPropagation();
    }
  }, renderContent()));
};
export default SettingsDrawer;