import { getText } from "../../../../locales/index.js";
import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import PisellSingleLineText_default from "../../../pisellSingleLineText/PisellSingleLineText.js";
import "../../../pisellSingleLineText/index.js";
import PisellLongText_default from "../../../pisellLongText/PisellLongText.js";
import "../../../pisellLongText/index.js";
import MemoizedPisellNumber from "../../../pisellNumber/PisellNumber.js";
import "../../../pisellNumber/index.js";
import MemoizedPisellCurrency from "../../../pisellCurrency/PisellCurrency.js";
import "../../../pisellCurrency/index.js";
import MemoizedPisellPercent from "../../../pisellPercent/PisellPercent.js";
import "../../../pisellPercent/index.js";
import PisellPhone_default from "../../../pisellPhone/PisellPhone.js";
import "../../../pisellPhone/index.js";
import PisellEmail_default from "../../../pisellEmail/PisellEmail.js";
import "../../../pisellEmail/index.js";
import PisellUrl_default from "../../../pisellUrl/PisellUrl.js";
import "../../../pisellUrl/index.js";
import PisellRating_default from "../../../pisellRating/PisellRating.js";
import "../../../pisellRating/index.js";
import { _objectWithoutProperties } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import PisellSingleSelect from "../../../pisellSingleSelect/PisellSingleSelect.js";
import "../../../pisellSingleSelect/index.js";
import PisellMultipleSelect from "../../../pisellMultipleSelect/PisellMultipleSelect.js";
import "../../../pisellMultipleSelect/index.js";
import { fieldMaps } from "../../../table/Table/fields/index.js";
import AsyncOptionsSelectWrapper from "./AsyncOptionsSelectWrapper.js";
import React, { useMemo } from "react";
import { DatePicker, Form } from "antd";
import dayjs from "dayjs";
import "./index.less";
//#region src/components/filter/components/FilterItem/index.tsx
const _excluded = ["value", "onChange"], _excluded2 = ["getOptions"];
const { RangePicker } = DatePicker;
/** 列 type（RecordBoardFieldType）对应 Pisell 组件，用于筛选项编辑态；与 RecordBoard 列 type 一致 */
const PISELL_FIELD_TYPE_MAP = {
	singleLineText: PisellSingleLineText_default,
	longText: PisellLongText_default,
	email: PisellEmail_default,
	phone: PisellPhone_default,
	url: PisellUrl_default,
	number: MemoizedPisellNumber,
	currency: MemoizedPisellCurrency,
	percent: MemoizedPisellPercent,
	rating: PisellRating_default,
	singleSelect: PisellSingleSelect,
	multipleSelect: PisellMultipleSelect
};
/** 筛选类型到 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.isDayjs(val[0]) ? val[0] : dayjs(val[0]) : null;
		const end = val[1] ? dayjs.isDayjs(val[1]) ? val[1] : dayjs(val[1]) : null;
		if (start && end) return [start, end];
	}
	return null;
}
/** 使用 antd RangePicker，仅做 value 归一化以兼容 form 里存的 string[] */
const AntdRangePickerWithValue = React.forwardRef(function AntdRangePickerWithValue(_ref, ref) {
	let { value, onChange } = _ref, rest = _objectWithoutProperties(_ref, _excluded);
	const normalized = toRangePickerValue(value);
	return /* @__PURE__ */ React.createElement(RangePicker, _objectSpread2(_objectSpread2({}, rest), {}, {
		ref,
		placeholder: [getText("table-action-range-picker-placeholder-start"), 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 = _objectSpread2(_objectSpread2({}, itemProps || {}), other || {});
		delete componentProps.component;
		return /* @__PURE__ */ React.createElement(Form.Item, _objectSpread2({
			label,
			name,
			hidden: isHidden
		}, (other === null || other === void 0 ? void 0 : other.formItemProps) || {}), /* @__PURE__ */ React.createElement(CustomComponent, componentProps));
	}
	if (type === "rangePickerNew") {
		const componentProps = _objectSpread2(_objectSpread2(_objectSpread2({}, itemProps || {}), other || {}), {}, { prefixCls: "pisell-lowcode" });
		return /* @__PURE__ */ React.createElement(Form.Item, _objectSpread2(_objectSpread2({
			label,
			name
		}, (other === null || other === void 0 ? void 0 : other.formItemProps) || {}), {}, {
			hidden: isHidden,
			className: "pisell-lowcode-range-picker-wrap"
		}), /* @__PURE__ */ React.createElement(AntdRangePickerWithValue, componentProps));
	}
	const Com = useMemo(() => {
		var _FILTER_TYPE_TO_FIELD, _fieldMaps;
		if (PisellCom) return PisellCom;
		return (_fieldMaps = 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 = _objectSpread2(_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 = _objectWithoutProperties(componentProps, _excluded2);
		return /* @__PURE__ */ React.createElement(Form.Item, _objectSpread2(_objectSpread2({
			label,
			name
		}, (other === null || other === void 0 ? void 0 : other.formItemProps) || {}), {}, { hidden: isHidden }), /* @__PURE__ */ React.createElement(AsyncOptionsSelectWrapper, _objectSpread2({
			getOptions,
			fieldType: fieldTypeFromColumn
		}, restProps)));
	}
	return /* @__PURE__ */ React.createElement(Form.Item, _objectSpread2(_objectSpread2({
		label,
		name
	}, (other === null || other === void 0 ? void 0 : other.formItemProps) || {}), {}, { hidden: isHidden }), /* @__PURE__ */ React.createElement(Com, componentProps));
};
//#endregion
export { FilterItem as default };
