const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_useEngineContext = require("../../../../hooks/useEngineContext.js");
const require_types = require("./types.js");
const require_constants = require("./constants.js");
const require_locales = require("./locales.js");
require("./SettingList.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let _ant_design_icons = require("@ant-design/icons");
let _dnd_kit_core = require("@dnd-kit/core");
let _dnd_kit_sortable = require("@dnd-kit/sortable");
let _dnd_kit_utilities = require("@dnd-kit/utilities");
require("./AuthMethods.less");
//#region src/components/authentication/admin/AuthMethods/index.tsx
const SortableItem = ({ method, onToggle, onSettings, onConfigClick, enableSetting = true }) => {
	const { attributes, listeners, setNodeRef, transform, transition, isDragging } = (0, _dnd_kit_sortable.useSortable)({ id: method.id });
	const style = {
		transform: _dnd_kit_utilities.CSS.Transform.toString(transform),
		transition
	};
	const getCategoryClassName = (category) => {
		var _category$toLowerCase;
		return category === null || category === void 0 || (_category$toLowerCase = category.toLowerCase()) === null || _category$toLowerCase === void 0 ? void 0 : _category$toLowerCase.replace(/[^a-z0-9]/g, "-");
	};
	const getCategoryText = (category) => {
		const categoryMap = {
			[require_types.AuthMethodCategory.GENERAL]: "auth.category.general",
			[require_types.AuthMethodCategory.PASSWORD_FREE]: "auth.category.password-free",
			[require_types.AuthMethodCategory.AUTH_FREE]: "auth.category.auth-free",
			[require_types.AuthMethodCategory.OAUTH]: "auth.category.oauth"
		};
		return _pisell_utils.locales.getText(categoryMap[category] || category);
	};
	return /* @__PURE__ */ react.default.createElement("div", {
		ref: setNodeRef,
		style,
		className: (0, classnames.default)("auth-methods-item", { "auth-methods-dragging": isDragging })
	}, /* @__PURE__ */ react.default.createElement("div", require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({ className: "auth-methods-item-drag" }, attributes), listeners), /* @__PURE__ */ react.default.createElement(_ant_design_icons.HolderOutlined, null)), /* @__PURE__ */ react.default.createElement("div", { className: "auth-methods-item-body" }, /* @__PURE__ */ react.default.createElement("div", { className: "auth-methods-item-icon" }, method.icon), /* @__PURE__ */ react.default.createElement("div", { className: "auth-methods-item-content" }, /* @__PURE__ */ react.default.createElement("div", { className: "auth-methods-item-header" }, /* @__PURE__ */ react.default.createElement("span", { className: "auth-methods-item-title" }, method.title), /* @__PURE__ */ react.default.createElement("span", { className: (0, classnames.default)("auth-methods-item-category", getCategoryClassName(method.category)) }, getCategoryText(method.category))), /* @__PURE__ */ react.default.createElement("div", { className: "auth-methods-item-description" }, method.description), method.configLink && /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement("div", { style: {
		marginTop: 8,
		fontSize: 12,
		color: "#8c8c8c",
		display: "flex",
		alignItems: "center",
		gap: 4
	} }, /* @__PURE__ */ react.default.createElement(_ant_design_icons.InfoCircleOutlined, { style: { marginRight: 4 } }), _pisell_utils.locales.getText(method.configLink), /* @__PURE__ */ react.default.createElement("div", {
		className: "auth-methods-item-config-link",
		onClick: onConfigClick
	}, /* @__PURE__ */ react.default.createElement("span", null, _pisell_utils.locales.getText("auth.oauth.config.link")))))), /* @__PURE__ */ react.default.createElement("div", { className: "auth-methods-item-actions" }, enableSetting && method.hasSettings && /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
		type: "default",
		icon: /* @__PURE__ */ react.default.createElement(_ant_design_icons.SettingOutlined, null),
		onClick: () => onSettings()
	}, _pisell_utils.locales.getText("auth.button.settings")), /* @__PURE__ */ react.default.createElement(antd.Switch, {
		checked: method.enabled,
		onChange: onToggle,
		disabled: method.disableToggle
	}))));
};
const AuthMethods = ({ className, style, methods: initialMethods, onOrderChange, onToggle, onSettings, onConfigClick, enableSetting = true }) => {
	var _context$engine;
	const context = require_useEngineContext.default();
	_pisell_utils.locales.init(require_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 [methods, setMethods] = (0, react.useState)(initialMethods || require_constants.getDefaultAuthMethods());
	(0, react.useEffect)(() => {
		if (initialMethods && initialMethods.length > 0) {
			console.log("AuthMethods 组件同步父组件数据:", initialMethods);
			setMethods([...initialMethods].sort((a, b) => {
				var _a$sort, _b$sort;
				return ((_a$sort = a.sort) !== null && _a$sort !== void 0 ? _a$sort : 0) - ((_b$sort = b.sort) !== null && _b$sort !== void 0 ? _b$sort : 0);
			}));
		}
	}, [initialMethods]);
	const sensors = (0, _dnd_kit_core.useSensors)((0, _dnd_kit_core.useSensor)(_dnd_kit_core.PointerSensor, { activationConstraint: { distance: 8 } }), (0, _dnd_kit_core.useSensor)(_dnd_kit_core.KeyboardSensor, { coordinateGetter: _dnd_kit_sortable.sortableKeyboardCoordinates }));
	const handleDragEnd = (event) => {
		const { active, over } = event;
		if (over && active.id !== over.id) setMethods((items) => {
			const updatedItems = (0, _dnd_kit_sortable.arrayMove)(items, items.findIndex((item) => item.id === active.id), items.findIndex((item) => item.id === over.id)).map((item, index) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, { sort: index }));
			onOrderChange === null || onOrderChange === 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 ? require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, { enabled }) : item));
		onToggle === null || onToggle === void 0 || onToggle(method, enabled);
	};
	const handleSettings = (method) => {
		onSettings === null || onSettings === void 0 || onSettings(method);
	};
	const handleConfigClick = (method) => {
		onConfigClick === null || onConfigClick === void 0 || onConfigClick(method);
	};
	if (!methods || methods.length === 0) return /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)("auth-methods", className),
		style
	}, /* @__PURE__ */ react.default.createElement("div", { className: "auth-methods-empty" }, /* @__PURE__ */ react.default.createElement(antd.Empty, { description: "No authentication methods available" })));
	return /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)("auth-methods", className),
		style
	}, /* @__PURE__ */ react.default.createElement(_dnd_kit_core.DndContext, {
		sensors,
		collisionDetection: _dnd_kit_core.closestCenter,
		onDragEnd: handleDragEnd
	}, /* @__PURE__ */ react.default.createElement(_dnd_kit_sortable.SortableContext, {
		items: methods.map((m) => m.id),
		strategy: _dnd_kit_sortable.verticalListSortingStrategy
	}, methods.map((method) => /* @__PURE__ */ react.default.createElement(SortableItem, {
		key: method.id,
		method,
		onToggle: (enabled) => handleToggle(method, enabled),
		onSettings: () => handleSettings(method),
		onConfigClick: method.configLink ? () => handleConfigClick(method) : void 0,
		enableSetting
	})))));
};
//#endregion
exports.default = AuthMethods;
