const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_index = require("../../fields/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
//#region src/components/dataSourceComponents/dataSourceTable/filter/utils.tsx
const genGetField = (dataSource) => {
	const { fields = [] } = dataSource || {};
	const fieldMap = fields.reduce((acc, field) => {
		acc[field.name] = field;
		return acc;
	}, {});
	return (key) => {
		return fieldMap[key];
	};
};
const fieldType2FilterMap = {
	Checkbox: "FormItemSelect",
	ColorPicker: "FormItemInput",
	DatePicker: "FormItemDateRangePicker",
	Input: "FormItemInput",
	Radio: "FormItemSelect",
	Select: "FormItemSelect",
	Percent: "FormItemPercent",
	InputNumber: "FormItemInputNumber",
	"Input.JSON": "FormItemInput",
	"Input.Email": "FormItemInput",
	"Input.URL": "FormItemInput",
	"Input.Password": "FormItemInput",
	"Radio.Group": "FormItemSelect",
	"Checkbox.Group": "FormItemSelect",
	"Translation": "FormItemTranslation"
};
const getCptProps = (field, cptType, item) => {
	var _field$uiSchema, _field$uiSchema2, _field$uiSchema3, _field$uiSchema6, _field$uiSchema8, _item$other;
	let props = (field === null || field === void 0 || (_field$uiSchema = field.uiSchema) === null || _field$uiSchema === void 0 ? void 0 : _field$uiSchema["x-component-props"]) || {};
	props.style = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, (props === null || props === void 0 ? void 0 : props.style) || {}), {}, {
		width: "200px",
		margin: 0
	});
	props.placeholder = item.label;
	props.allowClear = true;
	if (cptType === "FormItemSelect") {
		props.mode = "multiple";
		props.showSearch = true;
		props.optionFilterProp = "label";
	}
	if ([
		"Select",
		"Radio.Group",
		"Checkbox.Group"
	].includes(field === null || field === void 0 || (_field$uiSchema2 = field.uiSchema) === null || _field$uiSchema2 === void 0 ? void 0 : _field$uiSchema2["x-component"]) && ((field === null || field === void 0 || (_field$uiSchema3 = field.uiSchema) === null || _field$uiSchema3 === void 0 || (_field$uiSchema3 = _field$uiSchema3.enum) === null || _field$uiSchema3 === void 0 ? void 0 : _field$uiSchema3.length) || 0) > 0) {
		var _field$uiSchema4, _field$uiSchema5;
		if (["Radio.Group", "Checkbox.Group"].includes(field === null || field === void 0 || (_field$uiSchema4 = field.uiSchema) === null || _field$uiSchema4 === void 0 ? void 0 : _field$uiSchema4["x-component"])) props.direction = "horizontal";
		else {
			props.showSearch = true;
			props.optionFilterProp = "label";
		}
		props.options = field === null || field === void 0 || (_field$uiSchema5 = field.uiSchema) === null || _field$uiSchema5 === void 0 ? void 0 : _field$uiSchema5.enum;
	}
	if ((field === null || field === void 0 || (_field$uiSchema6 = field.uiSchema) === null || _field$uiSchema6 === void 0 ? void 0 : _field$uiSchema6["x-component"]) === "ColorPicker") {
		var _field$uiSchema7;
		props.defaultValue = field === null || field === void 0 || (_field$uiSchema7 = field.uiSchema) === null || _field$uiSchema7 === void 0 ? void 0 : _field$uiSchema7.default;
	}
	if (cptType === "DateRangePicker") props.style = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, (props === null || props === void 0 ? void 0 : props.style) || {}), {}, { width: "240px" });
	if ((field === null || field === void 0 || (_field$uiSchema8 = field.uiSchema) === null || _field$uiSchema8 === void 0 ? void 0 : _field$uiSchema8["x-component"]) === "Checkbox" && cptType === "Select") {
		props.mode = void 0;
		props.allowClear = true;
		props.options = [{
			label: "是",
			value: true
		}, {
			label: "否",
			value: false
		}];
	}
	props = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, props), (item === null || item === void 0 || (_item$other = item.other) === null || _item$other === void 0 ? void 0 : _item$other.fieldProps) || {});
	return props;
};
const field2Cpt = (field, item, otherProps) => {
	const { isCustom, type: configType } = item;
	const { uiSchema } = field || {};
	const { "x-component": xComponent } = uiSchema || {};
	let cptType = fieldType2FilterMap[xComponent];
	if (isCustom) cptType = configType;
	const Cpt = require_index.formFieldMap[cptType];
	if (!Cpt) return null;
	const props = getCptProps(field, cptType, item);
	const style = (0, _pisell_utils.isFunction)(otherProps === null || otherProps === void 0 ? void 0 : otherProps.style) ? otherProps === null || otherProps === void 0 ? void 0 : otherProps.style(props.style) : (otherProps === null || otherProps === void 0 ? void 0 : otherProps.style) ? require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, props.style), otherProps === null || otherProps === void 0 ? void 0 : otherProps.style) : props.style;
	return /* @__PURE__ */ react.default.createElement(Cpt, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, props), {}, {
		label: item.label,
		name: item.name || item.value
	}, otherProps), {}, { style }));
};
const genFilterItems = (list, dataSource) => {
	return list.map((item) => {
		if (!item) return null;
		const { name } = item;
		return field2Cpt(genGetField(dataSource)(name), item);
	});
};
const FILTER_STORAGE_KEY = "filter-setting-";
const getStorageKey = (componentId) => `${FILTER_STORAGE_KEY}${componentId}`;
const getStoredFilterOrder = (componentId) => {
	try {
		const stored = localStorage.getItem(getStorageKey(componentId));
		return stored ? JSON.parse(stored) : null;
	} catch (e) {
		console.error("Failed to parse stored filter order:", e);
		return null;
	}
};
const saveFilterOrder = (componentId, order) => {
	try {
		localStorage.setItem(getStorageKey(componentId), JSON.stringify(order));
	} catch (e) {
		console.error("Failed to save filter order:", e);
	}
};
//#endregion
exports.field2Cpt = field2Cpt;
exports.genFilterItems = genFilterItems;
exports.genGetField = genGetField;
exports.getStoredFilterOrder = getStoredFilterOrder;
exports.saveFilterOrder = saveFilterOrder;
