var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/authentication/admin/AuthMethods/index.tsx
var AuthMethods_exports = {};
__export(AuthMethods_exports, {
  AuthMethodCategory: () => import_types2.AuthMethodCategory,
  AuthMethodType: () => import_types2.AuthMethodType,
  SettingList: () => import_SettingList.default,
  default: () => AuthMethods_default
});
module.exports = __toCommonJS(AuthMethods_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_materials = require("@pisell/materials");
var import_icons = require("@ant-design/icons");
var import_core = require("@dnd-kit/core");
var import_sortable = require("@dnd-kit/sortable");
var import_utilities = require("@dnd-kit/utilities");
var import_classnames = __toESM(require("classnames"));
var import_utils = require("@pisell/utils");
var import_types = require("./types");
var import_constants = require("./constants");
var import_locales = __toESM(require("./locales"));
var import_useEngineContext = __toESM(require("../../../../hooks/useEngineContext"));
var import_AuthMethods = require("./AuthMethods.less");
var import_SettingList = __toESM(require("./SettingList"));
var import_types2 = require("./types");
var SortableItem = ({
  method,
  onToggle,
  onSettings,
  onConfigClick,
  enableSetting = true
}) => {
  const { attributes, listeners, setNodeRef, transform, transition, isDragging } = (0, import_sortable.useSortable)({
    id: method.id
  });
  const style = {
    transform: import_utilities.CSS.Transform.toString(transform),
    transition
  };
  const getCategoryClassName = (category) => {
    var _a;
    return (_a = category == null ? void 0 : category.toLowerCase()) == null ? void 0 : _a.replace(/[^a-z0-9]/g, "-");
  };
  const getCategoryText = (category) => {
    const categoryMap = {
      [import_types.AuthMethodCategory.GENERAL]: "auth.category.general",
      [import_types.AuthMethodCategory.PASSWORD_FREE]: "auth.category.password-free",
      [import_types.AuthMethodCategory.AUTH_FREE]: "auth.category.auth-free",
      [import_types.AuthMethodCategory.OAUTH]: "auth.category.oauth"
    };
    return import_utils.locales.getText(categoryMap[category] || category);
  };
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      ref: setNodeRef,
      style,
      className: (0, import_classnames.default)("auth-methods-item", {
        "auth-methods-dragging": isDragging
      })
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "auth-methods-item-drag", ...attributes, ...listeners }, /* @__PURE__ */ import_react.default.createElement(import_icons.HolderOutlined, null)),
    /* @__PURE__ */ import_react.default.createElement("div", { className: "auth-methods-item-body" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "auth-methods-item-icon" }, method.icon), /* @__PURE__ */ import_react.default.createElement("div", { className: "auth-methods-item-content" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "auth-methods-item-header" }, /* @__PURE__ */ import_react.default.createElement("span", { className: "auth-methods-item-title" }, method.title), /* @__PURE__ */ import_react.default.createElement(
      "span",
      {
        className: (0, import_classnames.default)(
          "auth-methods-item-category",
          getCategoryClassName(method.category)
        )
      },
      getCategoryText(method.category)
    )), /* @__PURE__ */ import_react.default.createElement("div", { className: "auth-methods-item-description" }, method.description), method.configLink && /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("div", { style: { marginTop: 8, fontSize: 12, color: "#8c8c8c", display: "flex", alignItems: "center", gap: 4 } }, /* @__PURE__ */ import_react.default.createElement(import_icons.InfoCircleOutlined, { style: { marginRight: 4 } }), import_utils.locales.getText(method.configLink), /* @__PURE__ */ import_react.default.createElement("div", { className: "auth-methods-item-config-link", onClick: onConfigClick }, /* @__PURE__ */ import_react.default.createElement("span", null, import_utils.locales.getText("auth.oauth.config.link")))))), /* @__PURE__ */ import_react.default.createElement("div", { className: "auth-methods-item-actions" }, enableSetting && method.hasSettings && /* @__PURE__ */ import_react.default.createElement(
      import_materials.Button,
      {
        type: "default",
        icon: /* @__PURE__ */ import_react.default.createElement(import_icons.SettingOutlined, null),
        onClick: () => onSettings()
      },
      import_utils.locales.getText("auth.button.settings")
    ), /* @__PURE__ */ import_react.default.createElement(import_antd.Switch, { checked: method.enabled, onChange: onToggle, disabled: method.disableToggle })))
  );
};
var AuthMethods = ({
  className,
  style,
  methods: initialMethods,
  onOrderChange,
  onToggle,
  onSettings,
  onConfigClick,
  enableSetting = true
}) => {
  var _a, _b;
  const context = (0, import_useEngineContext.default)();
  import_utils.locales.init(import_locales.default, ((_b = (_a = context == null ? void 0 : context.engine) == null ? void 0 : _a.props) == null ? void 0 : _b.locale) || "en");
  const [methods, setMethods] = (0, import_react.useState)(initialMethods || (0, import_constants.getDefaultAuthMethods)());
  (0, import_react.useEffect)(() => {
    if (initialMethods && initialMethods.length > 0) {
      console.log("AuthMethods 组件同步父组件数据:", initialMethods);
      const sortedMethods = [...initialMethods].sort((a, b) => {
        const sortA = a.sort ?? 0;
        const sortB = b.sort ?? 0;
        return sortA - sortB;
      });
      setMethods(sortedMethods);
    }
  }, [initialMethods]);
  const sensors = (0, import_core.useSensors)(
    (0, import_core.useSensor)(import_core.PointerSensor, {
      activationConstraint: {
        distance: 8
      }
    }),
    (0, import_core.useSensor)(import_core.KeyboardSensor, {
      coordinateGetter: import_sortable.sortableKeyboardCoordinates
    })
  );
  const handleDragEnd = (event) => {
    const { active, over } = event;
    if (over && active.id !== over.id) {
      setMethods((items) => {
        const oldIndex = items.findIndex((item) => item.id === active.id);
        const newIndex = items.findIndex((item) => item.id === over.id);
        const newItems = (0, import_sortable.arrayMove)(items, oldIndex, newIndex);
        const updatedItems = newItems.map((item, index) => ({
          ...item,
          sort: index
        }));
        onOrderChange == null ? void 0 : onOrderChange(updatedItems);
        return updatedItems;
      });
    }
  };
  const handleToggle = (method, enabled) => {
    console.log("AuthMethods handleToggle:", method.id, enabled);
    setMethods(
      (items) => items.map((item) => item.id === method.id ? { ...item, enabled } : item)
    );
    onToggle == null ? void 0 : onToggle(method, enabled);
  };
  const handleSettings = (method) => {
    onSettings == null ? void 0 : onSettings(method);
  };
  const handleConfigClick = (method) => {
    onConfigClick == null ? void 0 : onConfigClick(method);
  };
  if (!methods || methods.length === 0) {
    return /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("auth-methods", className), style }, /* @__PURE__ */ import_react.default.createElement("div", { className: "auth-methods-empty" }, /* @__PURE__ */ import_react.default.createElement(import_antd.Empty, { description: "No authentication methods available" })));
  }
  return /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("auth-methods", className), style }, /* @__PURE__ */ import_react.default.createElement(import_core.DndContext, { sensors, collisionDetection: import_core.closestCenter, onDragEnd: handleDragEnd }, /* @__PURE__ */ import_react.default.createElement(import_sortable.SortableContext, { items: methods.map((m) => m.id), strategy: import_sortable.verticalListSortingStrategy }, methods.map((method) => /* @__PURE__ */ import_react.default.createElement(
    SortableItem,
    {
      key: method.id,
      method,
      onToggle: (enabled) => handleToggle(method, enabled),
      onSettings: () => handleSettings(method),
      onConfigClick: method.configLink ? () => handleConfigClick(method) : void 0,
      enableSetting
    }
  )))));
};
var AuthMethods_default = AuthMethods;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  AuthMethodCategory,
  AuthMethodType,
  SettingList
});
