const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_index = require("../../../../locales/index.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_PisellSingleLineText = require("../../../pisellSingleLineText/PisellSingleLineText.js");
const require_PisellLongText = require("../../../pisellLongText/PisellLongText.js");
const require_PisellNumber = require("../../../pisellNumber/PisellNumber.js");
const require_PisellCurrency = require("../../../pisellCurrency/PisellCurrency.js");
const require_PisellPercent = require("../../../pisellPercent/PisellPercent.js");
const require_PisellPhone = require("../../../pisellPhone/PisellPhone.js");
const require_PisellEmail = require("../../../pisellEmail/PisellEmail.js");
const require_PisellUrl = require("../../../pisellUrl/PisellUrl.js");
const require_PisellRating = require("../../../pisellRating/PisellRating.js");
const require_objectWithoutProperties = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_PisellSingleSelect = require("../../../pisellSingleSelect/PisellSingleSelect.js");
const require_PisellMultipleSelect = require("../../../pisellMultipleSelect/PisellMultipleSelect.js");
const require_index$1 = require("../../../table/Table/fields/index.js");
const require_AsyncOptionsSelectWrapper = require("./AsyncOptionsSelectWrapper.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
require("./index.less");
//#region src/components/filter/components/FilterItem/index.tsx
const _excluded = ["value", "onChange"], _excluded2 = ["getOptions"];
const { RangePicker } = antd.DatePicker;
/** 列 type（RecordBoardFieldType）对应 Pisell 组件，用于筛选项编辑态；与 RecordBoard 列 type 一致 */
const PISELL_FIELD_TYPE_MAP = {
	singleLineText: require_PisellSingleLineText.default,
	longText: require_PisellLongText.default,
	email: require_PisellEmail.default,
	phone: require_PisellPhone.default,
	url: require_PisellUrl.default,
	number: require_PisellNumber.default,
	currency: require_PisellCurrency.default,
	percent: require_PisellPercent.default,
	rating: require_PisellRating.default,
	singleSelect: require_PisellSingleSelect.default,
	multipleSelect: require_PisellMultipleSelect.default
};
/** 筛选类型到 table field 类型的映射（table fields 无 single/input，用 select/text） */
const FILTER_TYPE_TO_FIELD = {
	single: "select",
	input: "text",
	datePicker: "date",
	rangePicker: "rangePicker",
	search: "search"
};
/** 将 form 中可能为 string[] 或 dayjs[] 的值转为 RangePicker 需要的 [Dayjs, Dayjs] | null */
function toRangePickerValue(val) {
	if (val == null) return null;
	if (Array.isArray(val) && val.length >= 2) {
		const start = val[0] ? dayjs.default.isDayjs(val[0]) ? val[0] : (0, dayjs.default)(val[0]) : null;
		const end = val[1] ? dayjs.default.isDayjs(val[1]) ? val[1] : (0, dayjs.default)(val[1]) : null;
		if (start && end) return [start, end];
	}
	return null;
}
/** 使用 antd RangePicker，仅做 value 归一化以兼容 form 里存的 string[] */
const AntdRangePickerWithValue = react.default.forwardRef(function AntdRangePickerWithValue(_ref, ref) {
	let { value, onChange } = _ref, rest = require_objectWithoutProperties._objectWithoutProperties(_ref, _excluded);
	const normalized = toRangePickerValue(value);
	return /* @__PURE__ */ react.default.createElement(RangePicker, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, rest), {}, {
		ref,
		placeholder: [require_index.getText("table-action-range-picker-placeholder-start"), require_index.getText("table-action-range-picker-placeholder-end")],
		value: normalized,
		onChange: (dates) => onChange === null || onChange === void 0 ? void 0 : onChange(dates !== null && dates !== void 0 ? dates : void 0)
	}));
});
const FilterItem = (props) => {
	var _componentProps$getOp;
	const { label, type, name, other, isHidden, props: itemProps } = props;
	const fieldTypeFromColumn = other === null || other === void 0 ? void 0 : other.fieldType;
	const PisellCom = fieldTypeFromColumn ? PISELL_FIELD_TYPE_MAP[fieldTypeFromColumn] : void 0;
	if (type === "custom") {
		const CustomComponent = other === null || other === void 0 ? void 0 : other.component;
		if (!CustomComponent) return null;
		const componentProps = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, itemProps || {}), other || {});
		delete componentProps.component;
		return /* @__PURE__ */ react.default.createElement(antd.Form.Item, require_objectSpread2._objectSpread2({
			label,
			name,
			hidden: isHidden
		}, (other === null || other === void 0 ? void 0 : other.formItemProps) || {}), /* @__PURE__ */ react.default.createElement(CustomComponent, componentProps));
	}
	if (type === "rangePickerNew") {
		const componentProps = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, itemProps || {}), other || {}), {}, { prefixCls: "pisell-lowcode" });
		return /* @__PURE__ */ react.default.createElement(antd.Form.Item, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({
			label,
			name
		}, (other === null || other === void 0 ? void 0 : other.formItemProps) || {}), {}, {
			hidden: isHidden,
			className: "pisell-lowcode-range-picker-wrap"
		}), /* @__PURE__ */ react.default.createElement(AntdRangePickerWithValue, componentProps));
	}
	const Com = (0, react.useMemo)(() => {
		var _FILTER_TYPE_TO_FIELD, _fieldMaps;
		if (PisellCom) return PisellCom;
		return (_fieldMaps = require_index$1.fieldMaps[(_FILTER_TYPE_TO_FIELD = FILTER_TYPE_TO_FIELD[type]) !== null && _FILTER_TYPE_TO_FIELD !== void 0 ? _FILTER_TYPE_TO_FIELD : type]) === null || _fieldMaps === void 0 ? void 0 : _fieldMaps.edit;
	}, [type, PisellCom]);
	if (!Com) return null;
	const isPisell = Boolean(PisellCom);
	const componentProps = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, itemProps || {}), other || {}), isPisell ? { mode: "edit" } : {});
	const getOptions = (_componentProps$getOp = componentProps.getOptions) !== null && _componentProps$getOp !== void 0 ? _componentProps$getOp : other === null || other === void 0 ? void 0 : other.getOptions;
	if (isPisell && typeof getOptions === "function" && (fieldTypeFromColumn === "singleSelect" || fieldTypeFromColumn === "multipleSelect") && fieldTypeFromColumn) {
		const { getOptions: _go } = componentProps, restProps = require_objectWithoutProperties._objectWithoutProperties(componentProps, _excluded2);
		return /* @__PURE__ */ react.default.createElement(antd.Form.Item, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({
			label,
			name
		}, (other === null || other === void 0 ? void 0 : other.formItemProps) || {}), {}, { hidden: isHidden }), /* @__PURE__ */ react.default.createElement(require_AsyncOptionsSelectWrapper.default, require_objectSpread2._objectSpread2({
			getOptions,
			fieldType: fieldTypeFromColumn
		}, restProps)));
	}
	return /* @__PURE__ */ react.default.createElement(antd.Form.Item, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({
		label,
		name
	}, (other === null || other === void 0 ? void 0 : other.formItemProps) || {}), {}, { hidden: isHidden }), /* @__PURE__ */ react.default.createElement(Com, componentProps));
};
//#endregion
exports.default = FilterItem;
