const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_objectWithoutProperties = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_useEngineContext = require("../../hooks/useEngineContext.js");
const require_useThemeTokens = require("../../hooks/useThemeTokens.js");
const require_index = require("../NumericStepper/index.js");
const require_locales = require("./locales.js");
const require_index$1 = require("./components/Indicator/index.js");
const require_index$2 = require("./components/LayoutContainer/index.js");
const require_useSelectionController = require("./hooks/useSelectionController.js");
const require_usePresetProps = require("./hooks/usePresetProps.js");
const require_Item = require("./Item.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let _ant_design_icons = require("@ant-design/icons");
require("./selector.less");
//#region src/pro/Selector/Selector.tsx
const _excluded = ["show", "render"], _excluded2 = [
	"disabled",
	"size",
	"style"
];
const ATag = antd.Tag;
const SelectorContext = react.default.createContext({
	disabled: false,
	mode: "single",
	itemLayout: "horizontal",
	valueType: "primitive",
	variant: "default",
	itemProps: {},
	preset: {}
});
const Selector = (0, react.forwardRef)((props, ref) => {
	var _context$appHelper;
	const { disabled, fieldNames, layout, indicatorProps, itemProps, itemLayout = "horizontal", mode = "single", preset = {}, renderItem, ruleConfig, valueType = "primitive", variant = "default" } = props;
	const { colorPrimary, colorPrimaryBg } = require_useThemeTokens.default(props.theme);
	const context = require_useEngineContext.default();
	_pisell_utils.locales.init(require_locales.default, (context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.storage) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.get("umi_locale")) || "en");
	const { state, actions } = require_useSelectionController.default(props);
	const { options, errors, validated, quantityInfo } = state;
	const { showQuantity, remainingCount } = quantityInfo;
	const headerProps = props.headerProps || props.titleProps || {};
	const presetConfig = require_usePresetProps.default({
		mode,
		preset,
		itemLayout,
		variant,
		layout,
		indicatorProps
	});
	const finalLayout = presetConfig.layout;
	const finalIndicatorProps = presetConfig.indicatorProps;
	(0, react.useImperativeHandle)(ref, () => ({
		validate: actions.validate,
		reset: actions.reset,
		clear: actions.clear,
		getErrors: () => {
			return errors || [];
		}
	}));
	(0, react.useEffect)(() => {
		document.body.id = "body";
	}, [actions]);
	const renderIndicator = (option, isDefault) => {
		const _ref = finalIndicatorProps || {}, { show = true, render } = _ref, rest = require_objectWithoutProperties._objectWithoutProperties(_ref, _excluded);
		if ((finalIndicatorProps === null || finalIndicatorProps === void 0 ? void 0 : finalIndicatorProps.show) === false) return null;
		if (react.default.isValidElement(finalIndicatorProps === null || finalIndicatorProps === void 0 ? void 0 : finalIndicatorProps.render)) return finalIndicatorProps === null || finalIndicatorProps === void 0 ? void 0 : finalIndicatorProps.render;
		if (typeof (finalIndicatorProps === null || finalIndicatorProps === void 0 ? void 0 : finalIndicatorProps.render) === "function") {
			var _finalIndicatorProps$;
			return finalIndicatorProps === null || finalIndicatorProps === void 0 || (_finalIndicatorProps$ = finalIndicatorProps.render) === null || _finalIndicatorProps$ === void 0 ? void 0 : _finalIndicatorProps$.call(finalIndicatorProps, require_objectSpread2._objectSpread2({
				option,
				selected: option.selected,
				disabled: disabled || option.disabled,
				actions
			}, rest));
		}
		return /* @__PURE__ */ react.default.createElement(require_index$1.default, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, rest), {}, {
			mode,
			checked: option.selected,
			disabled: disabled || option.disabled,
			onChange: (e) => {
				if (e.target.checked) actions.update(option._key, { quantity: 1 });
				else actions.update(option._key, { quantity: 0 });
			},
			children: isDefault ? option._label : void 0
		}));
	};
	const renderQuantitySelector = (option) => {
		if (valueType === "primitive") return null;
		if (props.showStepper === false) return null;
		if ((props.showStepper === "auto" || props.showStepper === void 0) && !showQuantity) return null;
		return /* @__PURE__ */ react.default.createElement(require_index.default, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, props.stepperProps || {}), {}, {
			min: option.effectiveMin,
			max: option.effectiveMax,
			value: option.quantity,
			disabled: disabled || option.disabled,
			onChange: (value) => {
				actions.update(option._key, { quantity: value });
			}
		}));
	};
	const renderOption = (option) => {
		var _option$ruleConfig$mi, _option$ruleConfig, _ruleConfig$min, _option$ruleConfig$ma, _option$ruleConfig2;
		const optionItemValue = Array.isArray(state.values) ? state.values.find((item) => (item === null || item === void 0 ? void 0 : item.value) === option._key) : [state.values].find((item) => (item === null || item === void 0 ? void 0 : item.value) === option._key);
		if (react.default.isValidElement(renderItem)) return renderItem;
		const configMin = (_option$ruleConfig$mi = option === null || option === void 0 || (_option$ruleConfig = option.ruleConfig) === null || _option$ruleConfig === void 0 ? void 0 : _option$ruleConfig.min) !== null && _option$ruleConfig$mi !== void 0 ? _option$ruleConfig$mi : 0;
		const groupMin = (_ruleConfig$min = ruleConfig === null || ruleConfig === void 0 ? void 0 : ruleConfig.min) !== null && _ruleConfig$min !== void 0 ? _ruleConfig$min : 0;
		const effectiveMin = options.length < 2 && groupMin > 0 ? groupMin : configMin;
		const configMax = (_option$ruleConfig$ma = option === null || option === void 0 || (_option$ruleConfig2 = option.ruleConfig) === null || _option$ruleConfig2 === void 0 ? void 0 : _option$ruleConfig2.max) !== null && _option$ruleConfig$ma !== void 0 ? _option$ruleConfig$ma : Number.MAX_SAFE_INTEGER;
		const effectiveMax = remainingCount !== void 0 ? Math.min(remainingCount + (option.quantity || 0), configMax) : configMax;
		option.effectiveMin = effectiveMin;
		option.effectiveMax = effectiveMax;
		if (typeof renderItem === "function") return renderItem({
			dataSource: option,
			actions,
			optionItemValue,
			indicator: renderIndicator(option),
			numberSelector: renderQuantitySelector(option),
			values: state.values,
			quantityInfo
		});
		const numberSelector = renderQuantitySelector(option);
		const indicator = renderIndicator(option);
		return /* @__PURE__ */ react.default.createElement(require_Item.default, {
			key: option._key,
			dataSource: option,
			actions,
			indicator,
			numberSelector,
			optionItemValue,
			quantityInfo,
			values: state.values
		});
	};
	const renderOptions = () => {
		if (variant === "select") {
			const _value = state.values;
			const _maxCount = ruleConfig === null || ruleConfig === void 0 ? void 0 : ruleConfig.max;
			const _ref2 = props.selectProps || {}, { disabled: selectDisabled, size = "large", style } = _ref2, rest = require_objectWithoutProperties._objectWithoutProperties(_ref2, _excluded2);
			return /* @__PURE__ */ react.default.createElement(antd.Select, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, rest), {}, {
				size,
				disabled: selectDisabled || disabled,
				style: style || { width: "100%" },
				mode: mode === "multiple" ? "multiple" : void 0,
				options,
				fieldNames,
				value: _value,
				onChange: (value) => actions.setValue(value),
				maxCount: _maxCount
			}));
		}
		return /* @__PURE__ */ react.default.createElement(require_index$2.default, {
			className: "pisell-pro-selector_options-container",
			config: finalLayout
		}, options.map(renderOption));
	};
	const renderRequiredTag = () => {
		const { required = false } = ruleConfig || {};
		if (!required) return null;
		const style = { borderRadius: 16 };
		if (errors.find((item) => item.type === "required" || item.type === "min")) return /* @__PURE__ */ react.default.createElement(ATag, {
			icon: /* @__PURE__ */ react.default.createElement(_ant_design_icons.ExclamationCircleOutlined, null),
			color: "error",
			style
		}, _pisell_utils.locales.getText("pisell2.pro.selector.required-tag"));
		if (validated) return /* @__PURE__ */ react.default.createElement(ATag, {
			icon: /* @__PURE__ */ react.default.createElement(_ant_design_icons.CheckCircleOutlined, null),
			color: "success",
			style
		}, _pisell_utils.locales.getText("pisell2.pro.selector.required-tag"));
		return /* @__PURE__ */ react.default.createElement(ATag, { style }, _pisell_utils.locales.getText("pisell2.pro.selector.required-tag"));
	};
	const renderTip = () => {
		var _headerProps$tip2, _headerProps$tip3, _headerProps$tip4;
		if (Array.isArray(errors) && errors.length) {
			var _headerProps$tip;
			const error = errors[errors.length - 1];
			return /* @__PURE__ */ react.default.createElement("span", {
				className: "pisell-pro-selector_tip",
				style: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, (headerProps === null || headerProps === void 0 || (_headerProps$tip = headerProps.tip) === null || _headerProps$tip === void 0 ? void 0 : _headerProps$tip.style) || {}), {}, { color: "#D92D20" })
			}, error === null || error === void 0 ? void 0 : error.message);
		}
		if ((headerProps === null || headerProps === void 0 || (_headerProps$tip2 = headerProps.tip) === null || _headerProps$tip2 === void 0 ? void 0 : _headerProps$tip2.visible) === false) return null;
		let text = headerProps === null || headerProps === void 0 || (_headerProps$tip3 = headerProps.tip) === null || _headerProps$tip3 === void 0 ? void 0 : _headerProps$tip3.text;
		if ((ruleConfig === null || ruleConfig === void 0 ? void 0 : ruleConfig.min) || (ruleConfig === null || ruleConfig === void 0 ? void 0 : ruleConfig.max)) text = _pisell_utils.locales.getText("pisell2.pro.selector.tip.min-max-selection")(ruleConfig === null || ruleConfig === void 0 ? void 0 : ruleConfig.min, ruleConfig === null || ruleConfig === void 0 ? void 0 : ruleConfig.max);
		if (!text) if (mode === "multiple") text = _pisell_utils.locales.getText("pisell2.pro.selector.mode.multiple");
		else text = _pisell_utils.locales.getText("pisell2.pro.selector.tip.require-one-option");
		if (!text) return null;
		return /* @__PURE__ */ react.default.createElement("span", {
			className: "pisell-pro-selector_tip",
			style: require_objectSpread2._objectSpread2({}, (headerProps === null || headerProps === void 0 || (_headerProps$tip4 = headerProps.tip) === null || _headerProps$tip4 === void 0 ? void 0 : _headerProps$tip4.style) || {})
		}, "(", text, ")");
	};
	const renderHeader = () => {
		var _headerProps$icon, _headerProps$icon2, _headerProps$icon3, _headerProps$icon4, _headerProps$title, _headerProps$title2, _headerProps$title3, _headerProps$title$te, _headerProps$title4;
		if ((headerProps === null || headerProps === void 0 ? void 0 : headerProps.visible) === false) return null;
		const _extraContent = (headerProps === null || headerProps === void 0 ? void 0 : headerProps.renderExtra) ? headerProps === null || headerProps === void 0 ? void 0 : headerProps.renderExtra({
			dataSource: options,
			values: state.values
		}) : null;
		return /* @__PURE__ */ react.default.createElement("div", {
			className: "pisell-pro-selector_header",
			style: require_objectSpread2._objectSpread2({}, (headerProps === null || headerProps === void 0 ? void 0 : headerProps.style) || {})
		}, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-pro-selector_header-content-wrapper" }, /* @__PURE__ */ react.default.createElement("div", {
			className: "pisell-pro-selector_header-content",
			style: {
				display: "flex",
				alignItems: "center",
				gap: 8,
				flex: 1
			}
		}, (headerProps === null || headerProps === void 0 || (_headerProps$icon = headerProps.icon) === null || _headerProps$icon === void 0 ? void 0 : _headerProps$icon.visible) === false || !(headerProps === null || headerProps === void 0 || (_headerProps$icon2 = headerProps.icon) === null || _headerProps$icon2 === void 0 ? void 0 : _headerProps$icon2.icon) ? null : /* @__PURE__ */ react.default.createElement("span", { style: headerProps === null || headerProps === void 0 || (_headerProps$icon3 = headerProps.icon) === null || _headerProps$icon3 === void 0 ? void 0 : _headerProps$icon3.style }, headerProps === null || headerProps === void 0 || (_headerProps$icon4 = headerProps.icon) === null || _headerProps$icon4 === void 0 ? void 0 : _headerProps$icon4.icon), (headerProps === null || headerProps === void 0 || (_headerProps$title = headerProps.title) === null || _headerProps$title === void 0 ? void 0 : _headerProps$title.visible) === false || !(headerProps === null || headerProps === void 0 || (_headerProps$title2 = headerProps.title) === null || _headerProps$title2 === void 0 ? void 0 : _headerProps$title2.text) && !props.title ? null : /* @__PURE__ */ react.default.createElement("span", {
			className: "pisell-pro-selector_header-content-title",
			style: require_objectSpread2._objectSpread2({}, (headerProps === null || headerProps === void 0 || (_headerProps$title3 = headerProps.title) === null || _headerProps$title3 === void 0 ? void 0 : _headerProps$title3.style) || {})
		}, (_headerProps$title$te = headerProps === null || headerProps === void 0 || (_headerProps$title4 = headerProps.title) === null || _headerProps$title4 === void 0 ? void 0 : _headerProps$title4.text) !== null && _headerProps$title$te !== void 0 ? _headerProps$title$te : props.title), renderRequiredTag()), renderTip()), _extraContent);
	};
	return /* @__PURE__ */ react.default.createElement("div", {
		id: `pisell-pro-selector-${props.id}`,
		className: (0, classnames.default)("pisell-pro-selector", props.className),
		style: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, props.style || {}), {}, {
			["--color-primary"]: colorPrimary,
			["--color-primary-bg"]: colorPrimaryBg
		})
	}, renderHeader(), /* @__PURE__ */ react.default.createElement(SelectorContext.Provider, { value: {
		mode,
		disabled,
		valueType,
		variant,
		itemLayout,
		itemProps,
		preset
	} }, renderOptions()));
});
//#endregion
exports.SelectorContext = SelectorContext;
exports.default = Selector;
